質問
標準ライブラリ関数や標準 I/O ストリームを使用する代わりに、フォークとパイプを使用して文字列「hello world」を別の char 配列にコピーする次のコードを作成しました。プログラムは正常にコンパイルされていますが、出力が得られません。printf の出力も表示されません。
# include <string.h>
# include <unistd.h>
# include <stdio.h>
char string[] = "hello world";
int main()
{
int count, i;
int toPar[2], toChild[2];
char buf[256];
pipe(toPar);
pipe(toChild);
if (fork() == 0)
{
printf("\n--- child process ---");
close(0);
dup(toChild[0]);
close(1);
dup(toPar[1]);
close(toPar[1]);
close(toChild[0]);
close(toPar[0]);
close(toChild[1]);
for (;;)
{
if ((count = read(0, buf, sizeof(buf))) == 0)
break;
printf("\nChild buf: %s", buf);
write(1, buf, count);
}
}
printf("\n--- parent process ---");
close(1);
dup(toChild[1]);
close(0);
dup(toPar[0]);
close(toPar[1]);
close(toChild[0]);
close(toPar[0]);
close(toChild[1]);
for (i = 0; i < 15; i++)
{
write(1, string, strlen(string));
printf("\nParent buf: %s", buf);
read(0, buf, sizeof(buf));
}
return 0;
}
解決
あなたの printf
が書いています stdout
- ただし、親と子の両方で、ファイル記述子 1 をパイプにリダイレクトしているため、そこが printf
出力は行きます。
の代わりに printf(...)
, 、 使用 fprintf(stderr, ...)
- その後、出力を確認できるようになります。 stderr
はまだ端末を指しています。
いくつかのバグがあることに注意してください。
- 子供は電話すべきです
_exit(0)
完了すると、それ以外の場合は親コードにドロップされます。 - の
write
使用する必要がありますstrlen(string) + 1
, これにより、null ターミネータが書き込まれます。
他のヒント
printf("\nParent buf: %s\n", buf);
私はパイプが他のプロセスによって閉鎖されていない限り、単純に戻りません読んで、これらのパイプはブロッキングIOを行っていることを推測すると思います。これ、およびバッファIOをやってprintfの、任意の出力を取得してからの防止ができます。
所属していません StackOverflow