Professional Documents
Culture Documents
Problem Solving 3 2023 MechCSoalan
Problem Solving 3 2023 MechCSoalan
Question 1
1: int sum = 0;
2:
3: void add2(){
4: int local;
5: local = sum;
6: sum = local + 2;
7: }
8:
9:
void main(){
10:
cobegin {
11:
12:
add2();
13: add2();
14: }
15: cout << "Sum = " << sum << endl;
16: }
Figure Q3.1
b. The program can produce two different outputs: Sum = 4 and Sum = 2. Illustrate the
execution traces of the two processes by showing the program statements execution sequence
of the two processes.
[5 marks]
c. If we implement the two processes in two different processors, does it solve the problem in
question b.? Explain your answer.
[4 marks]
d. Rewrite the program in Figure Q3.1 to satisfy the mutual exclusions requirement. Describe
how your program satisfies the requirement. [4 marks]
e. Give and explain THREE (3) fundamental facilities to realize CP? The following code
segment in Figure Q3.2 shows how Java supports one of the fundamental facilities using the
case study. Explain the code and describe the how the concurrent tasks are supported in Java
language.
Figure Q3.2
[4 marks]
f. Rewrite your solution in d. to satisfy the mutual exclusions requirement using Java
concurrent language. Describe how your program satisfies the requirement.
[5 marks]