How to Use Pipes on Linux

How to Use Pipes on 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.

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?

3 Answers 3

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.

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 worldn” 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.

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.

Not the answer you’re looking for? Browse other questions tagged c linux pipe or ask your own question.

Related

Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

  • Blog
  • Facebook
  • Twitter
  • LinkedIn
  • Instagram

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2020.12.10.38158

The tr (translate) command is used in Linux mainly for translating and deleting characters. It can be used to convert uppercase to lowercase, squeeze repeating characters and deleting characters.

Tr command requires two sets of characters for transformations and also can be used with other commands using Unix pipes for advanced translations.

In this tutorial, we learn how to use tr command in the Linux operating systems through some examples.

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.