ScorpioCPH
ScorpioCPH

Reputation: 171

How to use signalfd and epoll to get event when my child process exit?

I create a sigset_t and set it empty, then add SIGCHLD to it, then set it BLOCK:

sigset_t    sigmask;
sigemptyset (&sigmask);

sigprocmask (SIG_BLOCK, &sigmask, NULL);

Then create a signalfd via signalfd4()

int signalfd = signalfd4 (-1, &sigmask, sizeof(sigset_t), SFD_NONBLOCK);

Then add it to a epollfd which created previously:

struct epoll_event      epev;
memset (&epev, 0, sizeof(struct epoll_event));
epev.events        = POLLIN;

int retval = epoll_ctl (epollfd, EPOLL_CTL_ADD, signalfd, &epev);

and retval is 0;

At last, I use epoll_wait(epollfd, ...) to wait my child process which careate by fork() and sleep 2 senconds without do anything then exit, but nothing returned.

Anyone please give me some help on this? Thank you!

Upvotes: 4

Views: 3784

Answers (1)

Martin Redmond
Martin Redmond

Reputation: 13986

After setting up the signal and the signal fd I found the following worked:

  epev.fd = sigfd;   // sigfd if from signalfd, need to remember it
  pid_t me = getpid();                                                                                             
  fork();                                                                                                                       

For the parent:

  if ( me == getpid() ) {                                                                                                       
    // parent                                                                                                                   
    while ( 1 ) { // loop of epoll_wait
      retval = epoll_wait ( epollfd, &epev, 1, -1 );

If this is the signal fd read it to find which signal was sent.

      if ( epev.data.fd == sigfd ) {
        struct signalfd_siginfo si;                                                                                             
        while ( 1 ) {                                                                                                           
          int nr = read ( sigfd, &si, sizeof si );                                                                              
          if ( nr != sizeof si ) {                                                                                              
            break;  // done reading from the signal fd
          }                                                                                                                     
          if ( si.ssi_signo == SIGCHLD ) {                                                                                      

do what the parent needs to do when it received SIGCHILD

          } else {

some other signal

          }                                                                                                                     
        }                                                                                                                       
      }                                                                                                                         
    }                                                                                                                           
  } else {                                                                                                                      
  // child    

child code

Upvotes: 1

Related Questions