if(fork()) 是否创建子进程? [英] Does if(fork()) create a child process?

查看:23
本文介绍了if(fork()) 是否创建子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在复习操作系统考试,目前正在尝试理解这个过程问题:

int main(){诠释v=0;如果(叉子()){v++;如果(!叉()){叉();v--;}}}

所以问题要求

<块引用>

  1. 绘制一棵树,反映上述程序运行时创建的进程的父子层次结构.

  2. 创建了多少个整数变量 v 的单独副本?讨论树中每个进程在程序结束时 v 的值是多少.

我遇到的主要问题是

if(fork())

代码中的一行.通过查看其他堆栈溢出,我意识到

if(fork()) = if(fork() != 0)

但我仍然难以理解第一个 if 语句是否创建了子进程?因为这是第二个 if 语句的唯一方法:

(!fork())

可以执行吗?

这是我目前对这个问题的理解的程度.

希望图表清晰易读!我真的很感激任何提示或指示.

解决方案

注意 if (fork())/*etc*/ 完全

pid_t newtmp = fork();if (newtmp != 0)/*等*/

其中 newtmp 是您的程序中未出现的新(新)变量名称(您可以使用 x1x2 等.... 前提是它在您的程序中没有出现),并且 pid_t 是某种整数类型(可能是 int).

一旦你用 explicitunique 名称重写你的代码给 fork 的结果,你会更好地理解它.

顺便说一句,代码味道很差.当你使用 fork 时,你需要处理 三种 情况:

  • fork 失败(例如,因为您的系统没有足够的内存,或者因为您超出了某些限制)并给出 -1

  • fork 在孩子中成功,所以给出 0

  • fork 在父进程中成功,所以给出了子进程的 pid.

但是,当您编写 if (fork()) 代码时,您会忘记 - 或处理不正确 - 第一种情况(失败).它很少会发生.

仔细阅读(并多次)fork(2) 手册页.请注意,fork 很难理解.

关于限制,请注意 setrlimit(2) (和 ulimit bash 内置).

I'm revising for an operating systems exam and currently trying to understand this processes question:

int main() 
{
    int v=0;
    
    if(fork()) 
    {
        v++;
        
        if(!fork())
        {
            fork();
            v--; 
        }
    } 
}

So the question asks to

  1. Draw a tree reflecting the parent-child hierarchy of processes created when the program above is run.

  2. How many separate copies of the integer variable v are created? Discuss what is the value of v at the end of the program for each of the processes in the tree.

The main thing I'm having an issue with is the

if(fork())

line in the code. From looking at other stack overflows I realised that

if(fork()) = if(fork() != 0)

but I'm still having difficulty understanding whether the first if statement creates a child process? Because thats the only way that the second if statement:

(!fork())

can be executed?

This is how far I've got with my current understanding of the question. screenshot of attempt

Hopefully the diagram is legible! I'd be really grateful for any hints or pointers with this.

解决方案

Notice that if (fork()) /*etc*/ is exactly the same as

pid_t newtmp = fork();
if (newtmp != 0) /*etc*/

where newtmp is a fresh (new) variable name not occurring in your program (you could use x1, x2 etc.... provided it has no occurrence in your program), and pid_t is some integral type (probably int).

Once you rewrote your code with explicit and unique names given to result of fork you'll understand it better.

BTW, the code is poor taste. When you use fork you need to handle three cases:

  • the fork failed (e.g. because your system has not enough memory, or because you exceeded some limits) and gives -1

  • the fork succeeded in the child so gives 0

  • the fork succeeded in the parent, so gives the pid of the child.

But when you code if (fork()) you are forgetting -or handling incorrectly- the first case (failure). It can rarely happen.

Read carefully (and several times) the fork(2) man page. Notice that fork is difficult to understand.

Regarding limits, be aware of setrlimit(2) (and the ulimit bash builtin).

这篇关于if(fork()) 是否创建子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆