我想統計用1,10和fork()si執行的進程的數量。該程序在Linux中執行。我真的不知道如何使用wait或WEXITSTATUS,我花了數小時在論壇上,仍然沒有得到它。有人能幫助我嗎?C:進程如何在linux中進行通信
感謝, 德拉戈什
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
int nr = 1;
int main()
{
int pid;
int i;
int stare;
for(i = 1; i<=10 ; i++)
{
pid = fork();
if(pid !=0)
{
//parent
wait(&stare);
nr = nr + stare;
}
else
{
//child
nr++;
stare = WEXITSTATUS(nr);
exit(nr);
}
}
printf("\nNr: %d\n", nr);
}
'WEXITSTATUS(nr);'只對父級有意義。在孩子的過程中,這是毫無用處的。 (孩子沒有「看見」狀態(除了第二個孩子,他們會看到第一個孩子的狀態等) – wildplasser 2013-04-06 11:23:07
在論壇上花費幾小時?爲什麼不花幾分鐘時間在[手冊頁](http:// publib .boulder.ibm.com/infocenter/tpfhelp/current/topic/com.ibm.ztpf-ztpfdf.doc_put.cur/gtpc2/cpp_wait.html#cpp_wait)? – 2013-04-06 11:24:51