gpt4 book ai didi

c - fork() 和 waitpid 可能的输出

转载 作者:太空宇宙 更新时间:2023-11-04 04:46:31 24 4
gpt4 key购买 nike

我的课本给出了如下主要例程:

int main()
{
if(fork() == 0)
{
printf("a");
}
else
{
printf("b");
waitpid(-1, NULL, 0);
}
printf("c");
exit(0);
}

它询问可能的输出是什么,我找到了 3 个:

abcc:操作系统选择子进程先执行,并打印“a”。然后,操作系统暂停子进程并恢复父进程,打印“b”。然后parent必须等到child完成,child打印“c”,最后parent打印“c”。

bacc:操作系统选择先运行的父进程,并打印“b”。然后 parent 必须等待 child 完成并打印“ac”。然后父打印“c”。

acbc:操作系统选择子进程先运行直到完成,打印“ac”。然后父级运行完成,打印“bc”。

不过,课本上多了一个答案,bcac。我不明白这是怎么可能的,因为如果先打印 b,那么 parent 必须等待 child 继续,然后打印“ac”,然后 parent 会打印“c”,给出我已经列出的 bacc .有没有我遗漏的东西,或者说只有 3 种可能的输出是否正确?

最佳答案

不要总是相信教科书...

来自errata :

p. 772, Solution to Practice Problem 8.3. The sequence bcac is not possible. Strike the second to last sentence. The last sentence should be “There are three possible sequences: acbc, abcc, and bacc.” Please see Web Aside ECF:GRAPHS on the Web Aside page for an example of the process graph.

关于c - fork() 和 waitpid 可能的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20335081/

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