现在的位置: 首页 > 综合 > 正文

阻塞型通信程序

2017年12月19日 ⁄ 综合 ⁄ 共 810字 ⁄ 字号 评论关闭

 

  1. #include"stdio.h"  
  2. #include"unistd.h"  
  3. #include"sys/types.h"  
  4. #include"signal.h"  
  5. #include"wait.h"  
  6.  
  7. void sigchld_handler(int sig)  
  8. {  
  9.     pid_t pid;  
  10.     int status;  
  11.     for(;(pid=waitpid(-1,&status,WNOHANG))>0;)  
  12.     {  
  13.         printf("child %d died :%d\n",pid,WEXITSTATUS(status));  
  14.         printf("hi,parent process received SIHHLD signal successfully!\n");  
  15.     }  
  16.     return;  
  17. }  
  18.  
  19. void main()  
  20. {  
  21.     //pid_t pc,pr;  
  22.     int pc=fork();  
  23.     if(pc==0)  
  24.     {  
  25.         printf("子进程!\n");  
  26.         sleep(1);  
  27.         printf("This is child process with pid of %d\n", getpid());  
  28.         //exit(1);  
  29.     }  
  30.     else if(pc>0)  
  31.     {      
  32.         signal(SIGCHLD,sigchld_handler);  
  33.         pause();  
  34.     }  
  35.     else 
  36.     {  
  37.         printf("创建进程出错!\n");  
  38.         exit(1);  
  39.     }  
  40. }  
  41.  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702521

抱歉!评论已关闭.