Is there a version of the wait() system call that sets a timeout?

前端 未结 3 1581
悲&欢浪女
悲&欢浪女 2020-12-07 02:31

Is there any way to use the wait() system call with a timeout, besides using a busy-waiting or busy-sleeping loop?

I\'ve got a parent process that

3条回答
  •  难免孤独
    2020-12-07 03:24

    There's not a wait call that takes a timeout.

    What you can do instead is install a signal handler that sets a flag for SIGCHLD, and use select() to implement a timeout. select() will be interrupted by a signal.

    static volatile int punt;
    static void sig_handler(int sig)
    {
        punt = 1;
    }
    
    ...
    
    struct timeval timeout = {10,0};
    int rc;
    
    signal(SIGCHLD, sig_handler);
    
    fork/exec stuff
    //select will get interrupted by a signal
    
    rc = select(0, NULL,NULL,NULL, &timeout ); 
    if (rc == 0) {
    // timed out
    } else if (punt) {
        //child terminated
    }
    

    More logic is needed if you have other signal you need to handle as well though

提交回复
热议问题