How do I use signalfd and epoll to get an event when my child exits?

I create sigset_t

and set it empty, then add to it SIGCHLD

, then set it BLOCK

:

sigset_t    sigmask;
sigemptyset (&sigmask);

sigprocmask (SIG_BLOCK, &sigmask, NULL);

      

Then create signalfd

viasignalfd4()

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

      

Then add it to the epollfd that you created earlier:

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;

Finally, I use epoll_wait(epollfd, ...)

senconds to wait for my child process, which is executed with fork () and sleep 2 senconds, but nothing comes out but nothing comes back.

Someone please help me with this? Thank!

+3


source to share


1 answer


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

  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 it is an fd signal, 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 is supposed to do when he got SIGCHILD

          } else {

      

some other signal

          }                                                                                                                     
        }                                                                                                                       
      }                                                                                                                         
    }                                                                                                                           
  } else {                                                                                                                      
  // child    

      

child code

+1


source







All Articles