CS360 Midterm -- May 2, 2002
Answer to Question 2
Part A: The syntax is:
int pipe(int p[2])
Pipe() sets up a FIFO buffer in the operating system, and
returns two file descriptors to the calling process. P[1]
may be written to, and up to PIPE_BUF bytes are written to
the buffer before the calling process blocks. P[0] may be
used to read from the buffer, and clears the bytes read from the
buffer so that more bytes may be written.
A pipe is typically used to set up a communication channel between
two processes that are related through fork() calls.
Pipe returns zero on success, and a negative number on failure.
Part B: The syntax is:
int dup(int fd)
Dup duplicates a file descriptor, and returns a new file
descriptor. The two file descriptors, although they have different
values, may be used interchangably. In the operating system, the
file descriptors are indices to a file table. Both indices (the
argument to dup and the return value) will point to the same
file table entry when dup() returns.
Part C: Pthread_mutex_lock() takes as its argument a
data structure called a mutex. When pthread_mutex_lock()
returns, it is said to ``hold'' the mutex. If
pthread_mutex_lock() is called when another thread holds the
mutex, the calling thread will block until the mutex has been
unlocked. Therefore, only one thread may hold the mutex at any
one time.
Part D. See
the midterm exam, question 4, part C.
Part E. You need to set up a sequence of commands so that
one process is writing to another process that has already exited.
Here are a few examples:
- cat | ls -- as soon as the user types RETURN
into the keyboard,
the cat process will generate SIGPIPE, since the ls
process will have exited.
- cat | cat /dev/null -- same thing.
- cat f2 | cat f1 -- I'll give credit for this. The
second process should exit much more quickly than the first, which
should then generate SIGPIPE.
- cat f2 | head -10 -- again, the head process
will exit well before cat is done, so cat will generate
SIGPIPE.
Grading
- Part A: 3 points.
- Part B: 2 points.
- Part C: 2 points.
- Part D: 2 points. Since you had seen this before, you got zero
if you got it wrong.
- Part E: 3 points. Two for getting a good sequence of commands,
and one for saying that the correct process received SIGPIPE.