(1)在父进程中,fork返回新创建子进程的进程ID;
(2)在子进程中,fork返回0;
(3)如果出现错误,fork返回一个-1。
打印父子进程fork函数的返回值代码:
此段代码不够严谨哈!!!!
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid_t pid2;
pid_t retpid;
pid = getpid();
printf("before fork pid:%d\n",pid);
retpid = fork();
pid2 = getpid();
printf("after fork pid:%d\n",pid2);
if(pid == pid2) //父进程
{
printf("this is father print\n");
printf("father retpid = %d\n",retpid);//打印fork返回值
printf("father pid :%d\n",getpid());
}
else //子进程
{
printf("this is child print\n");
printf("child retpid = %d\n",retpid);//打印fork返回值
printf("child pid :%d\n",getpid());
}
return 0;
}
运行结果: