fork()调用的输出

编程入门 行业动态 更新时间:2024-10-23 12:34:00
本文介绍了fork()调用的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下fork()调用的输出是什么?

What would be the output of following fork() calls ?

func(){ fork(); fork(); fork() && fork() || fork(); fork(); print("Saikacollection\n"); }

有人是我的新手,可以帮助我获得此代码的答案以及一些解释吗?我在SO的fork()上发现了几个问题,但想不出多少.

Can anyone help me in getting the answer to this code as well as some explanations as I am new to OS ? I have found several questions on fork() on SO, but couldn't figure out much.

推荐答案

Saikacollection将被打印40次,作为代码输出.可以解释如下:-

Saikacollection will be printed 40 times as output to the code. This can be explained as follows :-

要了解输出,我们需要了解以下内容:-

To understand the output, we need to know following :-

  • 成功执行fork()调用后,将创建新的子代.创建子进程的过程称为parent process.
  • Fork()调用将子级的pid(进程标识符)返回给父级
  • Fork()返回0到子进程.
  • On successful execution of a fork() call, new child is created. The process creating the child is called parent process.
  • Fork() call returns pid (process identifier) of the child to parent
  • Fork() returns 0 to the child process.

考虑显示的图像:-

公约:该过程的所有父母(主叫方)都写到left并标有star.

Convention : All the parents(callers) of the process are written to left and marked with a star.

fork()

  • 在开始时,我们只有1个进程,因此fork()调用将创建一个子进程.考虑到树的根为1级,我们可以在2级看到,我们有两个进程,parent(left)和child(right).

    • At the beginning, we have just 1 process, so a fork() call creates a child. Considering the root of the tree as level 1, we can see at level 2 , we have two processes, parent(left) and child(right) .

      fork()

      fork()再次进一步创建4这样的进程,标记为1、2、3、4.由于这四个进程都将进一步经历相似的代码结构,因此我们可以说进程总数将是单个进程产生的4倍次数

      fork() again further creates 4 such processes, marked as 1, 2, 3, 4. Since all of the four processes will go through similar code structure further, we can say the total number of processes will be 4 times a single process produces

      fork()&&fork()||fork()

      理解此语句涉及认识到以下事实:在C中,&& operator has more precedence than ||

      Understanding this statement involves, realizing the fact that in C, && operator has more precedence than ||

      如果我们对所有三个节点都做同样的事情,我们本来可以得到5*4 = 20进程.

      Had we done the same for all the three nodes, we could have got 5*4 = 20 processes.

      fork()

      最终fork()仅doubles该步骤中可用的进程数.

      Final fork() just doubles the number of process available at that step.

更多推荐

fork()调用的输出

本文发布于:2023-10-09 06:18:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474934.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:fork

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!