c - 用于循环条件的 fork() 会产生竞争条件吗?

标签 c linux operating-system fork

好吧,这是我们今天的操作系统考试的练习题

给定这个 C 语言的程序

#include <unistd.h>
#include <stdlib.h>

int  main(){
    int i;
    for(i=2;i>=1&&!fork();i--)
            printf("%d\n",i);

exit(EXIT_SUCCESS);
}

执行它给出这个输出:

2
1

我发现不明确的是索引“i”是如何由程序管理的 进程执行的正确顺序是什么 (如果有一个顺序或者它是由调度程序建立的随机顺序?)

为什么只打印 2 和 1 - 我的假设是:

(father executes the "for" only for i=2, prints 2 then exits ?)
(the first child starts from i=1 forks a child prints 1 exits ?)

此时我的问题是:

是否有没有进入 for 的第二个 child fork? 并且 2 由父亲打印,1 由第一个 child 打印?

还有最后一件事:

您将如何重写这个 fork-conditioned-for 以使其更具可读性(例如 if 语句)

最佳答案

当你 fork 时,就像所有的程序都被复制粘贴了,两个相同的进程从那个点开始(尽管父亲得到 child 的 PId 作为 fork< 的返回值 child 得到零)

i 发生的事情是它在子进程中的值与派生时在父进程中的值完全相同。

因此,当 i 为 2 时:

for parent: i>=1 && !fork()  <-- fails
for child1: i>=1 && !fork()  <-- succeeds

因此,2 由原始程序的子程序打印。

现在 parent 已经退出,child1 正在运行。它执行 i-- 现在 i 是 1:

for child1:   i>=1 && !fork()  <-- fails
for child1.1: i>=1 && !fork()  <-- succeeds

再次,child1 退出,它的 child child1.1 进入 for 并打印 1。然后它执行 i-- 并且 i 变为 0,失败 i>=1 并且因为 short-circuit evaluation , fork 没有被执行。这个child1.1也退出了。

为了回答你的最后一个问题,fork 通常是这样写的:

pid_t pid = fork();
if (pid)
{
    // in parent
}
else
{
    // in child
}

现在你只需要一个for循环:

#include <unistd.h>
#include <stdlib.h>

int  main(){
    int i;
    for(i=2;i>=1;i--)
        if (fork())
            break;              /* parent wants to break */
        else
            printf("%d\n",i);   /* child prints something */

    exit(EXIT_SUCCESS);
}

关于c - 用于循环条件的 fork() 会产生竞争条件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193512/

相关文章:

c - sigwait() 反复被 SIGUSR1 解锁

c - 初学者类型转换

php - Linux Box 使用 PHP 将文件写入 Windows Server Web Share

c - 复制文件描述符并独立搜索它们

C 使用预定义消息将标准输出重定向到文件

php - 文件和文件夹属性 - 编程 API

使用 #if ... #endif 检查版本号

c - 假设文本文件有多个空格,如何计算文档中的字数

c - 学校实验室 Shellcode BufferOverflow 的段错误

java - 忘记关闭 ServerSocket