考慮代碼:代碼在哪裏執行在子進程中啓動?
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
/* main --- do the work */
int main(int argc, char **argv)
{
pid_t child;
if ((child = fork()) < 0) {
fprintf(stderr, "%s: fork of child failed: %s\n",
argv[0], strerror(errno));
exit(1);
} else if (child == 0) {
// do something in child
}
} else {
// do something in parent
}
}
我的問題是從那裏在子進程開始執行代碼的作用,首先執行哪即線?? 如果它執行整個代碼,它也會創建它自己的子進程,並且事情會持續發生,這並不是肯定會發生的!
如果叉()命令後開始,它是如何進去if語句在第一?
」哪一次不會發生!!!「所以爲了清楚你已經運行了這個,它只分了一次? – 2012-04-16 16:03:50
好的:fork()調用返回**兩次**:一次爲父,一次爲子。目前,父母和孩子在分叉的返回值上只有不同。 – wildplasser 2012-04-16 16:04:27