UNIX Lecture Notes – Chapter 08

Categories:

Recommended

8.1 Introduction

Processes that cooperate in order to complete one or more tasks almost always need to communicate with each other. Sometimes the communication requires sharing data. One method of sharing data is by sharing a common file. If at least one of the processes modifies the file, then the file must be accessed in mutual exclusion. Sharing a file is essentially like sharing a memory-resident resource in that both are a form of communication that uses a shared resource that is accessed in mutual exclusion. Another paradigm involves passing data back and forth through some type of communication channel that provides the required mutual exclusion. A pipe is an example of this, as is a socket. This type of communication is broadly known as a message-passing solution to the problem.

This chapter is concerned only with message-passing types of communication. We will begin with unnamed pipes, after which we will look at named pipes, also known as FIFO ‘s, and then look at sockets. Part I is exclusively related to pipes.

8.2 Unnamed Pipes

You are familiar with how to use pipes at the command level. A command such as

$ last | grep ‘reboot’

connects the output of last to the input of grep, so that the only lines of output will be those lines of last that contain the word ‘reboot’. The ‘|’ is a bash operator; it causes bash to start the last command and the grep command simultaneously, and to direct the standard output of last into the standard input of grep.

Although ‘|’ is a bash operator, it uses the lower-level, underlying pipe facility of UNIX, which was invented by Douglas Mcilroy, and was incorporated into UNIX in 1973. You can visualize the pipe mechanism as a special file or buffer that acts quite literally like a physical pipe, connecting the output of last to the input of grep, as in Figure 8.1.

The last program does not know that it is writing to a pipe and grep does not know that it is reading from a pipe. Moreover, if last tries to write to the pipe faster than grep can drain it, last will block, and if grep tries to read from an empty pipe because it is reading faster than last can write, grep will block, and both of these actions are handled behind the scenes by the kernel.

What then is a pipe? Although a pipe may seem like a file, it is not a file, and there is no file pointer associated with it. It is conceptually like a conveyor belt consisting of a fixed number of logical blocks that can be filled and emptied. Each write to the pipe fills as many blocks as are needed to satisfy it, provided that it does not exceed the maximum pipe size, and if the pipe size limit was not reached, a new block is made available for the next write. Filled blocks are conveyed to the read-end of the pipe, where they are emptied when they are read. These types of pipes are called unnamed pipes because they do not exist anywhere in the file system. They have no names.

An unnamed pipe1 in UNIX is created with the pipe() system call.

#include <unistd.h> int pipe(int filedes[2]);

The system call pipe(fd), given an integer array fd of size 2, creates a pair of file descriptors, fd[0] and fd[1], pointing to the “read-end” and “write-end” of a pipe inode respectively. If it is successful, it returns a 0, otherwise it returns -1. The process can then write to the write-end, fd[1], using the write() system call, and can read from the read-end, fd[0], using the read() system call. The read and write-ends are opened automatically as a result of the pipe() call. Written data are read in first-in-first-out (FIFO) order. The following program (pipedemo0.c in the demos directory) demonstrates this simple case.

Attribution

Stewart Weiss (2019), UNIX Application and System Programming, URL: http://www.compsci.hunter.cuny.edu/~sweiss/course_materials/unix_lecture_notes.php

This work is licensed under Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) :  (https://creativecommons.org/licenses/by-sa/4.0/).

VP Flipbook Maker

Get creative with Visual Paradigm Online Flipbook Maker! Easily convert your content into interactive flipbooks and customize them with the user-friendly design tools. Try it now and breathe life into your digital works!