CS360 Final -- December 10, 2002
Answers to Question 1
The key here is to know which file descriptors match which files.
Initially, when p1 is called, we have:
- File descriptor 0: Coming from f3.
- File descriptor 1: Going to fout-3.
- File descriptor 2: Going to the screen.
- File descriptor 3: Coming from f1.
- File descriptor 4: Coming from f2.
- File descriptor 5: Going to fout-1.
- File descriptor 6: Going to fout-2.
- File descriptor 7: Coming from the pipe.
- File descriptor 8: Going to the pipe.
So, the answers:
- C1: f3 is written to fout-3:
fout-1: a |
fout-2: a |
fout-3: d |
screen: a |
- C2:File descriptor 3 (f1) is written to file descriptor 5
(fout-1):
fout-1: b |
fout-2: a |
fout-3: a |
screen: a |
- C3: f2 is written to fout-3:
fout-1: a |
fout-2: a |
fout-3: c |
screen: a |
- C4: f3 is written to the screen:
fout-1: a |
fout-2: a |
fout-3: a |
screen: d |
- C5: f1 is written to the pipe. Since all of f1
will fit into the pipe's buffer, the write()'s will complete
just fine. No files will be altered.
fout-1: a |
fout-2: a |
fout-3: a |
screen: a |
- C6: f2 is written to the read-end of the pipe.
This will cause an error on write(), which will be printed
to the screen:
fout-1: a |
fout-2: a |
fout-3: a |
screen: w |
- C7: This is going to hang forever reading from the pipe, which
has an open write end, but nothing in it.
fout-1: z |
fout-2: z |
fout-3: z |
screen: z |
- C8: This dup's file descriptor 3 (f1) onto
file descriptor 9 (the lowest unused one), and then
writes that to fout-2:
fout-1: a |
fout-2: b |
fout-3: a |
screen: a |
- C9: This dup's file descriptor 1 (fout-3) onto
file descriptor 3, and then tries to read from it. This will cause
an error, because the file descriptor is not write-only:
fout-1: a |
fout-2: a |
fout-3: a |
screen: i |
- C10: This dup's file descriptor 0 (f3) onto
file descriptor 3, and then dup's file descriptor 4 (f2)
onto file descriptor 1. Then file descriptor 3 (now f3) is
written to file descriptor 5 (f1).
fout-1: d |
fout-2: a |
fout-3: a |
screen: a |
- C11:dup's file descriptor 6 (fout-2) onto
standard error, then closes file descriptor 8, and then tries to read
from it. That will generate an error, but since standard error is
now going to fout-2, the error message will go to fout-2:
fout-1: a |
fout-2: n |
fout-3: a |
screen: a |
- C12: Now, two processes are forked. f3 goes to
fout-3 in the first, and f1 goes to fout-2 in
the other:
fout-1: a |
fout-2: b |
fout-3: d |
screen: a |
- C13: Data now flows from f2 through the pipe to fout-1:
fout-1: c |
fout-2: a |
fout-3: a |
screen: a |
- C14: Again, two processes are forked. In the first,
an error will be generated trying to read from the closed file
descriptor 3. That message will go to the screen. In the other
process, f3 will be written to fout-1:
fout-1: d |
fout-2: a |
fout-3: a |
screen: i |
- C15: Now, file descriptor 3 is closed again, which
will generate a read error. However, this time, the error will
go to fout-3, since it has been dup'd onto standard
error.
fout-1: a |
fout-2: a |
fout-3: i |
screen: a |
- C16: The same dup's as before are done. However,
there's no error this time. f2 will be written to
f1 (which has been dup'd onto file descriptor 1).
fout-1: c |
fout-2: a |
fout-3: a |
screen: a |
- C17: Finally, standard error is closed, and fout-2 is
dup'd onto it. Therefore, when the program tries to read from
file descriptor 1, which is write-only, it will generate an error message
which is printed to fout-2:
fout-1: a |
fout-2: g |
fout-3: a |
screen: a |
If you want to try these out yourself,
grab wcat.c
and p1.c from this page.
Grading: 18 points
For the majority of commands, each command was worth 1 point, divided
in half. You got half credit if you got the correct input file and
half credit if you got the correct output file. If you got the correct
input file but wrote two output files, you only got half credit.
If there was an error message, I gave lots of partial credit on which
error message was produced. If the actual error was a read error, then
you received .25 for putting down any read error, even if you
didn't put down the correct one. If you put down a write error, then
you received .15 points, and if you answered e, then you received
.1 points. Finally, if you put down the wrong kind of error, but the
correct file descriptor, you received .3 points.
Four questions had special grading.
- In C5, the answer should have been
a for all outputs -- if you answered that, you received .5
points. Otherwise zero.
- In C7, the answer should have been z somewhere. If you
put a z down, you received full credit (.5 points).
If you put down all a's (since there was no output), you received
.3 points.
- In C12, there were two inputs and outputs, so the problem was worth
2 points.
- In C14, there were two inputs and outputs, so the problem was worth
2 points.
In case you're wondering, this means that there were 13 commands worth
one point, 2 commands worth .5 points, and 2 commands worth 2 points.
Oh, and the random lottery extra credit was answering i, w, g, a for
C18.