当前位置: 技术问答>linux和unix
关于fork的问题
来源: 互联网 发布时间:2017-01-04
本文导语: #include #include #include #include void main() { int p_id; p_id = fork(); if(p_id == 0) { printf("this is childn p_id = %dn PID = %dn PPID = %dn",p_id,getpid(),getppid()); } if(p_id == -1) { printf("error"); exit(-1); } else { printf("this is...
#include
#include
#include
#include
void main()
{
int p_id;
p_id = fork();
if(p_id == 0)
{
printf("this is childn p_id = %dn PID = %dn PPID = %dn",p_id,getpid(),getppid());
}
if(p_id == -1)
{
printf("error");
exit(-1);
}
else
{
printf("this is fathern p_id = %dn PID = %dn PPID = %dn",p_id,getpid(),getppid());
}
}
我改编了书上一个小程序 ,编译没有错 ,与性结果是:
this is father
p_id = 4630
PID = 4629
PPID = 3578
this is child
p_id = 0
PID = 4630
PPID = 4629
this is father
p_id = 0
PID = 4630
PPID = 4629
我不明白 为什么会有两个父进程,而且第二个父进程进程号于子进程相同
哪个高手帮我讲解一下。。?还是我程序有问题
#include
#include
#include
void main()
{
int p_id;
p_id = fork();
if(p_id == 0)
{
printf("this is childn p_id = %dn PID = %dn PPID = %dn",p_id,getpid(),getppid());
}
if(p_id == -1)
{
printf("error");
exit(-1);
}
else
{
printf("this is fathern p_id = %dn PID = %dn PPID = %dn",p_id,getpid(),getppid());
}
}
我改编了书上一个小程序 ,编译没有错 ,与性结果是:
this is father
p_id = 4630
PID = 4629
PPID = 3578
this is child
p_id = 0
PID = 4630
PPID = 4629
this is father
p_id = 0
PID = 4630
PPID = 4629
我不明白 为什么会有两个父进程,而且第二个父进程进程号于子进程相同
哪个高手帮我讲解一下。。?还是我程序有问题
|
if(p_id == -1)
改为
else if(p_id == -1)
改为
else if(p_id == -1)
|
子进程走了两次printf