Answers to Question 2 (11 points, 20 minutes)
Part 1 (8 points)
If you understood setjmp()/longjmp(), then this part should have
been a straightforward trace through the program.
- The program starts up and calls setjmp(), which returns zero.
Thus, i and j are both zero:
i = 0. j = 0
Calling fork()
- Now, fork() is called. The parent (lets call it process
1000) waits, and the child (process 1001) takes over. The child
increments j and prints out i and j:
i = 0. j = 1
- Since i+j is odd and less than four, process 1001 calls
longjmp(i+1), which effects a return from the setjmp()
with a value of one. You may wonder if this can work, since the child
never explicitly called setjmp(). The answer is yes, because the
parent's address space, including the stack, was duplicated in the fork()
call. Once process 1001 returns from setjmp(), i is 1 and
j is 1:
i = 1. j = 1
Calling fork()
- Again, fork() is called. Process 1001 waits and
process 1002 takes over. Process 1002
increments j and prints out i and j:
i = 1. j = 2
- Since i+j is odd and less than four, process 1002 calls
longjmp(i+1), which effects a return from the setjmp()
with a value of two:
i = 2. j = 2
Calling fork()
- Once again, fork() is called. Process 1002 waits and
process 1003 takes over. Process 1003
increments j and prints out i and j:
i = 2. j = 3
- Now, i+j is greater than 4, so process 1003 exits:
2 3 Exiting
- Then, process 1002's wait() call returns, and since i+j
is even, it exits:
2 2 Exiting
- And the same thing happens to processes 1001 and 1000, and the program
terminates:
1 1 Exiting
0 0 Exiting
Thus, the program's output looks like:
i = 0. j = 0
Calling fork()
i = 0. j = 1
i = 1. j = 1
Calling fork()
i = 1. j = 2
i = 2. j = 2
Calling fork()
i = 2. j = 3
2 3 Exiting
2 2 Exiting
1 1 Exiting
0 0 Exiting
Grading
It's hard to quantify how to grade this question. Basically, there
were two kinds of answers -- those that were close to right, and those
that were nowhere close to right. When grading questions that were
close to right, I started with 8 points and deducted for things that
were wrong. When grading questions that were nowhere close to right,
I started with 0 points and added for things that looked good.
Part 2 (3 points)
Yes, the output would be different. This is because the buffers for
standard output are not flushed at the end of a line. Thus, each
process would have all the output from its ancestor
processes to that point in its buffer. I.e.:
- Process 1003 will print out:
i = 0. j = 0
Calling fork()
i = 0. j = 1
i = 1. j = 1
Calling fork()
i = 1. j = 2
i = 2. j = 2
Calling fork()
i = 2. j = 3
2 3 Exiting
- Process 1002 will print out:
i = 0. j = 0
Calling fork()
i = 0. j = 1
i = 1. j = 1
Calling fork()
i = 1. j = 2
i = 2. j = 2
Calling fork()
2 2 Exiting
- Process 1001 will print out:
i = 0. j = 0
Calling fork()
i = 0. j = 1
i = 1. j = 1
Calling fork()
1 1 Exiting
- And process 1000 will print out:
i = 0. j = 0
Calling fork()
0 0 Exiting
This gives the following output:
i = 0. j = 0
Calling fork()
i = 0. j = 1
i = 1. j = 1
Calling fork()
i = 1. j = 2
i = 2. j = 2
Calling fork()
i = 2. j = 3
2 3 Exiting
i = 0. j = 0
Calling fork()
i = 0. j = 1
i = 1. j = 1
Calling fork()
i = 1. j = 2
i = 2. j = 2
Calling fork()
2 2 Exiting
i = 0. j = 0
Calling fork()
i = 0. j = 1
i = 1. j = 1
Calling fork()
1 1 Exiting
i = 0. j = 0
Calling fork()
0 0 Exiting
Grading
To get full credit, you had to say something equivalent to the first
three sentences of this answer. You did not have to give any output.