CS 111 Lecture 6 Scribe Notes - Winter 2012

by Yuting Wang, Armando De los Santos and Greg Rivera for a lecture by Professor Paul Eggert on January 30, 2012

Table of Contents

  1. Pipe
    1. What's a pipe
    2. Advantages
    3. Disavantages
    4. An example
    5. Problems with pipes
  2. Signal handling
    1. Motivation
    2. The API for signal handling
    3. Some other issues

What's a pipe

Advantages

  1. Since pipe is implemented entirely in RAM, it doesn't need any temp file or disk I/O.
  2. A pipe has a bounded buffer in it, so save RAM.

Disadvantages

  1. Pipe has to be read and written sequentially, thus no random access available.
  2. We can use shared memory instead, it allows randon access, it's faster, but it has synchronization issues.

An example

du | sort -n: sort files according to their space usage.

  1. In the root process, call int pipe(fd[2]). It returns 1 if succeeds, otherwise 0. The file descriptor of the reading end is generated and saved in fd[0], writing end in fd[1].
  2. fork() to get a child process.

Problems with pipes

Signal Handling: Motivation

The API for signal handling

  1. user includes signal.h.
  2. handler_t signal (int, handler_t)
  3. Common way of defining and installing signal handling:
    #include < signal.h >
    int gotpipe;
    void handle (int sig) { gotpipe == malloc(100); strcpy (gotpipe); } //You should be careful what system calls you use in this handler function. For example, don't call printf here or you will mess up the pointers
    int main(void) {
         old_handler = signal (SIGPIPE, handler) // With signal handling, can call from within signal call itself
         malloc(3000);
    }
  4. Some examples of signals (defined in signal.h): SIGINT, SIGPWR, SIGHUP, SIGTERM, SIGSGV, etc.
  5. The kill() system call
    int kill (pid_t, int): deliver the signal number(int) to process(pid_t).

Some other issues

Valid XHTML 1.0 Strict