CS 111 Scribe Notes: Lecture 6 (4/15/09)

by Alan Chan and Emily Tran

What can go wrong with files?

  1. File Descriptors
  2. Creating a temporary file

Pipes

Pipes are a bounded I/O buffer. The following command is an example of a pipe being used in the shell. It sorts files and directories in order according to size:

$ du | sort -n

This command equivalent to the following:

$ du > files.list

$ sort -rn files.list

Using a pipe is faster because it runs both commands in parallel. We will not encounter any race conditions with them, and we do not have to use any temporary files to save information.

What can go wrong with pipes?

  1. Write hangs.
  2. write() returns -1 and sets errno = ESPIPE, but the majority of programs don't chech this exit status.
  3. Writer gets signal SIGPIPE and exits.

Signals

Signals provide a way for asynchronous event handling. The operating system defines a set of signals, each of which are handled differently by the processes that receive them. When a process receives a signal, the operating system interrupts the normal flow of execution, and calls the relevant signal handler.

Signal handling is vulnerable to race conditions. For example, in this case:

signal(SIGPIPE, cleanup);

void cleanup(int p) {

malloc(19);

}

malloc() is not reentrant. That is, it cannot be safely executed concurrently if a second cleanup signal was delivered to a process that is in the middle of executing of the first cleanupsignal handling routine.