Re: [理工] [OS] fork()

看板Grad-ProbAsk作者 (單戈示申)時間14年前 (2012/03/13 21:09), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
首先 first 先print fork出一個process如下 ╭────╮ │ main() │ ╰─┬──╯ ╭┴─╮ │ 子 │ ╰──╯ 此時會有兩個process在裡面 所以second會print兩次 因為parent process裡面有wait的動作代表他會等待child執行完畢 因此會print fourthfifth 再來換父process print thirdfifth 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
文章代碼(AID): #1FNqSIoo (Grad-ProbAsk)
文章代碼(AID): #1FNqSIoo (Grad-ProbAsk)