- #include"stdio.h"
- #include"unistd.h"
- #include"sys/types.h"
- #include"signal.h"
- #include"wait.h"
- voidsigchld_handler(intsig)
- {
- pid_tpid;
- intstatus;
- for(;(pid=waitpid(-1,&status,WNOHANG))>0;)
- {
- printf("child%ddied:%d\n",pid,WEXITSTATUS(status));
- printf("hi,parentprocessreceivedSIHHLDsignalsuccessfully!\n");
- }
- return;
- }
- voidmain()
- {
- //pid_tpc,pr;
- intpc=fork();
- if(pc==0)
- {
- printf("子进程!\n");
- sleep(1);
- printf("Thisischildprocesswithpidof%d\n",getpid());
- //exit(1);
- }
- elseif(pc>0)
- {
- signal(SIGCHLD,sigchld_handler);
- pause();
- }
- else
- {
- printf("创建进程出错!\n");
- exit(1);
- }
- }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702521