Signal Handler Added. - redy to deploy
This commit is contained in:
parent
91af9dcc91
commit
bc1f7e0712
1 changed files with 17 additions and 5 deletions
20
main.c
20
main.c
|
@ -21,8 +21,10 @@ void FreeTokens(char** tokens) ;
|
|||
void checkIfQuit(char* word);
|
||||
int getFileNameIndex(char** args);
|
||||
void freeRemainderInArr(char** args, int beforLast);
|
||||
void sigHandler(int sig);
|
||||
|
||||
|
||||
//GLOBAL INT - THIS WILL HOLD CHILD PID OR -1 IF THERE IS NO CHILD
|
||||
int pidOfChild = -1;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -37,7 +39,7 @@ int main()
|
|||
if(getUserName(user_n) == ERROR)
|
||||
goto quit;
|
||||
|
||||
signal(SIGINT, SIG_IGN); // ignore SIGINT
|
||||
signal(SIGINT, sigHandler); // Change the default SIGINT handler
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
@ -114,8 +116,6 @@ int main()
|
|||
}
|
||||
else if(pid == 0) // child
|
||||
{
|
||||
signal(SIGINT, SIG_DFL); // set the child SIGINT handler to default! -> ^C will kill child but not father.
|
||||
|
||||
if(writeToFile == true)
|
||||
{
|
||||
int i_lastArg = getFileNameIndex(args);
|
||||
|
@ -286,3 +286,15 @@ void freeRemainderInArr(char** args, int beforLast)
|
|||
free(args[beforLast]);
|
||||
args[beforLast - 1] = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will be calld when the SIGINT is invoked.
|
||||
* only if there is a child, the "father" will kill it.
|
||||
* @param sig -
|
||||
*/
|
||||
void sigHandler(int sig)
|
||||
{
|
||||
if(pidOfChild >= 0)
|
||||
kill(pidOfChild, SIGINT);
|
||||
pidOfChild = -1;
|
||||
}
|
Loading…
Reference in a new issue