Reputation: 31
I'm trying to suspend the process but it doesn't work. this is part of code. there are Ctrl+Z and Ctrl+C. Ctrl+C is working. I cannot get why with Z it doesn't work. (the actual code):
//ctrl+Z
void sigstop(int p){
signal(SIGTSTP,&sigstop);
kill(my_pid,SIGSTOP);
fflush(stdout);
}
// Ctrl+C
void sigkill(int p){
signal(SIGINT,&sigkill);
kill(my_pid,SIGKILL);
fflush(stdout);
}
Code in the main method:
...
my_pid = fork();
if (my_pid == 0) {
signal(SIGTSTP,&sigstop); //for Ctrl+Z
signal(SIGINT,&sigkill); //for Ctrl+C
checkCommand();
execvp(argv[0], argv);
exit(getpid());
}
Upvotes: 3
Views: 19614
Reputation: 196
I think that calling kill() in the sigkill function, just begins an infinite, recursive loop where the kill() just calls again the sigkill function, which calls kill() which calls again sigkill function... etc... Instead of calling kill(), set a global boolean variable and check for it in your main function. If this global boolean variable is set, you just exit gracefully.
Something like:
volatile bool gTerminate = false;
void sigkill(int p)
{
gTerminate = true;
signal(SIGINT, &sigkill);
}
int main(...)
{
//initialization stuff...
while( !gTerminate )
{
//do stuff
}
return -1;
}
Upvotes: 0
Reputation: 140659
Instead of installing signal handlers for SIGTSTP
and SIGINT
, put the terminal into raw mode with cfmakeraw
or tcsetattr
. ^C and ^Z will then be readable as ordinary characters, which should be much less troublesome. However, you will then need to implement line-editing yourself -- GNU readline is your friend there. For further advice, please see the Implementing a Shell and Job Control sections of the GNU C Library Manual. (You can safely ignore the part where it tries to warn you that job control might not be supported by the kernel -- if anyone is still using one of those systems anymore, they have only themselves to blame for it.)
Upvotes: 6