免费注册 查看新帖 |

Chinaunix

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

[C] 为什么两个进程,只有一个进程在运行,另外一个进程感觉从来没有跑过 [复制链接]

论坛徽章:
13
CU大牛徽章
日期:2013-03-14 14:14:082016科比退役纪念章
日期:2016-07-22 11:15:35数据库技术版块每日发帖之星
日期:2016-05-27 06:20:002015亚冠之吉达阿赫利
日期: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
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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.     }
复制代码
我看过pd2_output,里面全是X,没有y,也就是说child2 从来没有机会跑过。
为什么呢?

论坛徽章:
0
2 [报告]
发表于 2016-01-26 12:40 |只看该作者
                    kill(child2,SIGUSR2);
这里写的有问题,应该是kill(child2,SIGUSR1). SIGUSR2没有注册handler,这里把child2杀死了。

论坛徽章:
13
CU大牛徽章
日期:2013-03-14 14:14:082016科比退役纪念章
日期:2016-07-22 11:15:35数据库技术版块每日发帖之星
日期:2016-05-27 06:20:002015亚冠之吉达阿赫利
日期: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 [报告]
发表于 2016-01-26 13:00 |只看该作者
怪不得啊,我这才发现,多谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP