linux多进程操作demon

/
/*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);
				}
			}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值