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:

Grading