fork

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.\n");
        //输出进程ID和父进程ID
        printf("pid: %d\tppid:%d\n",getpid(),getppid());
        printf("I will sleep five seconds.\n");
        //睡眠5s,保证父进程先退出
        sleep(5);
        printf("pid: %d\tppid:%d\n",getpid(),getppid());
        printf("child process is exited.\n");
    }
    //父进程
    else
    {
        printf("I am father process.\n");
        //父进程睡眠1s,保证子进程输出进程id
        sleep(1);
        printf("father process is  exited.\n");
    }
    return 0;
}

https://www.cnblogs.com/anker/p/3271773.html

孤儿进程和僵尸进程

COW机制

https://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html
https://en.wikipedia.org/wiki/Copy-on-write

posted @ 2021-11-12 11:21  R=(1-sinθ)  阅读(42)  评论(0编辑  收藏  举报