Chinaunix

标题: FIFO难道不会引发SIGPIPE吗? [打印本页]

作者: isnowran    时间: 2006-08-10 15:34
标题: FIFO难道不会引发SIGPIPE吗?
我的系统是redhat-2.4.32,向一个对端未打开的FIFO进行写操作,不会引发SIGPIPE,但是man 4 fifo上面说这样做会引发SIGPIPE

  1. NOTES
  2.        When a process tries to write to a FIFO that is not opened for read  on
  3.        the other side, the process is sent a SIGPIPE signal.

  4.        FIFO special files can be created by mkfifo(3), and are specially indi-
  5.        cated in ls -l.

  6. SEE ALSO
  7.        mkfifo(3),  mkfifo(1),  pipe(2),  socketpair(2),  open(2),   signal(2),
  8.        sigaction(2)

  9. Linux Man Page                    1999-06-20                           FIFO(4)
  10. (END)
复制代码


在man 2 write 中对于EPIPE是这么描述的,不知道它所说的 "pipe or socket" 中的 pipe是否包含fifo( named pipe )

  1.        EPIPE  fd is connected to a pipe or socket whose reading end is closed.
  2.               When  this  happens the writing process will also receive a SIG-
  3.               PIPE signal.  (Thus, the write return value is seen only if  the
  4.               program catches, blocks or ignores this signal.)

复制代码


疑惑中,测试代码

  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <signal.h>

  9. void sigpipe( int )
  10. {
  11.         printf( "caught signal\n" );
  12. }

  13. int main( int argc, char* argv[] )
  14. {
  15.         const char* msg = "test";
  16.         mkfifo( "fifo", 00777 );
  17.         int fd = open( "fifo", O_RDWR );
  18.         if( -1 == fd )
  19.         {
  20.                 perror( "open" );
  21.                 return -1;
  22.         }

  23.         struct sigaction sigact;
  24.         sigact.sa_handler = sigpipe;
  25.         sigemptyset( &sigact.sa_mask );
  26.         sigact.sa_flags = 0;
  27.         if( -1 == sigaction( SIGPIPE, &sigact, NULL ) )
  28.         {
  29.                 perror( "sigaction" );
  30.                 return -1;
  31.         }

  32.         while( 1 )
  33.         {
  34.                 // should lead to caught SIGPIPE
  35.                 if( -1 == write( fd, msg, strlen( msg ) ) )
  36.                 {
  37.                         perror( "write" );
  38.                         return -2;
  39.                 }
  40.         }

  41.         return 0;
  42. }
复制代码

作者: fanyunfei    时间: 2006-08-10 17:52
because you have no set O_NONBLOCK flag, so process block until read open by other process

sorry, my system have no chinese language and my english is poor
作者: isnowran    时间: 2006-08-10 22:23
原帖由 fanyunfei 于 2006-8-10 17:52 发表
because you have no set O_NONBLOCK flag, so process block until read open by other process

sorry, my system have no chinese language and my english is poor


写管道是否会引发SIGPIPE跟写文件描述符是否阻塞是没有关系的
作者: fanyunfei    时间: 2006-08-10 22:26
oh, sorry
作者: isnowran    时间: 2006-08-10 22:30
猫抬头
作者: isnowran    时间: 2006-08-11 10:40
终于找到原因了,写描述符打开为 O_RDWR 模式了, 哈哈




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2