CS560 Midterm Exam -- Answers and Grading


Question 2 -- 12 points

Answer

Longjmp #1: This is called from kt_exit(), and returns to the setjmp() statement at line 140. The purpose is so that when a thread (besides the main one, of course) exits, the stack will be popped down so that it looks like the thread has returned from its initial procedure call. In this way, the same code (the code starting at line 146) can be executed to clean up an exiting thread, regardless of whether the thread exited with kt_exit() or by returning from its initial procedure call.

Longjmp #2: This is called in the scheduler at line 117. It is called when the scheduler has selected a new thread to run, and this is a thread that has blocked for some reason. The longjmp() puts us onto this thread's stack, and returns from the setjmp() call at line 75. When it returns at line 75, it is on the correct stack, and therefore returns (line 78), having the effect of making the blocking call unblock.

Longjmp #3: This is called in the scheduler at line 134. This is the one that starts a new thread running. Setjmp() is called at line 121, but on an old thread's stack. The next few lines (123 to 131) set the stack pointer and the frame pointer in the jump buf's so that they will define a new base frame on the new thread's stack. Finally the longjmp() returns from the setjmp() at line 121, but on a new clean stack so that the thread's procedure can be exectued (line 142).


Grading

Each longjmp() was worth four points.