亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2011 | 回復(fù): 0
打印 上一主題 下一主題

[Linux] 如果寫到管道時讀進程已終止會產(chǎn)生SIGPIPE---怎么實現(xiàn)? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-01-16 14:36 |只看該作者 |倒序瀏覽
我在學(xué)習(xí)SIGPIPE時,按照描述就是:如果寫到管道時讀進程已終止會產(chǎn)生SIGPIPE。另外還有一個socket也會出這個信號,不過socket這個先不管了。我學(xué)習(xí)FIFO,就想實現(xiàn)“如果寫到管道時讀進程已終止會產(chǎn)生SIGPIPE”(注:APUE上寫的)。
我實現(xiàn)代碼如下:
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <signal.h>
  9. #include <unistd.h>

  10. #define FIFO_SERVER "/tmp/myfifo"

  11. void my_func(int sign_no)
  12. {
  13.         if(sign_no == SIGPIPE)
  14.         {
  15.                 printf("Sorry Sir,I have get SIGPIPE\n");
  16.         }
  17.     else
  18.         exit(1);
  19. }

  20. int main(int argc, char** argv)
  21. {
  22.         int fd;
  23.         char w_buf[100];
  24.         int nwrite;
  25.        
  26.         signal(SIGPIPE, my_func);
  27.        
  28.         if((mkfifo(FIFO_SERVER, O_CREAT|O_EXCL|O_RDWR)<0) && (errno!=EEXIST))
  29.     {
  30.                 printf("Cant create fifioserver\n");
  31.         }       
  32.    
  33.     //fd = creat(FIFO_SERVER,O_RDWR|O_NONBLOCK);
  34.         fd = open(FIFO_SERVER, O_RDWR|O_NONBLOCK, 0);
  35.         if(fd == -1)
  36.         {
  37.                 perror("open");
  38.                 exit(1);
  39.         }
  40.        
  41.        
  42.         if(argc == 1)
  43.         {
  44.                 printf("Please send something\n");
  45.                 exit(-1);
  46.         }
  47.         strcpy(w_buf, argv[1]);
  48.     while(1)
  49.     {
  50.         
  51.         nwrite = write(fd, w_buf, 100);       
  52.             if(nwrite == -1)
  53.             {
  54.                     if(errno == EAGAIN)
  55.                             printf("The FIFO has not been read yet,Please try later\n");
  56.             }
  57.             else
  58.             {
  59.                     printf("Write %s to the FIFO\n", w_buf);
  60.             }
  61.         sleep(1);
  62.     }
  63.         close(fd);
  64.         return 0;
  65. }
復(fù)制代碼
然后在另一個終端手動刪掉/tmp/myfifo這個管道,但是卻不行,沒有出現(xiàn)SIGPIPE這個信號,誰能幫幫我呀。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP