在带有 C 的 Linux 中使用管道

发布于 2024-09-01 03:44:32 字数 814 浏览 4 评论 0原文

我正在学习操作系统课程,我们应该学习如何使用管道在进程之间传输数据。

我们得到了这段简单的代码,演示了如何使用管道,但我很难理解它。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main()
{
      int  pipefd [2], n;
      char buff[100] ;


      if( pipe( pipefd) < 0)
      {
        printf("can not create pipe \n");
      }
      printf("read fd = %d, write fd = %d \n", pipefd[0], pipefd[1]);
      if ( write (pipefd[1],"hello world\n", 12)!= 12)
      {
        printf("pipe write error \n");
      }
      if(  ( n = read ( pipefd[0] , buff, sizeof ( buff)  ) ) <= 0 )
      {
        printf("pipe read error \n");
      }
      write ( 1, buff, n ) ;
exit (0);
  }

写函数有什么作用?它似乎将数据发送到管道并将其打印到屏幕上(至少看起来第二次调用 write 函数时它会这样做)。

有没有人可以推荐一些好的网站来学习诸如这个、FIFO、信号、C 语言中使用的其他基本 linux 命令等主题?

I'm doing a course in Operating Systems and we're supposed to learn how to use pipes to transfer data between processes.

We were given this simple piece of code which demonstrates how to use pipes,but I'm having difficulty understanding it.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main()
{
      int  pipefd [2], n;
      char buff[100] ;


      if( pipe( pipefd) < 0)
      {
        printf("can not create pipe \n");
      }
      printf("read fd = %d, write fd = %d \n", pipefd[0], pipefd[1]);
      if ( write (pipefd[1],"hello world\n", 12)!= 12)
      {
        printf("pipe write error \n");
      }
      if(  ( n = read ( pipefd[0] , buff, sizeof ( buff)  ) ) <= 0 )
      {
        printf("pipe read error \n");
      }
      write ( 1, buff, n ) ;
exit (0);
  }

What does the write function do? It seems to send data to the pipe and also print it to the screen (at least it seems like the second time the write function is called it does this).

Does anyone have any suggestions of good websites for learning about topics such as this, FIFO, signals, other basic linux commands used in C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

烏雲後面有陽光 2024-09-08 03:44:33

该程序通过 pipe(2) 调用创建管道。该管道有一个打开用于读取的文件描述符 (pipefd[0]) 和一个打开用于写入的文件描述符 (pipefd[1])。程序首先将“hello world\n”写入管道的写入端,然后从管道的读取端读取消息。然后,该消息通过 write(2) 调用文件描述符 1.


Beej 的 Unix 进程间通信指南< /a> 提供了一些关于 Unix/Linux IPC 的好信息。您经常会找到对他的其他指南的引用,Beej 网络编程指南

我找到了理解 UNIX/LINUX 编程:理论与实践指南< /em> Bruce Molay 所著,是一本关于 Unix/Linux 系统编程的优秀书籍。

The program creates a pipe via the pipe(2) call. The pipe has a file descriptor open for reading (pipefd[0]) and one open for writing (pipefd[1]). The program first writes "hello world\n" to the write end of the pipe and then reads the message out of the read end of the pipe. The message is then written out to the console (stdout) via the write(2) call to file descriptor 1.


Beej's Guide to Unix Interprocess Communication provides some good information on Unix/Linux IPC. You will often find references to his other guide, Beej's Guide to Network Programming.

I found Understanding UNIX/LINUX Programming: A Guide to Theory and Practice by Bruce Molay to be an excellent book on Unix/Linux system programming.

绮筵 2024-09-08 03:44:33

write() 的第一个参数是要写入的文件描述符

在第一次调用中,代码写入管道的一端 (pipefd[1])。在第二次调用中,它写入文件描述符 1,在 POSIX 兼容系统中该描述符始终是标准输出(控制台)。文件描述符 2 是标准错误,就其价值而言。

The first argument to write() is the file descriptor to write to.

In the first call, the code is writing to one end of the pipe (pipefd[1]). In the second call, it is writing to file descriptor 1, which in POSIX-compliant systems is always standard output (the console). File descriptor 2 is standard error, for what it's worth.

☆獨立☆ 2024-09-08 03:44:33

该函数创建一个管道并将其端点文件描述符存储在 pipefd[0]pipefd[1] 中。您向一端写入的任何内容都可以从另一端读取,反之亦然。第一个 write() 调用将“hello world”写入 pipefd[1],而 read() 调用则从 pipefd[1] 读取相同的数据代码>pipefd[0]。然后,第二个 write() 调用将该数据写入文件描述符 1,默认情况下为 STDOUT,这就是您在屏幕。

管道一开始可能会令人困惑。当您阅读/编写更多使用它们的代码时,它们将变得更容易理解。我推荐 W. Richard Stevens UNIX 环境中的高级编程作为一本了解它们的好书。我记得它有很好的代码示例。

The function creates a pipe and stores its endpoing file descriptors in pipefd[0] and pipefd[1]. Anything you write to one end can be read from the other and vice versa. The first write() call writes "hello world" to pipefd[1], and the read() call reads that same data from pipefd[0]. Then, the second write() call writes that data to file descriptor 1, which is STDOUT by default, which is why you see it on the screen.

Pipes can be confusing at first. As you read / write more code that use them, they'll become much easier to understand. I recommend W. Richard Stevens Advanced Programming in the UNIX Environment as a good book to understand them. As I recall, it has good code examples.

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