Wincij
Wincij

Reputation: 111

Multi-proccess pause and resume programme

I serached it but i can't find anything i am looking for. Can anyone give a simple example how to pause for a while (not for given time, like sleep()) and proceed programme ? I tried something but it just pause, and then the only thing i can do is to terminate the program before the second printf:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include<termios.h>
#include<signal.h>
#include <sys/wait.h>

struct sigaction act;
sigset_t to_start, to_stop;




int main(int argc, char *argv[])
{



  int i=0;
printf("BEFORE SIGSUSPEND");




sigemptyset(&to_stop);
sigsuspend(&to_stop);

printf("AFTER SIGSUSPEND");
fflush(stdout)





    return 0;
}

Upvotes: 1

Views: 178

Answers (1)

Petr Skocik
Petr Skocik

Reputation: 60056

Your child process should start with the signal you want to send blocked.

Then you need to make sure:

  1. a delivery of the signal won't kill the process (=> you need to set up a signal handler for it (an empty one will do))
  2. the signal can be delivered (=> sigsuspend will do that)

In code:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void handler(int X) { }
int main(void)
{
    sigset_t to_stop;
    sigemptyset(&to_stop);
    sigaction(SIGINT /*you'd probably use SIGUSR1 here*/,
      &(struct sigaction){ .sa_handler = handler }, 0);

    puts("BEFORE");
    /*SIGINT will now wake up the process without killing it*/
    sigsuspend(&to_stop);
    puts("AFTER");
}

You should be able to try this out with Ctrl+C. In real code, you probably should be using SIGUSR1/SIGUSR1 or one of the realtime signals for this.

Upvotes: 1

Related Questions