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

  免費注冊 查看新帖 |

Chinaunix

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

[C] 為什么兩個進(jìn)程,只有一個進(jìn)程在運行,另外一個進(jìn)程感覺從來沒有跑過 [復(fù)制鏈接]

論壇徽章:
13
CU大;照
日期:2013-03-14 14:14:082016科比退役紀(jì)念章
日期:2016-07-22 11:15:35數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-05-27 06:20:002015亞冠之吉達(dá)阿赫利
日期:2015-08-05 10:06:542015年亞洲杯之韓國
日期:2015-04-01 16:05:42雙魚座
日期:2014-11-13 11:04:24丑牛
日期:2014-07-25 17:29:54子鼠
日期:2014-04-25 12:25:45丑牛
日期:2014-04-17 08:35:48巨蟹座
日期:2014-04-16 16:50:05CU大;照
日期:2013-03-14 14:14:29CU大牛徽章
日期:2013-03-14 14:14:26
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2016-01-26 11:46 |只看該作者 |倒序瀏覽
代碼如下:
  1. #include <apue.h>
  2. #include <sys/wait.h>
  3. #include <unistd.h>
  4. #include <limits.h>
  5. #include <fcntl.h>


  6. #define READ_FD 0
  7. #define WRITE_FD 1
  8. #define RD_CHUNK 10
  9. #define ATOMIC

  10. #ifndef PIPE_BUF
  11.     #define PIPE_BUF _POSIX_PIPE_BUF
  12. #endif

  13. void do_nothing(int signo)
  14. {
  15.     return ;
  16. }

  17. int main(int argc, char *argv[])
  18. {
  19.     int i,repeat;
  20.     int bytesread;
  21.     int mssglen;
  22.     pid_t child1, child2;
  23.     int fd[2];
  24.     int outfd;

  25.     char message[RD_CHUNK + 1];
  26.     char *Child1_Chunk, *Child2_Chunk;
  27.     long Chunk_Size;

  28.     static struct sigaction sigact;

  29.     sigact.sa_handler=do_nothing;
  30.     sigfillset(&(sigact.sa_mask));
  31.     sigaction(SIGUSR1, &sigact, NULL);

  32.     //check argc
  33.     if( argc < 2){
  34.         fprintf(stderr,"Usage: %s size \n",argv[0]);
  35.         exit(1);
  36.     }

  37.     //try to create pipe
  38.     if(-1 == pipe(fd)){
  39.         perror("pipe call");
  40.         exit(2);
  41.     }

  42.     repeat=atoi(argv[1]);

  43. #ifdef ATOMIC
  44.     Chunk_Size = PIPE_BUF;
  45. #else
  46.     Chunk_Size = PIPE_BUF + 200;
  47. #endif
  48.     printf("Chunk size =%ld\n",Chunk_Size);
  49.     printf("Value of PIPE_BUF is %ld\n",PIPE_BUF);
  50.     Child1_Chunk=calloc(Chunk_Size,sizeof(char));
  51.     Child2_Chunk=calloc(Chunk_Size,sizeof(char));
  52.     if((NULL == Child1_Chunk) ||(NULL == Child2_Chunk)){
  53.         perror("calloc");
  54.         exit(2);
  55.     }

  56.     //create the string that child1 writes
  57.     Child1_Chunk[0] = '\0';     //just to be safe
  58.     for(i=0; i < Chunk_Size -2; i++)   
  59.         strcat(Child1_Chunk,"X");
  60.     strcat(Child1_Chunk,"\n");

  61.     //create the string that child2 writes
  62.     Child2_Chunk[0]='\0';       //just to be safe
  63.     for(i=0;i<Chunk_Size -2;i++)
  64.         strcat(Child2_Chunk,"y");
  65.     strcat(Child2_Chunk,"\n");

  66.     //create first child process
  67.     switch(child1 = fork()){
  68.         case -1:            //fork failed -- exit
  69.             perror("fork()");
  70.             exit(3);

  71.         case 0:             //child1 code
  72.             mssglen=strlen(Child1_Chunk);
  73.             pause();
  74.             sleep(5);
  75.             for(i=0;i<repeat;i++){
  76.                 if(write(fd[WRITE_FD],Child1_Chunk,mssglen)!= mssglen){
  77.                     perror("write");
  78.                     exit(4);
  79.                 }
  80.             }
  81.             close(fd[WRITE_FD]);
  82.             exit(0);

  83.         default:        //parent creates second child process
  84.             switch(child2 = fork()){
  85.                 case -1:        //fork failed --exit
  86.                     perror("fork()");
  87.                     exit(5);

  88.                 case 0:         //child2 code
  89.                     mssglen=strlen(Child2_Chunk);
  90.                     pause();
  91.                     for(i=0;i<repeat;i++){
  92.                         if(write(fd[WRITE_FD],Child2_Chunk,mssglen)!= mssglen){
  93.                             perror("write");
  94.                             exit(6);
  95.                         }
  96.                     }
  97.                     close(fd[WRITE_FD]);
  98.                     exit(0);

  99.                 default:        //parent code
  100.                     outfd=open("pd2_output",O_WRONLY|O_CREAT|O_TRUNC,0644);
  101.                     if(-1==outfd){
  102.                         perror("open");
  103.                         exit(7);
  104.                     }
  105.                     close(fd[WRITE_FD]);
  106.                     kill(child2,SIGUSR2);
  107.                     kill(child1,SIGUSR1);
  108.                     while((bytesread = read(fd[READ_FD],message,RD_CHUNK))!=0)
  109.                         if(bytesread >0 ){          //more data
  110.                             write(outfd,message,bytesread);
  111.                         }
  112.                         else{
  113.                             perror("read() ");
  114.                             exit(8);
  115.                         }
  116.                     close(outfd);
  117.                     //collect zombies
  118.                     for(i=1;i<=2;i++)
  119.                         if(wait(NULL)==-1){
  120.                             perror("wait failed");
  121.                             exit(9);
  122.                         }
  123.                         close(fd[READ_FD]);
  124.                         free(Child1_Chunk);
  125.                         free(Child2_Chunk);
  126.                     }
  127.                     exit(0);
  128.             }
  129.     }
復(fù)制代碼
我看過pd2_output,里面全是X,沒有y,也就是說child2 從來沒有機(jī)會跑過。
為什么呢?

論壇徽章:
0
2 [報告]
發(fā)表于 2016-01-26 12:40 |只看該作者
                    kill(child2,SIGUSR2);
這里寫的有問題,應(yīng)該是kill(child2,SIGUSR1). SIGUSR2沒有注冊handler,這里把child2殺死了。

論壇徽章:
13
CU大牛徽章
日期:2013-03-14 14:14:082016科比退役紀(jì)念章
日期:2016-07-22 11:15:35數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-05-27 06:20:002015亞冠之吉達(dá)阿赫利
日期:2015-08-05 10:06:542015年亞洲杯之韓國
日期:2015-04-01 16:05:42雙魚座
日期:2014-11-13 11:04:24丑牛
日期:2014-07-25 17:29:54子鼠
日期:2014-04-25 12:25:45丑牛
日期:2014-04-17 08:35:48巨蟹座
日期:2014-04-16 16:50:05CU大牛徽章
日期:2013-03-14 14:14:29CU大;照
日期:2013-03-14 14:14:26
3 [報告]
發(fā)表于 2016-01-26 13:00 |只看該作者
怪不得啊,我這才發(fā)現(xiàn),多謝。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(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