linux系统应用编程学习–初识进程
1,学习笔记
2.练习
#include <unistd.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
pid_t pid;
printf("PID of this process=%d,before fork()\n",getpid());
pid = fork();
printf("PID of this process=%d,after fork()\n",getpid());
printf("the value of fork() return =%d\n",pid);
if(pid ==0 ){
printf("this is child process,pid=%d\n",getpid());
}
else if(pid >0){
printf("this is parent process,pid =%d\n",getpid());
}
else{
printf("fork fail\n");
}
printf("is end\n");
}
编译执行
gavin@vm-ubuntu:~/202108/3-process$ gcc ./process.c -o ./process
gavin@vm-ubuntu:~/202108/3-process$ ./process
PID of this process=3924,before fork()
PID of this process=3924,after fork()
the value of fork() return =3925
this is parent process,pid =3924
is end
gavin@vm-ubuntu:~/202108/3-process$ PID of this process=3925,after fork()
the value of fork() return =0
this is child process,pid=3925
is end
3,总结
a,在fork()之后,父进程和子进程各自执行了一遍。
b, fork()的返回值可用于识别是父进程还是子进程。