• Linux中fork()函数的使用


    函数功能

    fork()函数的详细介绍可以用man命令来查看,下面截取一部分重要的介绍。

    #include       
    #include 
    
    pid_t fork(void);
    
    • 1
    • 2
    • 3
    • 4

    fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process.

    The child process and the parent process run in separate memory spaces. At the time of fork() both memory spaces have the same content. Memory writes, file mappings (mmap(2)), and unmap‐pings (munmap(2)) performed by one of the processes do not affect the other.

    RETURN VALUE
    On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

    NOTES
    Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to duplicate the parent’s page tables, and to create a unique task structure for the child.

    #include 
    #include 
    #include 
    #include 
    
    int main(){
        pid_t pid;
        pid = fork();
        if(pid == 0){  /* child */
            pid = getpid();
            printf("child's pid:%d\r\n",pid);
            exit(0);
        }
        else{ /* parent */
            pid = getpid();
            printf("parent's pid:%d\r\n",pid);
            wait(NULL);                /* Wait for child */
            exit(0);
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    上面的exit()函数是退出进程,exit(0)表示正常退出。创建的子进程执行完之后会自动退出,因此if对应的else可以不要。
    wait()函数一般都要和fork()函数配套使用,作用等待子进程退出。

    一次调用,两次返回

    fork()函数一次调用,两次返回,通过返回值来判断返回的是那个进程,因此不同的执行代码应该放到不同的进程中,如果不做区分,fork()后面的代码会被执行两次,例如下面的例子。

    #include 
    #include 
    #include 
    #include 
    
    int main(){
        fork();
        printf("Hello World!\r\n");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    上面的代码只有一次打印,但是调用fork()的原因,会打印两次。
    下面的代码会打印四次:

    #include 
    #include 
    #include 
    #include 
    
    int main(){
        fork();
        fork();
        printf("Hello World!\r\n");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    明确创建关系

    由于fork()函数会两次返回,因此在创建新进程的时候一定明确哪个是父进程,子进程由哪个进程创建,特别是存在多个进程的时候。例如下面的代码中两个子进程都是由同一个父进程创建的:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(){
        pid_t pid;
        pid = fork();
        if(pid == 0){/*child*/
            /*code*/
            printf("child 1 pid:%d\r\n",getpid());
            exit(0);
        }
        pid = fork();
        if(pid == 0){/*child*/
            /*code*/
            printf("child 2 pid:%d\r\n",getpid());
            exit(0);
        }
        while(pid = wait(NULL),pid != -1){} /*wait for child*/
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    用ChatGPT做一个Chrome扩展 | 京东云技术团队
    mysql技术文档--阿里巴巴java准则《Mysql数据库建表规约》--结合阿丹理解尝试解读--国庆开卷
    Tensoflow目标检测实战(4)训练模型转换至tflite并部署
    nginx解析漏洞复现
    【PA交易】BackTrader(一): 如何使用实时tick数据和蜡烛图
    架构演进技巧
    【计组】IO系统
    一文总结 Google I/O 2023
    用户运营都做些什么?你需要知道这些
    【数据结构与算法】| 链表练习
  • 原文地址:https://blog.csdn.net/qq_70244454/article/details/128106014