Question
One-way communication from a UNIX process to another through a pipe. In Assignment 1, we constructed two small C programs called writer and reader .
One-way communication from a UNIX process to another through a pipe.
In Assignment 1, we constructed two small C programs called writer and reader. We also created a pipe so that the output of writer is sent to reader as input:
writer n | reader
In that case, the pipe was created and managed by the shell. In this assignment, we will write another C program with the same goal of managing a pipe between two processes:
communicate n
The program can be structured as follows. It makes a pipe system call to create a pipe to be used by its child processes. It then forks to create a child process which uses execlp later to become the writer. communicate forks again to create another child process which again uses execlp to become the reader. After that, communicate simply waits for the writer and the reader to terminate before winding up the program. Thus, the purpose of communicate is to simply establish a pipe and launch the two processes writer and reader.
The pipe system call requires an array of two int components to be passed as its parameter. pipe returns two file descriptors (fd's) to be used by the two communicating processes one for reading and the other for writing. Thus, to read-from or write-into a pipe, we can use the read and write system calls, using appropriate fd's. But, in our case, the writer uses printf and the reader uses getchar. Normally, getchar is supposed to work with the standard input file (fd = 0), and printf is supposed to work with the standard output file (fd = 1). Thus, if getchar and printf should be used for the pipe communication, there is a need to close appropriate standard files and duplicate (using the system call dup) the file descriptors for the pipe suitably in the two processes. Thus, the UNIX system calls to be used are pipe, dup and close, the syntax and the semantics of which have been provided in an earlier handout distributed in class.
The following description of UNIX provides further hints to the structure of the program. For each user, UNIX maintains a file descriptor table to keep track of all open files. When a process is set up, three files are open standard input (fd = 0), standard output (fd = 1) and standard error (fd = 2). When a new file is opened the smallest unused file descriptor number is assigned. That is, with all three standard files open, the file descriptor for a new file opened will be 3. The dup system call allows an additional fd to be assigned to a file or pipe. This system call also uses the lowest unused fd number. Consider, for example, a file whose fd = 3. Now, in order to substitute this file for standard output, we need the following code:
close(1); dup(3); close(3);
Notice that we perform dup(3) to obtain a duplicate fd for the file with fd = 3. Since fd = 1 has already been closed, this fd can be assigned to the file. We also close fd = 3, since it is no longer needed. It is considered good style in programming to release unused fd's by closing them. In the case of a pipe, it is not just a matter of programming style; it is necessary for the correct working of the program. A process attempting to read from the pipe will see an end-of-file condition only if all processes that have access to the pipe have closed the writing end. So, be sure to release all unused pipe fd's in all processes as early as possible. In your program, after the fork system calls, the child processes must perform appropriate close and dup before executing execlp. fork and execlp preserve the open fd's and hence the effect of switching from the standard input/output to pipe will be in effect even after executing execlp.
Note: (Not required for this assignment) If a program wants to substitute another file for standard output, it simply needs to close the standard output and open the new file so that fd = 1 is assigned. But it is not possible to restore the original situation. In such circumstances, the following code is used:
fd = dup(1); close(1);
open(data, O_WRONLY, 0); /*open data for write*/
...
close(1); dup(fd); close(fd);
Create a script output which shows the listing of all three programs writer, reader, and communicate. Then show the execution of the programs, for the values of n = 1, 5, 8, 10, in two forms: (a) writer and reader communicating through a pipe created by the shell (as in Assignment 1), and (b) writer and reader communicating through a pipe created by communicate, i.e., invoke communicate with argument n.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started