【Linux】fork()函数详解 (深入浅出 实例讲解)

【Linux】fork()函数详解 (深入浅出 实例讲解)文章目录 toc 一 fork 入门知识二 fork 进阶知识三 fork 高阶知识参考资料一 fork 入门知识一个进程 包括代码 数据和分配给进程的资源

大家好,欢迎来到IT知识分享网。

【Linux】fork()函数详解 (深入浅出 实例讲解)


  相关推荐文章:

  【Linux】fork与vfork函数的联系与区别(浅析)

  【Linux】fork()函数详解 (深入浅出 实例讲解)



一、fork入门知识

#include <unistd.h> #include <stdio.h>  int main () { 
       pid_t fpid; //fpid表示fork函数返回的值 int count=0; fpid=fork(); if (fpid < 0) printf("error in fork!"); else if (fpid == 0) { 
       printf("i am the child process, my process id is %d/n",getpid()); printf("我是爹的儿子/n");//对某些人来说中文看着更直白。 count++; } else { 
       printf("i am the parent process, my process id is %d/n",getpid()); printf("我是孩子他爹/n"); count++; } printf("统计结果是: %d/n",count); return 0; } 

        运行结果是:

i am the child process, my process id is 5574 我是爹的儿子 统计结果是: 1 i am the parent process, my process id is 5573 我是孩子他爹 统计结果是: 1 

【Linux】fork()函数详解 (深入浅出 实例讲解)


二、fork进阶知识

        先看一份代码:

#include <unistd.h> #include <stdio.h> int main(void) { 
        int i=0; printf("i son/pa ppid pid fpid/n"); //ppid指当前进程的父进程pid //pid指当前进程的pid, //fpid指fork返回给当前进程的值 for(i=0;i<2;i++) { 
        pid_t fpid=fork(); if(fpid==0) printf("%d child %4d %4d %4d/n",i,getppid(),getpid(),fpid); else printf("%d parent %4d %4d %4d/n",i,getppid(),getpid(),fpid); } return 0; } 

        运行结果是:

 i son/pa ppid pid fpid 0 parent 2043 3224 3225 0 child 3224 3225 0 1 parent 2043 3224 3226 1 parent 3224 3225 3227 1 child 1 3227 0 1 child 1 3226 0 
 for(i=0;i<2;i++) { 
        pid_t fpid=fork();//执行完毕,i=0,fpid=3225 if(fpid==0) printf("%d child %4d %4d %4d/n",i,getppid(),getpid(),fpid); else printf("%d parent %4d %4d %4d/n",i,getppid(),getpid(),fpid); } return 0; 

        p3225(子进程)的变量为i=0,fpid=0(fork函数在子进程中返回0),代码内容为:

 for(i=0;i<2;i++) { 
        pid_t fpid=fork();//执行完毕,i=0,fpid=0 if(fpid==0) printf("%d child %4d %4d %4d/n",i,getppid(),getpid(),fpid); else printf("%d parent %4d %4d %4d/n",i,getppid(),getpid(),fpid); } return 0; 

        所以打印出结果:

0 parent 2043 3224 3225 0 child 3224 3225 0 

        第二步:假设父进程p3224先执行,当进入下一个循环时,i=1,接着执行fork,系统中又新增一个进程p3226,对于此时的父进程,p2043->p3224(当前进程)->p3226(被创建的子进程)。
        对于子进程p3225,执行完第一次循环后,i=1,接着执行fork,系统中新增一个进程p3227,对于此进程,p3224->p3225(当前进程)->p3227(被创建的子进程)。从输出可以看到p3225原来是p3224的子进程,现在变成p3227的父进程。父子是相对的,这个大家应该容易理解。只要当前进程执行了fork,该进程就变成了父进程了,就打印出了parent。
        所以打印出结果是:

1 parent 2043 3224 3226 1 parent 3224 3225 3227 

        第三步:第二步创建了两个进程p3226,p3227,这两个进程执行完printf函数后就结束了,因为这两个进程无法进入第三次循环,无法fork,该执行return 0;了,其他进程也是如此。
        以下是p3226,p3227打印出的结果:

1 child 1 3227 0 1 child 1 3226 0 

【Linux】fork()函数详解 (深入浅出 实例讲解)
        这个程序最终产生了3个子进程,执行过6次printf()函数。
        我们再来看一份代码:

#include <unistd.h> #include <stdio.h> int main(void) { 
        int i=0; for(i=0;i<3;i++) { 
        pid_t fpid=fork(); if(fpid==0) printf("son/n"); else printf("father/n"); } return 0; } 

        它的执行结果是:

father son father father father father son son father son son son father son 
#include <unistd.h> #include <stdio.h> int main() { 
        pid_t fpid;//fpid表示fork函数返回的值 //printf("fork!"); printf("fork!/n"); fpid = fork(); if (fpid < 0) printf("error in fork!"); else if (fpid == 0) printf("I am the child process, my process id is %d/n", getpid()); else printf("I am the parent process, my process id is %d/n", getpid()); return 0; } 

        执行结果如下:

fork! I am the parent process, my process id is 3361 I am the child process, my process id is 3362 
fork!I am the parent process, my process id is 3298 fork!I am the child process, my process id is 3299 
#include <stdio.h> #include <unistd.h> int main(int argc, char* argv[]) { 
        fork(); fork() && fork() || fork(); fork(); return 0; } 
#include <stdio.h> int main(int argc, char* argv[]) { 
        fork(); fork() && fork() || fork(); fork(); printf("+/n"); } 

【Linux】fork()函数详解 (深入浅出 实例讲解)        加上前面的fork和最后的fork,总共4*5=20个进程,除去main主进程,就是19个进程了。


三、fork高阶知识

        这一块我主要就fork函数讲一下操作系统进程的创建、死亡和调度等。因为时间和精力限制,我先写到这里,下次找个时间我争取把剩下的内容补齐。


转载:【Linux】fork()函数详解 (深入浅出 实例讲解)


四、参考资料

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/117372.html

(0)
上一篇 2025-11-21 16:33
下一篇 2025-11-21 17:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信