简体   繁体   English

在Linux和C中使用管道

[英]Using pipes in Linux with C

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). 似乎将数据发送到管道并同时将其打印到屏幕上(至少好像第二次调用write函数时会这样做)。

Does anyone have any suggestions of good websites for learning about topics such as this, FIFO, signals, other basic linux commands used in C? 有没有人对优秀的网站有任何建议,以学习诸如C,FIFO,信号,C语言中使用的其他其他基本Linux命令之类的主题?

The program creates a pipe via the pipe(2) call. 该程序通过pipe(2)调用创建一个管道。 The pipe has a file descriptor open for reading ( pipefd[0] ) and one open for writing ( pipefd[1] ). 管道具有一个打开的文件描述符( pipefd[0] )和一个打开的pipefd[1]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. 程序首先将“ hello world \\ n” 写入管道的写入端,然后从管道的读取端读取消息。 The message is then written out to the console (stdout) via the write(2) call to file descriptor 1. 然后,通过对文件描述符1的write(2)调用将消息写出到控制台(stdout)。


Beej's Guide to Unix Interprocess Communication provides some good information on Unix/Linux IPC. Beej的《 Unix进程间通信指南》提供了有关Unix / Linux IPC的一些很好的信息。 You will often find references to his other guide, Beej's Guide to Network Programming . 您经常可以找到他的另一本指南Beej的《网络编程指南》

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. 我发现Bruce Molay撰写的《 理解UNIX / LINUX编程:理论与实践指南》是一本关于Unix / Linux系统编程的优秀书籍。

The first argument to write() is the file descriptor to write to. write()的第一个参数是要write()文件描述符

In the first call, the code is writing to one end of the pipe ( pipefd[1] ). 在第一个调用中,代码正在写入管道的一端( pipefd[1] )。 In the second call, it is writing to file descriptor 1, which in POSIX-compliant systems is always standard output (the console). 在第二个调用中,它正在写入文件描述符1,在符合POSIX的系统中,文件描述符1始终是标准输出(控制台)。 File descriptor 2 is standard error, for what it's worth. 就其价值而言,文件描述符2是标准错误。

The function creates a pipe and stores its endpoing file descriptors in pipefd[0] and pipefd[1] . 该函数创建一个管道,并将其终结点文件描述符存储在pipefd[0]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] . 第一个write()调用将“ hello world”写入pipefd[1] ,而read()调用从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. 然后,第二个write()调用将该数据写入文件描述符1 ,默认情况下为STDOUT ,这就是您在屏幕上看到它的原因。

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. 我推荐UNIX环境中的 W. Richard Stevens 高级编程作为理解它们的好书。 As I recall, it has good code examples. 我记得,它有很好的代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM