进程的创建 fork(父·子进程)
fork函数调用成功,返回两次值,返回值为0,代表为子进程,返回值为非负数,代表为父进程
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
printf(“father pid is %d\n”,getpid());
pid = fork(); //fork后会拷贝代码段,父子进程都会跑一遍后面的代码段(看输出结果)
if(pid>0){
printf("this is father pid:%d\n",getpid());
}else if(pid == 0){
printf("this id child pid:%d\n",getpid());
}
return 0;
}
运行结果为:fork后会拷贝代码段,父子进程都会跑一遍后面的代码段