作者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