使用管道复制字符串

发布于 2024-08-18 17:20:01 字数 1473 浏览 5 评论 0原文

我编写了以下代码,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悸初 2024-08-25 17:20:01

您的 printf 正在写入 stdout - 但在父级和子级中,您已将文件描述符 1 重定向到管道,因此这就是 printf< 的位置/code> 输出将会继续。

使用 fprintf(stderr, ...) 而不是 printf(...) - 然后您将能够看到输出,因为 stderr< /code> 仍然指向您的终端。

请注意,您有一些错误:

  • 子级在完成时应该调用 _exit(0) ,否则它将落入父级代码中;
  • write 应该使用 strlen(string) + 1,以便写入 nul 终止符。

Your printfs are writing to stdout - but in both the parent and child, you've redirected file descriptor 1 to a pipe, so that's where the printf output will go.

Instead of printf(...), use fprintf(stderr, ...) - then you'll be able to see the output, since stderr is still pointing to your terminal.

Note that you have a couple of bugs:

  • the child should call _exit(0) when it is done, otherwise it will drop into the parent code;
  • the write should use strlen(string) + 1, so that it writes the nul terminator.
傻比既视感 2024-08-25 17:20:01

尝试添加“\n”,例如 printf("\nParent buf: %s\n", buf);

Try adding a "\n", like printf("\nParent buf: %s\n", buf);

離人涙 2024-08-25 17:20:01

我猜测这些管道正在执行阻塞 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文