1

what i am trying to do is that when my program receives SIGSTOP, it should send SIGCONT to itself. if i do it on terminal, it works but i want to do it in my program. I tried something like this, but it doesn't work..

can you help me?

int main()
{
    pid_t pid;
    pid = fork();

    if(pid > 0)
    {
       int i = 0;

        while(1)
        {
            if(i == 5)
            {
                kill(getpid(), SIGSTOP);
            }
            printf("i = %d\n" ,i);
            i++;
            sleep(1);
        } 
    }

    if(pid == 0)
    {
        while(1)
        {
            kill(getpid(), SIGCONT);
        }
    }
    return 0 ;
}
Wokers
  • 107
  • 3
  • 13
  • When you type Control-z in the terminal it sends `SIGTSTP`, not `SIGSTOP`. – Barmar Apr 11 '20 at 22:02
  • "if i do it on terminal". How is that possible to do even on the terminal? I doubt you are really doing that so please show the commands that you think achieves that. I don't think it is possible to do what you want either from the terminal or programmatically. From the [signal man page](http://man7.org/linux/man-pages/man7/signal.7.html): " The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored" – kaylum Apr 11 '20 at 22:04
  • i run the program, after i find out the process id on the terminal, kill -SIGSTOP 5014 , then program stops.. after i write kill -SIGCONT 5014 and it continues. – Wokers Apr 11 '20 at 22:06
  • Well that's not the process sending to itself, is it? Another process, the shell, is sending to the stopped process. So it's totally not the same as what your question seems to be asking about. – kaylum Apr 11 '20 at 22:08
  • yes, and i'm asking how can it send to itself. – Wokers Apr 11 '20 at 22:09
  • The answer: It can't be done. – kaylum Apr 11 '20 at 22:10

1 Answers1

3

You're mixing up which PID is which. The child is sending itself SIGCONT, which does nothing since it's already running. Make it send the parent SIGCONT instead.

  • it worked, thanks! when my program gets complicated, i should send SIGCONT in while loop right? and if yes, how should i locate while loop in my program? because if i use while loop, i can't do any other things. And if i calculate something else in while loop, this signal wont be sent frequently. – Wokers Apr 11 '20 at 22:12
  • @Wokers In all likelihood, you shouldn't be doing what you're doing at all in a "real" program. Why do you want it to immediately continue after being stopped anyway? – Joseph Sible-Reinstate Monica Apr 11 '20 at 22:14
  • because my prof want us to do so.. SIGSTOP will reach to my program and i need to handle. since it can not be handled, i found out to continue my program where it stops. – Wokers Apr 11 '20 at 22:18