From the course: LPIC-1 Exam 101 (Version 5.0) Cert Prep

Named and unnamed pipes - Linux Tutorial

From the course: LPIC-1 Exam 101 (Version 5.0) Cert Prep

Start my 1-month free trial

Named and unnamed pipes

- [Instructor] A pipe is a way of one program communicating with another. A common use for a pipe is taking the output of one command and sending it to the input of another command. For instance, we could grap a file and if the output was too long to fit on the screen, we could pipe the output to less a pager. Type into a terminal grap space TCP space slash ETC slash services space pipe space less, and hit enter. This looks through the ETC services file for the word TCP and then sends the output to less which presents it one page at a time. Press Q to quit. You can pipe any number of times, as it's not limited to just two commands. For example, type in grap space TCP space slash ETC services space pipe space AWK space single quote left curly bracket print space dollar sign one right curly bracket single quote space pipe space sort space pipe space less. This searches the ETC services file for the word TCP and then sends the lines to awk, which prints out the first column only, then sends the remaining output to sort, which sorts it, and finally sends it to less to be one page at a time. Hit enter. The type of pipes we are using are considered unnamed pipes. An unnamed pipe is a direct connection between two commands running in the same terminal. If we want to send output from a command in one terminal to another command in a different terminal, we can use a named pipe, or FIFO. FIFO stands for first in, first out. This is a pipe that exists in the file system. The pipe acts like a file on a disk that one process can write to while another reads from. A named pipe is still a direct connection between programs that just uses the file system as an interface. When one process is writing to the named pipe, its output gets blocked until the receiving process starts reading it. Let's take a look at how this works. To set this up, let's open a second terminal by going to our file menu and down to open terminal. This terminal we're going to drag to the right and bump the font size. I'll drag the original terminal to the left so it fills up the remaining space. Press Q to get out of less. Now we're going to create our named pipe. In the left-hand terminal, type in MK FIFO space named underscore pipe, and hit enter. This makes a pipe called named underscore pipe. This name is arbitrary. You can choose a different name if you wish. Now let's send some data to this pipe using echo in a redirect. Type in echo space double quote hi double quote space greater than space named pipe, and hit enter. Notice that I'm in my home directory in both of these terminals. Also notice that we didn't get our terminal bent. This is because the IO is being blocked. The echo command can't finish printing the word hi until another process reads it from the named pipe. Now in the second terminal, let's use cat to read the pipe. Type in cat space named pipe, and hit enter. We see that as soon as cat reads the output from the named pipe, the echo command was able to finish. We can use named pipes to send output from one command in the OS to another command without them running in the same shell or even by the same user. We don't need our second terminal anymore, so close it and make the first one full screen. Now let's take a look at the metadata of the named pipe using LS. Type in LS space dash L, and hit enter. You'll notice that the left-hand column, we can see that the leftmost character is a P for pipe. To get rid of a named pipe, you can delete it like any other file. Type in RM space named pipe, and hit Enter.

Contents