CS 162 Homework Assignment #1: Context Switching

Last updated February 9, 2001 by Matt Welsh, mdw@cs.berkeley.edu

The point of this assignment is to make sure you really understand the concept of context switching. Write down the sequence of method calls and returns, along with changes to the ready list, that would occur in Nachos if the "main" thread in Nachos were to run the KThread.SelfTest2 code listed below.

For each method call/return, give the name of the thread doing the call/return, the full method name, and the values of any significant arguments. You only have to list calls and returns to routines defined in KThread.java and TCB.java, along with calls to Machine.interrupt().disable() or enable(). In other words, you can ignore calls to assertion and debug calls, calls made internal to the interrupt simulation, any trivial functions, etc. You should also assume that timer interrupts are disabled -- that is, once a thread is running, it will not be preempted. (This does not mean that all interrupts are disabled however.) Assume that only the 'main' thread is on the ready queue when KThread.SelfTest2 is called.

It is acceptable (even encouraged) for you to generate your solution automatically. The code to be run is:

void KThread.SelfTest2() {

  // Create a Runnable for each thread and define its 'run()' method
  // Basically this means when the thread starts, it immediately
  // calls 'yield()' and exits. This is just a syntactic shortcut!
  Runnable myrunnable1 = new Runnable() {
   public void run() { yield(); }
  };

  Runnable myrunnable2 = new Runnable() {
   public void run() { yield(); }
  };

  KThread t1 = new KThread(myrunnable1);
  t1.setName("child 1");

  KThread t2 = new KThread(myrunnable2);
  t2.setName("child 2");

  t1.fork();
  t2.fork();

  yield();

}

For example, your solution should begin:
Ready List Thread Method Call
main main CALL KThread.SelfTest2()
main main CALL Runnable.Runnable() /* Create myrunnable1 */
main main RETURN FROM Runnable.Runnable()
main main CALL Runnable.Runnable() /* Create myrunnable2 */
main main RETURN FROM Runnable.Runnable()
main main CALL KThread.KThread(myrunnable1)