Program received signal SIGPIPE, Broken pipe

后端 未结 5 1944
陌清茗
陌清茗 2020-12-04 14:50

I write a client program based on posix sockets. The program creates multiple threads and is going to lock the server. But during debug in gdb time the program gives an info

5条回答
  •  一整个雨季
    2020-12-04 15:10

    The process received a SIGPIPE. The default behaviour for this signal is to end the process.

    A SIGPIPE is sent to a process if it tried to write to a socket that had been shutdown for writing or isn't connected (anymore).

    To avoid that the program ends in this case, you could either

    • make the process ignore SIGPIPE

      #include 
      
      int main(void)
      {
        sigaction(SIGPIPE, &(struct sigaction){SIG_IGN}, NULL);
      
        ...
      

      or

    • install an explicit handler for SIGPIPE (typically doing nothing):

      #include 
      
      void sigpipe_handler(int unused)
      {
      }
      
      int main(void)
      {
        sigaction(SIGPIPE, &(struct sigaction){sigpipe_handler}, NULL);
      
        ...
      

    In both cases send*()/write() would return -1 and set errno to EPIPE.

提交回复
热议问题