免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1984 | 回复: 5
打印 上一主题 下一主题

FIFO难道不会引发SIGPIPE吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-10 15:34 |只看该作者 |正序浏览
我的系统是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. }
复制代码

论坛徽章:
0
6 [报告]
发表于 2006-08-11 10:40 |只看该作者
终于找到原因了,写描述符打开为 O_RDWR 模式了, 哈哈

论坛徽章:
0
5 [报告]
发表于 2006-08-10 22:30 |只看该作者
猫抬头

论坛徽章:
0
4 [报告]
发表于 2006-08-10 22:26 |只看该作者
oh, sorry

论坛徽章:
0
3 [报告]
发表于 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跟写文件描述符是否阻塞是没有关系的

论坛徽章:
0
2 [报告]
发表于 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
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP