Re: [理工] [OS] fork()
首先 first 先print
fork出一個process如下
╭────╮
│ main() │
╰─┬──╯
╭┴─╮
│ 子 │
╰──╯ 此時會有兩個process在裡面
所以second會print兩次
因為parent process裡面有wait的動作代表他會等待child執行完畢
因此會print fourth 和 fifth
再來換父process print third 和 fifth
Done
有錯請指證!
※ 引述《wsx02 ()》之銘言:
: After executing the program shown below, what is the output on the screen?
: main()
: {
: int status, pid;
: printf("first\n");
: pid = fork();
: printf("second\n");
: if(pid != 0)
: {
: wait(&status);
: printf("third\n");
: }
: else
: {
: printf("fourth\n");
: }
: printf("fifth\n");
: }
: 答案是
: first
: second
: second
: fourth
: fifth
: thrid
: fifth
: 請問是怎麼跑出來的?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.169.104.4
推
03/13 21:21, , 1F
03/13 21:21, 1F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):