複数のコマンドをパイプラインで区切って実行するシェルを作成するように頼んでいる宿題があります。ここチャイルズをフォークしてコマンドを実行するための私のコードスニペットは、次のとおりです。複数のパイプラインを実装してCでコマンドを実行
for (int i = 0; i < commands; i++) {
place = commandStarts[i];
if((pid = fork()) < 0) exit(1);
else if (pid == 0) {
if(i < pipe_counter && dup2(fds[j + 1], 1) < 0) exit(1);// If this is not the last remaining command
if(j != 0 && dup2(fds[j-2], 0) < 0) exit(1); // If this is not the first command
for(int c = 0; c < 2*pipe_counter; c++) close(fds[c]);
if(execvp(argv[place], argv+place)) exit(0); // Execute and if it returns anything, exit!
}
j+=2;
}
for(int i = 0; i < 2 * pipe_counter; i++) close(fds[i]);
while ((wait(&status)) != pid); // The only program that gets to this point is the
// parent process, which should wait for completion
、それは簡単な例ではうまく動作しているようだにもかかわらず、いくつかのより複雑で、グレーディングシステムは私にこのヒントを与える:あなたがすべきプロンプトを表示する前にパイプチェーン内のすべてのプロセスが終了するのを待ってください。
私の間違いはどこですか?
ここで、各プロセスが完了するのを待つコードはどこですか? – wallyk
スニペットの最後でわかるように、ループ中にすべてのプロセスが完了するのを待っています! –
あなたは?どうしたの?...まったく。私はあなたが思うようにそれが動作するとは思わない。 – wallyk