作者Fight5566 (單戈示申)
看板Grad-ProbAsk
標題Re: [理工] [OS] fork()
時間Tue Mar 13 21:09:35 2012
首先
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
1F:推 wsx02:謝謝 03/13 21:21