Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Problem Solving 3: Concurrency Programming

Question 1

Consider the following concurrent program in C--.

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

a. The above concurrent program is implemented on a uniprocessor multiprogramming system.


Identify the form of concurrency that occurs here. Justify your answer.
[3 marks]

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.

1: public class Proc1 implements Runnable


2: {
3: public void run()
4: {
5: // code for proc2
6: }
7: }
8:
9: Proc1 P1 = new Proc1();
10:
12: Thread Task1 = new Thread(P1);
13: Thread Task2 = new Thread(P1);
14:
15: Task1.start();
16: Task2.start();
17:

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]

You might also like