Tariq Ganem
Tariq Ganem

Reputation: 38

Suspend and resume process via signals

I have two problems/questions here!

1) I tried to catch CTRL+Z and handle it but nothing happens!!

2) how can i test the SIGCONT ? (it doesn't have a shortcut like CTRL+ ..)

Is there anything wrong with my code? This my code below :

void sigstop()
{  
   printf(" Suspended\n");
}

void sigcont()
{  
   printf(" Its Back\n");
}

void sigint()
{ 
    printf(" Interrupt\n");
    //exit(0);
}

int main(int argc, char **argv){ 
    printf("Starting the program\n");
    signal(SIGSTOP,sigstop);
    signal(SIGCONT,sigcont);
    signal(SIGINT, sigint);
    while(1) {
        sleep(2);
    }   
    return 0;
}

Upvotes: 0

Views: 2761

Answers (1)

P.P
P.P

Reputation: 121387

1) I tried to catch CTRL+Z and handle it but nothing happens!!

CTRL + Z sends SIGTSTP. So you need to setup handler for SIGTSTP (not SIGSTOP). SIGSTOP and SIGKILL signals can't caught or handled; so your handler for SIGSTOP will be ignored.

2) how can i test the SIGCONT ? (it doesn't have a shortcut like CTRL+ ..)

Shells typically have a built-in command called fg. So once you suspend your process with CTRL + Z, you'd be able to send SIGCONT via fg. But you can always use the shell builtin kill (or the kill command) to send the desired signal to your process.


Couple of other issues in your code:

  1. signal function should be of the signal void func(int sig) { .. }. So you need to fix your signal functions.

  2. printf is not async-signal-safe and thus it can't be safely called from a signal handler.

Upvotes: 1

Related Questions