测试 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){ printf("%s:%d\n", "error", fpid); }else if (fpid == 0){ printf("%s,parent:%d,current:%d, fpid:%d\n", "child", getppid(), getpid(), fpid); }else{ 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