用父进程创建两个子进程
#include<stdio.h>#include<unistd.h>main(){inti;intpid1,pid2;//for(i=0;i<10;i++){if(pi...
#include<stdio.h>
#include<unistd.h>
main()
{
int i;
int pid1,pid2;
//for(i=0;i<10;i++){
if(pid1=fork()==-1)
{
printf("Create Process fail!\n");
//exit(EXIT_FAILURE);
}
if(pid1==0)
{printf("Child1:my pid is%d\n",getpid());
//exit(1);
//break;
}
else
{
if(pid2=fork()==-1);
printf("Create Process fail!\n");
//exit(EXIT_FAILURE);
if(pid2==0)
printf("Child2:my id is%d\n",getpid());
//sleep(5);
else//执行父进程代码
printf("Parent:my id is%d\n",getpid());
}
//return;
}
以上是程序,这是在Linux系统下的编程,输出的结果是
Child1:my id is 7074
Child1:my id is 7073
我想要的结果是
Child1:my id is ****
child2:my id is ****
parent:my id is ****
为什么会出现上面的情况?还有怎么改才能得到预期的结果啊?谢谢!! 展开
#include<unistd.h>
main()
{
int i;
int pid1,pid2;
//for(i=0;i<10;i++){
if(pid1=fork()==-1)
{
printf("Create Process fail!\n");
//exit(EXIT_FAILURE);
}
if(pid1==0)
{printf("Child1:my pid is%d\n",getpid());
//exit(1);
//break;
}
else
{
if(pid2=fork()==-1);
printf("Create Process fail!\n");
//exit(EXIT_FAILURE);
if(pid2==0)
printf("Child2:my id is%d\n",getpid());
//sleep(5);
else//执行父进程代码
printf("Parent:my id is%d\n",getpid());
}
//return;
}
以上是程序,这是在Linux系统下的编程,输出的结果是
Child1:my id is 7074
Child1:my id is 7073
我想要的结果是
Child1:my id is ****
child2:my id is ****
parent:my id is ****
为什么会出现上面的情况?还有怎么改才能得到预期的结果啊?谢谢!! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询