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

  免費注冊 查看新帖 |

Chinaunix

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

select和pipe的一個新的認識 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-07-19 00:06 |只看該作者 |倒序瀏覽
本帖最后由 kgn28 于 2010-07-19 00:08 編輯

先看程序:
  1. #include<iostream>
  2. using namespace std ;

  3. #include <fcntl.h>
  4. #include<unistd.h>
  5. #include<stdlib.h>
  6. #include<sys/types.h>
  7. #include<sys/wait.h>

  8. int inline my_sleep(int times){
  9.         struct timeval t;
  10.         t.tv_sec = times / 1000;
  11.         t.tv_usec = (times % 1000) * 1000;
  12.         select(0, NULL, NULL, NULL, &t);
  13. }

  14. int  main(int argc, char * argv)
  15. {
  16.         int n, fd[2];
  17.         pid_t p;
  18.         char buf[2048];
  19.         int maxfds;
  20.         struct timeval timeout;
  21.         int select_ret;

  22.         if(pipe(fd) < 0){
  23.                 perror("pipe create error");
  24.                 exit(2);
  25.         }

  26.         if((p = fork()) < 0){
  27.                 perror("fork error");
  28.         }else if( p  == 0) {
  29.                 close(fd[0]);
  30.                 cout<<"hi, I am the child process"<<endl;
  31.                 close(1);
  32.                 if(dup2(fd[1],1) < 0){
  33.                         perror("dup2 error");
  34.                         exit(1);
  35.                 }

  36.                 int ret = system("./scipt.sh ");

  37.                 if(ret < 0 ) {
  38.                         perror("system() error!\n");
  39.                 }else{
  40.                         if(ret == 127){
  41.                                 cout<<"Startup shell script error"<<endl;
  42.                         }else if(ret == 0){
  43.                                 cout<<"success!"<<endl;
  44.                         }else{
  45.                                 cout<<"Executing command error!"<<endl;
  46.                         }
  47.                 }
  48.                 sleep(200);
  49. //                while(1);
  50.                 exit(1);
  51.         }else{
  52.                 cout<<"Hi, i am the parent process"<<endl;
  53.                 close(fd[1]); //close write fd
  54.                 fd_set readfds;
  55.                 FD_ZERO(&readfds);
  56.                 FD_SET(fd[0],&readfds);
  57.                 maxfds = fd[0]+1;
  58.                 timeout.tv_sec =2 ;
  59.                 timeout.tv_usec = 0;
  60.                 while(true){
  61.                         select_ret = select(maxfds,&readfds,NULL,NULL,&timeout);
  62.                         if(select_ret > 0){
  63.                                 int len;
  64.                                 printf("select_ret is %d\n", select_ret);
  65.                                 if(FD_ISSET(fd[0], &readfds)){
  66.                                         len = read(fd[0],buf,2048);
  67.                                         buf[len] = '\0';
  68.                                         printf("the message recv form child pross is :%s# len=%d\n",buf, len);
  69.                                         memset(buf, 0x0, 2048);
  70.                                 }
  71.                         } else {
  72.                                 printf("select timeout\n");
  73.                         }
  74.                         timeout.tv_sec =2 ;
  75.                         timeout.tv_usec = 0;
  76.                 }
  77.                 wait(NULL);
  78.                 return 0;
  79.         }
  80. }
復制代碼
起初,子進程在printf后馬上退出(exit),然后產(chǎn)生的情況就是父進程這么select會很快的返回,并且read的返回值也是0. 加了sleep之后父進程的select都是timeout。這個現(xiàn)象說明:

1,重新考察select的功能,select測試的是文件描述符是否(可讀、可寫、例外),如果對一個文件描述符的read不會被block,那么在測試該描述符可讀時,select馬上返回。

2,pipe,如果子進程把write的文件描述符關(guān)閉,那么父進程read的時候不會被block并且會一直返回0以示write端已經(jīng)被關(guān)閉,所以這是去select,肯定是馬上返回,read的返回值也就是0了。

論壇徽章:
1
技術(shù)圖書徽章
日期:2013-11-12 10:33:00
2 [報告]
發(fā)表于 2013-08-14 20:11 |只看該作者
今天也遇到類似的問題,如果是命名管道的話,可以通過以O(shè)_RDWR open讀端的方式來解決

原文:http://www.myvoipapp.com/blogs/y ... ifo%E4%B8%8Eselect/
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(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