if statement with fork() - Linux

By Jacob
at 2017-07-23T16:42
at 2017-07-23T16:42
Table of Contents
最近小弟在讀OS, 用C++在ubuntu上做了一些練習, 發現一個很奇怪的地方
code1跟code2差別在else statement, 其中1代表一個process(包含test2自己)
但code1只是多了一個else卻造成output有8個1(比code2多了2個), stack overflow
上面也沒有類似例題, 想了滿久也不確定是什麼機制造成else那邊會再多生出2個
process, 先謝謝大家
[Code1_output: 11111111]
void test2(){
int i;
fork();
if(fork()>0){
fork();
}
else if(fork()==0){}
cout<<" 1 "; //輸出一個1代表1個process
}
[Code2_output: 111111]
void test2(){
int i;
fork();
if(fork()>0){
fork();
}
// else if(fork()==0){}
cout<<" 1 "; //輸出一個1代表1個process
}
--
Tags:
Linux
All Comments

By Ula
at 2017-07-27T02:10
at 2017-07-27T02:10

By Eden
at 2017-07-31T23:19
at 2017-07-31T23:19

By Candice
at 2017-08-02T07:00
at 2017-08-02T07:00

By Ida
at 2017-08-05T22:18
at 2017-08-05T22:18

By Kyle
at 2017-08-07T06:13
at 2017-08-07T06:13

By Odelette
at 2017-08-10T15:28
at 2017-08-10T15:28
Related Posts
Linux指令區分大小寫的好處?

By Caroline
at 2017-07-21T16:36
at 2017-07-21T16:36
工作站內網架設

By Valerie
at 2017-07-21T15:52
at 2017-07-21T15:52
@folder 的@是什麼意思

By Connor
at 2017-07-21T11:23
at 2017-07-21T11:23
自製LiveCD 簡要過程

By Ingrid
at 2017-07-21T09:00
at 2017-07-21T09:00
Asus x550vx ubuntu+win10雙系統安裝失敗

By Noah
at 2017-07-20T21:56
at 2017-07-20T21:56