Assignment 10, due Apr 18Solutions
Part of
the homework for 22C:112, Spring 2008
|
A problem: Write Unix code to do the actions A to J in the order given below:
A / \ B C / \ / \ D E F G \ / \ / H I \ / J
Note that D and E must be completed before H, and F and G must be completed before I. The order ABDEHCFGIJ would be legal, as would ABCDEFGHIJ and ACGFIBEDHJ. This will take 3 forks and 3 waits, but it is important for the parent process to wait for specific children at the right time, and if some child terminates in an unexpected order, it is important for the parent to remember that this child has already terminated. (1.0 points).
pid_t ida; A; ida = fork() if (ida) { /* parent */ pid_t idb; B; idb = fork() if (idb) { /* parent */ pid_t idc; D; do { /* wait for the correct child to exit */ idc = wait( NULL ); if (idc == ida) ida = 0; } while (idc != idb); } else { /* child */ E; exit( EXIT_SUCCESS ); } H; if (ida != 0) { /* ida may have already exited */ (void) wait( NULL ); } } else { /* child */ C; if (fork()) { /* parent */ F; (void) wait( NULL ); } else { /* child */ G; exit( EXIT_SUCCESS ); } I; } J;
a) Augment the following code so that statements B and C can communicate through a pipe. Specifically, statement B produces data that statement C consumes. Make sure you close any unneeded ends of the pipe, and make sure that all evidence of the pipe has been closed by the time the two logical threads of computation join:
(1 point)if (fork()) { /* parent */ B; wait(); } else { /* child */ C; exit(); }
{ int mypipe[2]; pipe( mypipe ); if (fork()) { /* parent */ close( mypipe[WRITEEND] ); B; close( mypipe[READEND] ); (void) wait( NULL ); } else { /* child */ close( mypipe[READEND] ); C; close( mypipe[WRITEEND] ); exit( EXIT_SUCCESS ); } }
b) Pipes were never intended for other purposes, but they can be abused in some interesting ways. Consider using a pipe as a semaphore, with the number of characters of data in the pipe used to indicate the count in the semaphore. Give implementations of the wait and signal operations using a pipe as the semaphore representaiton. (1 point)