欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
進(jìn)程控制:linux中fork同時(shí)創(chuàng )建多個(gè)子進(jìn)程注意事項

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

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

說(shuō)明:linux中fork同時(shí)創(chuàng )建多個(gè)子進(jìn)程注意事項。

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

            也算實(shí)驗出來(lái)了吧,不過(guò)還好有網(wǎng)絡(luò ),不然好多自己解決不了的問(wèn)題真就解決不了了。我先寫(xiě)了個(gè)這樣的創(chuàng )建多個(gè)子進(jìn)程的程序(模仿書(shū)上創(chuàng )建一個(gè)進(jìn)程的程序):

  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. }  

結果搞出兩個(gè)僵尸來(lái),怎么都想不明白,最后在網(wǎng)上找到了答案。并來(lái)回揣摩出來(lái)了方法和注意事項:
  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.     /* 這里不可以一下就創(chuàng )建完子進(jìn)程,要用 
  13.     *要 創(chuàng )建-》判斷-》使用-》return or exit.更不能這樣如test2.c 
  14.     *childpid1 = fork(); 
  15.     *childpid2 = fork(); 
  16.     *childpid3 = fork(); 
  17.     */  
  18.     childpid1 = fork();                                             //創(chuàng )建  
  19.     if(0 == childpid1)                                              //判斷  
  20.     {                                                                                   //進(jìn)入  
  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),多個(gè)子進(jìn)程是不可以用wait來(lái)等待的,它只會(huì )等待一個(gè) 其它都成僵尸了  
  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. }  

嚴格照著(zhù)這樣做就不會(huì )出現 僵尸,也不會(huì )影響其它進(jìn)程。

創(chuàng )建-》判斷-》使用-》return or exit.



本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
linux中創(chuàng )建多個(gè)子進(jìn)程的方法
Linux fork與vfork的深入分析
學(xué)習linux C編程實(shí)戰-----linux進(jìn)程控制
Linux--進(jìn)程組、會(huì )話(huà)、守護進(jìn)程
多進(jìn)程編程小例子(YC)
socketpair理解
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久