Pipes
On path: IPC | 1: Processes • 2: Interprocess Communication • 3: Shared Memory • 4: Files • 5: Unix Signals • 6: Pipes • 7: Sockets • 8: Synchronisation • 9: Synchronisation Barrier • 10: Atomicity • 11: Mutual exclusion • 12: Signal and Wait |
---|
Depends on | Queues • Streams |
---|
In Unix (and similar operating systems) a ‘pipe’ is a means of passing a serial stream of data from one process to another. A pipe is a FIFO queue with two interfaces – one for inserting data and one for removing them.
Pipes can be created to link processes, as is illustrated in this exercise.
A typical use for pipes is as a redirection of standard I/O streams, often invoked from a shell. As a random sort of example, find how many filenames in the current directory contain the letter ‘a’:
ls | grep a | wc
In an example like this (or, maybe, one which generates more traffic) the three child processes run independently, blocking if and when they are unable to input or output due to a pipe becoming empty or full or, possibly, timeslicing at other times.
Named pipes
It is sometimes said of Unix “Everything looks like a file”. Whilst not entirely true, lots of things can look like files – including pipes. Like a file, these need a name.
Try this experiment:
- Find a nice, secluded directory somewhere and open two separate shell windows upon it.
- In one window, type
mkfifo -m 666 FIFO
(“FIFO” is the name, “666” specifies the access permission.) - Try
ls -lsa
to see the pipe. - Attach one window to the pipe output with:
cat < FIFO
- Attach the other window to the pipe input with:
cat > FIFO
- Type things in your input window.
- Marvel!
- When finished, try to remember to
rm FIFO
Such a named pipe can also be created and used from within software etc. Note that (unlike a file) the open
call can block; opening one end of the pipe will block the opening process until the other end is open too. In Unix see man 3 mkfifo
for the C language interface, for example.
Similar mechanisms are available in Windows et alia, although the details of the interface are different.
Standard Streams
Unix processes have three standard I/O streams, created when the process is started:
- stdin - the default input: typically the keyboard
- stdout - the default output: typically the display (window)
- stderr - the diagnostic output: typically also the display (window)
A process will inherit its streams from a parent, so, for example, a process started by a Shell will run ‘within’ the shell window unless instructed otherwise.
stdin
and stdout
can conveniently be redirected. One application is to use pipes to connect one process to another.
Also refer to: | Operating System Concepts, 10th Edition: Chapter 3.7.4, pages 139-145 |
---|