21 Pipes
A “pipe” is a FIFO character list, which is managed by UNIX as yet another variety of file.
One group of processes may “write” into a “pipe” and another group may “read” from the same “pipe”. Hence “pipe”s may be, and are used, primarily for interprocess communication.
By exploiting the concept of a “filter”, which is a program which reads an input file and transforms it into an output file, and by using “pipes” to link two or more programs of this type together, UNIX offers its users a surprisingly comprehensive and sophisticated set of facilities.
21.1 pipe (7723)
A “pipe” is created as a result of a system call on the “pipe” procedure.
-
7728:
Allocate an “inode” for the root device;
7731:
Allocate a “file” table entry;
7736:
Remember the “file” table entry as “r” and allocate a second “file” table entry;
7744:
Return user file identifications in R0 and R1;
7746:
Complete the entries in the “file” array and the “inode” entry.
21.2 readp (7758)
“pipes” are different from other files in that two separate offsets into the file are kept – one for “read” operations and one for “write” operations. The “write” offset is actually the same as the file size.
-
7763:
the parameter passed to “readp” is a pointer to a “file” array entry, from which an “inode” pointer can be extracted;
7768:
“plock” (7862) ensures that only one operation takes place at a time: either “read” or “write”;
7776:
If a process wishing to write to a “pipe” has been blocked because the pipe was “full” (or rather because the valid part of the file had reached the file limit), it will have signified its predicament by setting the “IWRITE” flag in “ip-\({\gt}\)i_mode”;
7786:
Release the lock before going to sleep;
7787:
“i_count” is the number of file table entries pointing at the “inode”. If this is less than two, then the group of “writers” must be extinct;
7789:
A process waiting for input will raise the “IREAD” flag. Since a pipe cannot be full and empty simultaneously, no more than one of the flaqs “IWRITE” or “IREAD” should be set at any one time;
7799:
“prele” unlocks the file and “wakes up” any process waiting for the pipe.
21.3 writep (7805)
The structure of this procedure echoes that of “readp” in many respects.
-
7828:
Note that a “writer”, which finds that there are no more “readers” left, receives a “signal” just in case he is not monitoring the result of his “write” operation.
(A “reader” in the analogous situation receives a zero character count as the result of the read, and this is the standard end-of-file indication.)
7835:
The “pipe” size is not allowed to grow beyond “PIPSIZ” characters. As long as “PIPSIZ” (7715) is no greater than 4096, the file will not be converted to a “large” file. This is highly desirable from the viewpoint of access efficiency.
(Note that “PIPSIZ” limits the “write” offset pointer value. If the “read” offset pointer is not far behind, the true content of the “pipe” may be quite small).
21.4 plock (7862)
Lock the “inode” after waiting if necessary. This procedure is called by “readp” (7768) and “writep” (7815).
21.5 prele (7882)
Unlock the “inode” and “wake” any waiting processes. This procedure is called by several others (especially “iput”), in addition to “readp” and “writep”.
Section Five is the final section: last but not least. It is concerned with input/output for the slower, character oriented peripheral deviees.
Such devices share a common buffer pool, which is manipulated by a set of standard procedures.
The set of character oriented peripheral devices are exemplified by the following:
KL/DL11 interactive terminal
PC11 paper tape reader/punch
LP11 line printer.