这是我的C代码,用于从父进程创建两个子进程。 这会成功创建吗?我的电流输出为:此代码是否会导致父进程成功创建两个子进程?
您在id为29509
你在子进程1,你的父母的ID是29509
您在id为29511
你是在父进程的父进程子进程2和你的父母身份证是29509
为什么在我的输出的第三行父id是不同的?
#include<stdio.h>
#include<unistd.h>
int main(){
pid_t child1,child2;
int c,d,e;
child1=fork();
if(child1==0){
c=getppid();
printf("you are in child process 1 and your parent id is %d\n",c);
}
else{
child2=fork();
e=getpid();
printf("You are in parent process whose id is %d\n",e);
}
if(child2==0){
d=getppid();
printf("you are in child process 2 and your parent id is %d\n",d);
}
}
输出是
You are in parent process whose id is 29509
you are in child process 1 and your parent id is 29509
You are in parent process whose id is 29511
you are in child process 2 and your parent id is 29509