使用管道复制字符串
我编写了以下代码,使用 fork 和管道将字符串“hello world”复制到另一个 char 数组,而不是使用标准库函数或标准 i/o 流。该程序已成功编译,但我没有得到任何输出。甚至, 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;
}
i have written the following code to copy a string "hello world" to another char array using fork and pipes instead of using standard library functions or standard i/o streams. The program is compiling successfully but i am not getting any output. Even, the printf's output are not being shown.
# 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 printf 正在写入 stdout - 但在父级和子级中,您已将文件描述符 1 重定向到管道,因此这就是 printf< 的位置/code> 输出将会继续。
使用
fprintf(stderr, ...)
而不是printf(...)
- 然后您将能够看到输出,因为stderr< /code> 仍然指向您的终端。
请注意,您有一些错误:
_exit(0)
,否则它将落入父级代码中;write
应该使用strlen(string) + 1
,以便写入 nul 终止符。Your
printf
s are writing tostdout
- but in both the parent and child, you've redirected file descriptor 1 to a pipe, so that's where theprintf
output will go.Instead of
printf(...)
, usefprintf(stderr, ...)
- then you'll be able to see the output, sincestderr
is still pointing to your terminal.Note that you have a couple of bugs:
_exit(0)
when it is done, otherwise it will drop into the parent code;write
should usestrlen(string) + 1
, so that it writes the nul terminator.尝试添加“\n”,例如
printf("\nParent buf: %s\n", buf);
Try adding a "\n", like
printf("\nParent buf: %s\n", buf);
我猜测这些管道正在执行阻塞 IO,因此读取将不会返回,除非管道被其他进程关闭。这和 printf 执行缓冲 IO 一样,会阻止您获得任何输出。
I'd guess that those pipes are doing blocking IO, so read will simply not return unless the pipe is closed by the other process. That, and printf doing buffered IO, prevents you from getting any output.