输出结果
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>int main(int argc,char**argv)
{
pid_t pid;printf("Parent process PID [%d] start running....\n",getpid());
pid =fork();
if(pid < 0)
{
printf("fork() create child process failure :%s\n",strerror(errno));
return -1;
}else if(pid==0)
{
printf("Child process PID is [%d],and it is running,my parent ID is [%d]\n",getpid(),getppid());
return 0;
}else
{printf("Parent process PID[%d] continue runnning,and child process PID is [%d]\n",getpid(),pid);
return 0;
}
}
fork函数的返回值有三种情况
在父进程中,fork返回新创建子进程的进程ID;
在子进程中,fork返回0;
如果出现错误,fork返回一个负值;