Cprint.c (Horspool, p.253)
- line   #9 opens a file
- line #11 send you to the start of the loop for the next file
- line #13 redirects stdin
- line #14 closes one fd, since you do not want 2 fds open for one file
-- do always after a dup2
- line #15 - a system call
that creates a pipe -- allocates a protected chunk of memory and a read-pointer and a write-pointer,
Opens two entries, adds to the fd table. The first entry is the read end of the pipe. The second
entry is the write end of the pipe.
Note that pipe(fd) pipe(& fd[0] );
fd = open one fd; pipe (fd) - a pointer to 2 fds
e.g.
cb file1 | lpr, ..., cb filen | lpr
cb --"w"---->     |-----4k-----|----"r "-------> lpr
Process Tree parent child
I/O Interaction
parent & child |-----4k-----| parent & child
Open fd Table
- open - adds an entry
- close - removes it
- dup - copies a description of an open file to the next available location
- dup2 - copies a description of an open file to a specified position
- pipe - system call; can be used on i/o relations between processes (read end/write end)
- adds 2 entries to the open file descriptor table
Approach:
- create a pipe in one process
- use the fork system call to create a copy of this process
- in one process, close the read end of the pipe
- in the other process, close the write end of the pipe
N.B.
- open the pipe in the parent
- let the child do the write, the parent do the read
Process Hierarchy -- lpr child parent process cb child
I/O Relation -- file1 ... cb child stdout (pipe) stdin
lpr child
Click here to view the file description table for the
cprint program ... (beware it's slow!)
Table of Contents