catching signals while reading from pipe with select()

前端 未结 3 2043
盖世英雄少女心
盖世英雄少女心 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:32

    The answer is partly in one of the comment in the Q&A you point to;

    > Interrupt will cause select() to return a -1 with errno set to EINTR

    That is; for any interrupt(signal) caught the select will return, and the errno will be set to EINTR.

    Now if you specifically want to catch SIGTERM, then you need to set that up with a call to signal, like this;

    signal(SIGTERM,yourcatchfunction);
    

    where your catch function should be defined something like

    void yourcatchfunction(int signaleNumber) { .... }
    

    So in summary, you have setup a signal handler yourcatchfunction and your program is currently in a select() call waiting for IO -- when a signal arrives, your catchfunction will be called and when you return from that the select call will return with the errno set to EINTR.

    However be aware that the SIGTERM can occur at any time so you may not be in the select call when it occur, in which case you will never see the EINTR but only a regular call of the yourcatchfunction

    Hence the select() returning with err and errno EINTR is just so you can take non-blocking action -- it is not what will catch the signal.

提交回复
热议问题