メインプロセスを実行して4つのサブプロセスを作成したいと考えています。それらが作成されたら、メインプロセスが完了するのを待つようにします。ループ内でのforkが考えられないようにする
これは私の全体のコードです:
..
[8508] I the child, will sleep
[8503] I the parent, have waited for my children!
[8511] I the child, will sleep
[8510] I the child, will sleep
[8509] I the child, will sleep
[8520] I the child, will sleep
[8511] I the parent, have waited for my children!
[8510] I the parent, have waited for my children!
(prompt stuck)
なぜ親は最後に、一度に複数回プリントアウトの代わりん:私が代わりに何を得る
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
int i;
int childID;
int status;
for (i=0;i<4;i++){
childID=fork();
if (childID==0){
printf("[%d] I the child, will sleep\n", (int)getpid());
sleep(1);
}
}
if (!childID){
wait(&status);
printf("[%d] I the parent, have waited for my children!\n", (int)getpid());
}
return 0;
}
はこれですか?
出力があなたのコードと一致しません。それを訂正してください –
あなたが私たちに与えたコードは、「私は親です、私の子供を待つでしょう!コードにはそれ以上のものがありますか? – murgatroid99
回答に記載されているように、あなたの子供たちもフォークするだけでなく、あなたの子供たちのすべてではなく、最後のフォークされた子供を待っています。 – Shahbaz