fork执行
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
int main()
{
pid_t pid;
//创建一个进程
pid = fork(); //这里执行过后,内存中就创建了两个完全相同的进程,其实就是copy了一份;然后每个进程都从这里往下执行
//创建失败
if (pid < 0)
{
perror("fork error:");
exit(1);
}
//子进程
if (pid == 0)
{
printf("I am the child process.
");
//输出进程ID和父进程ID
printf("pid: %d ppid:%d
",getpid(),getppid());
printf("I will sleep five seconds.
");
//睡眠5s,保证父进程先退出
sleep(5);
printf("pid: %d ppid:%d
",getpid(),getppid());
printf("child process is exited.
");
}
//父进程
else
{
printf("I am father process.
");
//父进程睡眠1s,保证子进程输出进程id
sleep(1);
printf("father process is exited.
");
}
return 0;
}
孤儿进程和僵尸进程
COW机制
https://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html
https://en.wikipedia.org/wiki/Copy-on-write