2016-08-04
更多 →
2016-08-02
更多 →
2016-07-09
更多 →
2016-07-07
更多 →
2016-07-07
更多 →
2016-07-06
更多 →
2016-07-04
更多 →
2016-06-30
更多 →
2016-06-06

测试 fork 的时候,出现以下的一个情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <unistd.h>

int main(int argc, char const *argv[]){
printf("%s,parent:%d,current:%d\n", "start", getppid(), getpid());
pid_t fpid;
fpid = fork();
if (fpid < 0){ //error
printf("%s:%d\n", "error", fpid);
}else if (fpid == 0){ //child
printf("%s,parent:%d,current:%d, fpid:%d\n", "child", getppid(), getpid(), fpid);
}else{ //host
printf("%s,parent:%d,current:%d, fpid:%d\n", "host", getppid(), getpid(), fpid);
}
return 0;
}

按理来说,结果应该是

start,parent:15111,current:19431
host,parent:15111,current:19431, fpid:19432
child,parent:19431,current:19432, fpid:0

但是在 sublime 使用 CTRL + SHIFT + B 执行的时候,结果却是:

start,parent:15111,current:19431
host,parent:15111,current:19431, fpid:19432
start,parent:15111,current:19431
child,parent:19431,current:19432, fpid:0
更多 →
2016-05-30
更多 →