**
使用fork函数创建一个进程
3.创建进程函数fork的使用补充
**
pid_t fork(void);
fork函数调用成功,返回两次;
返回值为非负数,代表当前进程为父进程;
返回值为0, 代表当前进程为子进程;
调用失败,返回值为 -1;
#include <stdio.h>
#include <sys/types.h>
#include <usistd.h>
int main()
{
pid_t pid;
pid_t pid2;
pid_t retpid;
pid = getpid();
peintf("before fork: pid = =%d\n",pid);
retpid = fork(); //父子进程开始运行
pid2 = getpid();
printf("after fork: pid = %d\n",pid2);
if(pid == pid2){
printf("this is fu retpid = %d\n",retpid); //这个是父进程,进程号retpid为非负数,并且等于子进程的进程号
}else{
printf("this is zi retpid = %d\n",retpid); //这个是子进程,retpid等于0
}
return 0;
}