我有這樣的代碼:檢查兩個進程的退出碼
pid_t pid1 = fork();
if (pid1 == 0)
{
//do some stuff
if (something)
exit(0);
else
exit(1);
}
else if (pid1 == -1)
printf("error\n");
pid_t pid2 = fork();
if (pid2 == 0)
{
//do some stuff
if (something)
exit(0);
else
exit(1);
}
else if (pid2 == -1)
printf("error\n");
//here I want to check both exit codes
子進程將並行運行。我需要的是檢查退出代碼是否是1
或0
。我以爲我可以使用這樣的:
pid_t pid;
int status;
while((pid = wait(&status)) > 0)
printf("%d exit code: %d\n", pid, WEXITSTATUS(status));
我新的並行程序,所以我不知道這是一個正確的解決方案。在父進程到達循環之前,其中一個子進程沒有退出的可能性,因此它不會得到退出代碼?
你的代碼有什麼問題?當你運行它會發生什麼? –