Due: Saturday October 26 at 11:59pm
For the homework questions below, if you believe that you cannot answer a question without making some assumptions, state those assumptions in your answer.
void XCHG (bool *X, bool *Y) { bool tmp = *X; *X = *Y; *Y = tmp; }
Show how XCHG can be used instead of test-and-set to implement the acquire() and release() functions of the spinlock data structure described in the "Synchronization" lecture.
struct lock { ... } void acquire (struct lock *) { ... } void release (struct lock *) { ... }
Consider the following test program for an implementation of KThread.join in Nachos. It begins when the main Nachos thread calls KThread.selfTest. You do not need to know the details of how join is implemented. All you need to know is that when a parent thread calls join on a child thread, the parent does one of two things: (1) if the child is still running, the parent blocks until the child finishes (at which point the parent is placed on the ready queue); (2) if the child has finished, the parent continues to execute without blocking. Assume join uses a wait queue of some kind in its implementation.
private static class A implements Runnable { A () {} public void run () { KThread t2 = new KThread (new B()).setName ("B"); System.out.println ("foo"); t2.fork (); System.out.println ("far"); t2.join (); System.out.println ("fum"); } } private static class B implements Runnable { B () {} public void run () { System.out.println ("fie"); } } public static void selfTest() { KThread t1 = new KThread (new A()).setName ("A"); System.out.println ("fee"); t1.fork (); System.out.println ("foe"); t1.join (); System.out.println ("fun"); }
Assume that the scheduler runs threads in FIFO order with non-preemptive scheduling (no preemptive time-slicing), and threads are placed on wait queues in FIFO order. Trace the execution of this program until it returns from selfTest and (a) write the sequence of context switches that occurred up this point, (b) write the output of the program, and (c) list the queues that the threads are on, and their relative order if more than one thread is on a queue.
a. Context switches: main →
b. Output:
c. Thread queues when selfTest returns:
currentThread:
readyQueue:
join wait queue:
[Hint: First try the problem by following the code manually, keeping track of which queues threads are on using paper. Then, once you have implemented join, try adding the code as a test in KThread.java and running it to check your answer.]
a. Use pseudocode to write a monitor that implements Barrier.
monitor Barrier { ...private variables... void Done (int n) { ... } ... }
b. Use pseudocode to implement Barrier using an explicit lock and condition variable. The lock and condition variable have the semantics described at the end of the "Semaphore and Monitor" lecture in the ping_pong example, and as implemented by you in Project 1.
class Barrier { ...private variables... void Done (int n) { ... } ... }
You need only use pseudocode in your answers. Your pseudocode does not have to compile, and you can use whatever syntax you are most comfortable with (the solutions use the Nachos syntax). But it does have to look like code.
The Surfing class can be in one of two states, either breaking or calm. Surfer threads invoke the paddle method to indicate the direction, left or right, they would like to surf the break. When calm, surfer threads block until the next wave arrives. When breaking, surfer threads block if the wave is not breaking in their direction, and otherwise return immediately.
The ocean thread invokes the wave method indicating the direction the next wake is breaking (left, right, or both ways). It changes the state to breaking and wakes up all surfer threads waiting to catch waves in that direction, or wakes up all threads if the wave is breaking in both directions. It invokes the done method to indicate that the wave is finished breaking, changing the state back to calm. The ocean thread alternates invoking wave and done, and the state is initially calm.
class Surfing { enum State { calm, breaking; } enum Direction { LEFT, RIGHT, BOTH; } ...private variables... Surfing () { ... } void paddle (Direction dir) { // invoked by surfer threads ... } void wave (Direction dir) { // invoked by the ocean thread ... } void done () { // invoked by the ocean thread ... } }
Consider the following state:
Is the system deadlocked in this state? Explain using
a resource allocation graph as a reference.