分享

进程控制:linux中fork同时创建多个子进程注意事项

 mediatv 2014-12-03

/******************************************************************************************************************

参考:http://blog.sina.com.cn/s/blog_605f5b4f0100x444.html

说明:linux中fork同时创建多个子进程注意事项

******************************************************************************************************************/ 

            也算实验出来了吧,不过还好有网络,不然好多自己解决不了的问题真就解决不了了。我先写了个这样的创建多个子进程的程序(模仿书上创建一个进程的程序):

  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/wait.h>  
  4. #include <stdlib.h>  
  5.   
  6. int main(void)  
  7. {  
  8.     pid_t childpid1, childpid2, childpid3;  
  9.     int status;  
  10.     int num;  
  11.     childpid1 = fork();  
  12.     childpid2 = fork();  
  13.     childpid3 = fork();  
  14.     if( (-1 == childpid1)||(-1 == childpid2)||(-1 == childpid3))  
  15.     {  
  16.         perror("fork()");  
  17.         exit(EXIT_FAILURE);  
  18.     }  
  19.     else if(0 == childpid1)  
  20.     {  
  21.         printf("In child1 process\n");  
  22.         printf("\tchild pid = %d\n", getpid());  
  23.         exit(EXIT_SUCCESS);  
  24.     }  
  25.         else if(0 == childpid2)  
  26.         {  
  27.             printf("In child2 processd\n");  
  28.             printf("\tchild pid = %d\n", getpid());  
  29.             exit(EXIT_SUCCESS);  
  30.         }  
  31.             else if(0 == childpid3)  
  32.             {  
  33.                 printf("In child3 process\n");  
  34.                 printf("\tchild pid = %d\n", getpid());  
  35.                 exit(EXIT_SUCCESS);  
  36.             }  
  37.                 else  
  38.                 {  
  39.                     wait(NULL);  
  40.                     puts("in parent");  
  41.             //      printf("\tparent pid = %d\n", getpid());  
  42.             //      printf("\tparent ppid = %d\n", getppid());  
  43.             //      printf("\tchild process exited with status %d \n", status);  
  44.                 }  
  45.     exit(EXIT_SUCCESS);  
  46.       
  47. }  

结果搞出两个僵尸来,怎么都想不明白,最后在网上找到了答案。并来回揣摩出来了方法和注意事项:
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/wait.h>  
  4. #include <stdlib.h>  
  5.   
  6. int main(void)  
  7. {  
  8.     pid_t childpid1, childpid2, childpid3;  
  9.     int status;  
  10.     int num;  
  11.       
  12.     /* 这里不可以一下就创建完子进程,要用 
  13.     *要 创建-》判断-》使用-》return or exit.更不能这样如test2.c 
  14.     *childpid1 = fork(); 
  15.     *childpid2 = fork(); 
  16.     *childpid3 = fork(); 
  17.     */  
  18.     childpid1 = fork();                                             //创建  
  19.     if(0 == childpid1)                                              //判断  
  20.     {                                                                                   //进入  
  21.         printf("In child1 process\n");  
  22.         printf("\tchild pid = %d\n", getpid());  
  23.         exit(EXIT_SUCCESS);                                         //退出  
  24.     }  
  25.     childpid2 = fork();  
  26.     if(0 == childpid2)  
  27.     {  
  28.         printf("In child2 processd\n");  
  29.         printf("\tchild pid = %d\n", getpid());  
  30.         exit(EXIT_SUCCESS);  
  31.     }  
  32.     childpid3 = fork();  
  33.     if(0 == childpid3)  
  34.     {  
  35.         printf("In child3 process\n");  
  36.         printf("\tchild pid = %d\n", getpid());  
  37.         exit(EXIT_SUCCESS);  
  38.     }  
  39.     //这里不可以用wait(NULL),多个子进程是不可以用wait来等待的,它只会等待一个 其它都成僵尸了  
  40.     waitpid(childpid1, NULL, 0);  
  41.     waitpid(childpid2, NULL, 0);  
  42.     waitpid(childpid3, NULL, 0);  
  43.     puts("in parent");  
  44.   
  45.     exit(EXIT_SUCCESS);  
  46.       
  47. }  

严格照着这样做就不会出现 僵尸,也不会影响其它进程。

创建-》判断-》使用-》return or exit.



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多