gpt4 book ai didi

c - C 程序中的 fork 与 vfork 功能

转载 作者:行者123 更新时间:2023-12-02 09:32:17 24 4
gpt4 key购买 nike

我正在做一些C自学练习,遇到了以下问题:

a 部分:

int main(int argc, char **argv) {                                         

int a = 5, b = 8;
int v;

v = fork();
if(v == 0) {
// 10
a = a + 5;
// 10
b = b + 2;
exit(0);
}
// Parent code
wait(NULL);
printf("Value of v is %d.\n", v); // line a
printf("Sum is %d.\n", a + b); // line b
exit(0);

}

B 部分:

int main(int argc, char **argv) {                                         

int a = 5, b = 8;
int v;

v = vfork();
if(v == 0) {
// a = 10
a = a + 5;
// b = 6
b = b - 2;
exit(0);
}
// Parent code
wait(NULL);
printf("Value of v is %d.\n", v); // line a
printf("Sum is %d.\n", a + b); // line b
exit(0);

}

我们必须比较a行b行的输出。

a部分的输出是:

Value of v is 79525.
Sum is 13.

b部分的输出是:

Value of v is 79517.
Sum is 16.

在a部分中,总和是ab初始声明的总和,而在b部分中,总和包括子级内的总和流程。

我的问题是 - 为什么会发生这种情况?

根据这个post :

The basic difference between the two is that when a new process is created with vfork(), the parent process is temporarily suspended, and the child process might borrow the parent's address space. This strange state of affairs continues until the child process either exits, or calls execve(), at which point the parent process continues.

父进程的定义暂时中止对我来说没有多大意义。这是否意味着对于1b,程序会等到子进程完成运行(因此为什么子进程变量会被求和)然后才运行父进程?

问题陈述还假设“内核维护的父进程的进程ID是2500,并且新进程是操作系统在创建子进程之前创建的。”

根据这个定义,两个程序的 v 值是多少?

最佳答案

the parent process is temporarily suspended

基本上,在子进程调用 _exitexec 函数之一之前,父进程不会运行。在您的示例中,这意味着子级将运行,因此在父级运行并进行打印之前执行求和。

至于:

My question is - why is this happening?

首先,您的 b 部分具有未定义的行为,因为您违反了 vfork 语义。程序的未定义行为意味着程序不会以可预测的方式运行。请参阅this SO post on undefined behavior了解更多详细信息(它包括一些 C++,但大多数想法是相同的)。来自 POSIX specs on vfork :

The vfork() function has the same effect as fork(2), except that the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit(2) or one of the exec(3) family of functions.

所以你的 b 部分真的可以做任何事情。但是,您可能会看到 b 部分的输出有些一致。这是因为当您使用 vfork 时,您并没有创建新的地址空间。相反,子进程基本上“借用”父进程的地址空间,通常是为了调用 exec 函数之一并创建新的程序镜像。相反,在 b 部分中,您使用的是父地址空间。基本上,在子进程调用 exit (这也是无效的,因为它应该调用 _exit)之后,a 最有可能等于 10 和 b 在父级中很可能等于 6。因此,总和为 16,如 b 部分所示。我说最有可能是因为如前所述,该程序具有未定义的行为。

对于使用 fork 的 a 部分,子级获得自己的地址空间,并且其修改在父级中看不到,因此打印的值为 13 (5 + 8)。

最后,关于 v 的值,这似乎只是问题所陈述的内容,以使它显示的输出有意义。 v 的值可以是 vforkfork 返回的任何有效值,并且不必限制为 2500。

关于c - C 程序中的 fork 与 vfork 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31775949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com