9.7 Exercises

  1. 1.

    Implement counting semaphores in xv6. Choose a few of xv6’s uses of sleep and wakeup and replace them with semaphores. Judge the result.

  2. 2.

    Can you implement a variant of sleep() that takes just one argument, the channel, and doesn’t need a lock argument?

  3. 3.

    Fix the race mentioned above between kill and sleep, so that a kill that occurs after the victim’s sleep loop checks p->killed but before it calls sleep results in the victim abandoning the current system call.

  4. 4.

    Design a plan so that every sleep loop checks p->killed so that, for example, a process that is in the virtio driver can return quickly from the while loop if it is killed by another process.