程序一:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
//子进程与父进程间的关系
int main()
{
pid_t pid;
pid = vfork();
if(pid < 0)
{
printf("error in fork!");
}
else if(pid == 0)
{
printf("I am the child process,ID is %d/t%d\n",pid,getpid());
}
else
printf("I am the parent process,ID is %d/t%d\n",pid,getpid());
}
I am the child process,ID is 0/t4611
I am the parent process,ID is 4611/t4610
段错误 (核心已转储)
程序二:
#include <unistd.h>
#include <stdio.h>
//子进程与父进程间的关系
int main()
{
int i;
pid_t fpid;
printf(" i son/par ppid pid fpid\n");
//fpid是fork返回值
for(i = 0;i < 2;i++)
{
fpid = fork();
if(fpid == 0)
{
printf("%d child %d %d %d\n",i,getppid(),getpid(),fpid);
}
else if(fpid > 0)
{
printf("%d parent %d %d %d\n",i,getppid(),getpid(),fpid);
}
}
}
i son/par ppid pid fpid
0 parent 3491 4991 4992
0 child 4991 4992 0
1 parent 3491 4991 4993
1 parent 4991 4992 4994
1 child 4991 4993 0
1 child 4992 4994 0