Advanced Programming in the
UNIX Environment

by W. Richard Stevens
Addison-Wesley Professional Computing Series
0-201-56317-7 * Hardcover * 768 pages * ©1992
[Buy this book]

Chapter 14. Interprocess Communication

Introduction * Pipes * popen and pclose Functions * Coprocesses * FIFOs * System V IPC * Identifiers and Keys * Permission Structure * Configuration Limits * Advantages and Disadvantages * Message Queues * Semaphores * Shared Memory * Client-Server Properties * Summary

Example Relevant Functions
14.1 Send data from parent to child via a pipe pipe, fork, close, write, read
14.2 Display a file by piping into a PAGER program (like more) fopen, pipe, fork, close,
fgets, strlen, write, ferror,
waitpid, dup2, getenv, strrchr, execl
14.3Routines to let a parent and child synchronize:
(Similar to Sec. 10.17 but using pipes instead of signals)
TELL_WAIT Init function
TELL_PARENT Child: notify parent
TELL_CHILD Parent: notify child
WAIT_PARENT Child: wait for parent notification
WAIT_CHILD Parent: wait for child notification
pipe, write, read
14.4 Display a file by piping into a PAGER program
(Like 14.2 but using popen)
fopen, fgets, fputs, ferror,
popen, pclose
14.5 Possible implementation of popen and pclose popen :
open_max (Stevens' library),
calloc, pipe, fork,
close, dup2, execl
_exit, fdopen
pclose :
fileno, fclose, waitpid
14.6 filter (stdin->stdout) to convert uppercase to lowercase getchar, putchar,
isupper, tolower,
14.7 Invoke UC->LC filter above to read commands
Using popen in read mode
popen, fgets, fputs,
fflush, pclose, putchar,
err_sys (Stevens' library)
14.8 add2: filter (stdin->stdout) example:
Read two numbers from stdin, write sum to stdout.
read, sscanf, sprintf, strlen, write, exit,
err_sys (Stevens' library)
14.9 Test driver for 14.8 filter
Catching SIGPIPE and acting appropriately.
signal, pipe, fork,
close, fgets, strlen,
write, read, fputs,
ferror, dup2, execl,
err_sys (Stevens' library)
14.10 add2: filter to add two numbers (like 14.8)
Bad example: wrongly using buffered I/O (stdio). It needs:
setvbuf(stdout, NULL, _IOLBF, 0)
before the loop to work correctly.
sscanf, printf, fgets,
err_sys (Stevens' library)
14.11 Shared memory example:
Print where different types of data (static array, stack, heap, shared-mem) are stored.
printf, malloc,
shmget, shmat, shmctl,
err_sys (Stevens' library)
14.12 IPC between parent & child
Using memory mapped I/O of /dev/zero.
/dev/zero is used to get a mapped shared memory area initialized to 0. that parent & child can modify in turn.
open, mmap, close,
fork, exit,
err_sys, err_quit (Stevens' lib)

[Buy this book]