Lecture 010

Error

Three Kinds of Errors Solution:

  1. solve the error (doubling the memory)
  2. report the error (return -1)
  3. prevent the error to damage the system (reboot)
  4. redesign your system

Paradise Lost

There are issues when you write worker threads:

  1. When queue is temporarily empty, it doesn't mean it will be empty forever
  2. cond_wait() might not return to the same thread
mutex_lock(&m);
if (out_of_work) {
  w = (item_t*) NULL;
} else {
  if (!(w = (item_t*) dequeue(q))) {
    cond_wait(cvar, &lock);
    w = (item_t*) dequeue(q);
  }
}
mutex_unlock(&m);
return w;

Above code is wrong. The following code is more correct, but still wrong:

mutex_lock(&m);
if (out_of_work) {
  w = (item_t*) NULL;
} else {
  // change 'if' to 'while'
  while (!(w = (item_t*) dequeue(q))) {
    cond_wait(cvar, &lock);
  }
}
mutex_unlock(&m);
return w;

This is not a standard "time of check to time of use" (TOCTTOU) bug.

Yield

The below content assumes "user thread" (single process, no parallism)

To implement yield():

Yield between process: INT 50 syscall

Process switch is more than conditional variable: it execute when

I/O Example:

Table of Content