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.
- Longjmp #1:
- You got one point if you correctly said which setjmp() it
returned to.
- You got one point if you mentioned cleaning up a thread.
- You got up to two more points for a good precise description,
including why we longjmp() rather than simply cleaning
stuff up in kt_exit() and then calling kt_sched().
- Longjmp #2:
- You got three points for a good description.
- You got one point if you correctly said which setjmp() it
returned to.
- You got one point if you mentioned unblocking/scheduling a blocked thread.
- You got up to two more points for a good precise description, including
the fact that the blocked thread will now wake up.
- Longjmp #3:
- You got three points for a good description.
- You got one point if you correctly said which setjmp() it
returned to.
- You got one point if you mentioned initializing a new thread.
- You got up to two more points for a good precise description,
including why we need to longjmp() so that we change the
stack and frame pointers to the new thread's stack.