catching signals while reading from pipe with select()

前端 未结 3 2037
盖世英雄少女心
盖世英雄少女心 2020-12-29 00:18

using select() with pipe - this is what I am doing and now I need to catch SIGTERM on that. how can I do it? Do I have to do it when select() retur

3条回答
  •  暖寄归人
    2020-12-29 00:39

    You can call select() in a loop. This is known as restarting the system call. Here is some pseudo-C.

    int retval = -1;
    int select_errno = 0;
    
    do {
       retval = select(...);
    
       if (retval < 0)
       {
           /* Cache the value of errno in case a system call is later
            * added prior to the loop guard (i.e., the while expression). */
           select_errno = errno;
       }
    
       /* Other system calls might be added here.  These could change the
        * value of errno, losing track of the error during the select(),
        * again this is the reason we cached the value.  (E.g, you might call
        * a log method which calls gettimeofday().) */
    
    /* Automatically restart the system call if it was interrupted by
     * a signal -- with a while loop. */
    } while ((retval < 0) && (select_errno == EINTR));
    
    if (retval < 0) {
       /* Handle other errors here. See select man page. */
    } else {
       /* Successful invocation of select(). */
    }
    

提交回复
热议问题