/
/*author:cugwxs
date:2015.12.26
fuction:linux多线程操作demon
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
int main ()
{
pid_t pc0,pc1,pr0,pr1;
unsigned int i,j,z;
pc0 = fork();
if (pc0 < 0)
printf("error0 ocurred!\n");
else if(pc0>0){
pc1=fork();
if(pc1<0)
{
printf("error0 ocurred!\n");
}
else if(pc1>0){
printf("This is child1 process with pid of %d\n",getpid());
for(i=0;i<100;i++)
{
printf("child1:%d\n",i);
}
sleep(5);
}
//if(waitpid(pc1,NULL,0)==pc1)//依次执行进程PC1及其父进程
if(waitpid(pc1,NULL,WNOHANG))//同步执行进程PC1及其父进程
{
printf("This is partent1 process with pid of %d\n",getpid());
for(j=0;j<100;j++)
{
printf("partent1:%d\n",j);
}
}
}
//if(waitpid(pc0,NULL,WNOHANG))//同步执行进程PC1及其父进程和PC0进程
if(waitpid(pc0,NULL,0)==pc0)//依次执行(进程PC1及其父进程)和PC0进程
{
printf("This is partent with pid of %d\n",getpid());
for(z=0;z<20;z++)
{
printf("partent of child1:%d\n",z);
}
}
}