Uploaded by Ahmet Rizaner

Conc Exercises

advertisement
Department of Information
Technology
School of Computing and Technology
Eastern Mediterranean University
ITEC202
Exercises
Operating Systems
Concurrency & Synchronization
Ex1.
Add the semaphores necessary to synchronize processes A, B, C, D, and E so that process A must finish
executing before B starts, process B must finish before C and D start, and process D must finish before
process E starts. Use the notation increment(s) and test(s) to write your solution. Show your solution.
Remember to indicate the initial value of each semaphore.
Solution:
The relations among processes can be represented in the following diagrams.
C
S2
A
S1
B
S3
D
S4
E
The program can be as follows:
semaphore S1, S2, S3, S4 = 0, 0, 0, 0;
Process A:
- do work of A
increment(S1);
/* Let B start */
Process B:
test(S1); /* Block until A is finished */
- do work of B
increment(S2); /* Let C start */
increment(S3); /* Let D start */
Process C:
test(S2); /* Block until B is finished */
- do work of C
Process D:
test(S3); /* Block until B is finished */
- do work of D
singal(S4); /* Let E start */
Process E:
test(S4); /* Block until D is finished */
- do work of E
Ex2.
Use message passing to synchronize processes A, B, C, D, and E so that process A must finish before B
or C starts, process B must finish before D starts, process C and D must finish before E starts. Use the
notation send(mbox,msg) and receive(mbox,msg) to write your solution.
Solution:
The relations among processes can be represented in the following diagrams.
A
mbox
1
B
mbox
2
D
C
mbox
3
E
The program can be as follows:
Process A:
- do work of A
send(mbox1, msgA) /* Let B/C start */
send(mbox1, msgA) /* Let B/C start */
Process B:
read(mbox1, msgA); /* Block until A is finished */
- do work of B
send(mbox2, msgB); /* Let D start */
Process C:
read(mbox1, msgA); /* Block until A is finished */
- do work of C
send(mbox3, msgC); /* Let C start */
Process D:
read(mbox2, msgB)
- do work of D
send(mbox3, msgD);
Process E:
read(mbox3, msgC);
read(mbox3, msgD);
- do work of E
/* Block until B is finished */
/* Let E start */
/* Block until C is finished */
/* Block until D is finished */
Ex3.
Consider the following program segments for two different processes (P1, P2) executing concurrently and
where B and A are not shared variables, but x starts at zero and is a shared variable.
Process #1
Process #2
x=0;
for (a=1; a<=3; a++)
x=x+1;
x=0;
for (b=1; b<=3; b++)
x=x+1;
If processes P1 and P2 executes only once at any speed, what are the possible resulting values of x?
Explain your answer.
Solution:
If P l and P2 start at the same time with X=0 then, no matter which one finishes first, the answer will be 6.
An alternate scenario would be to reset X to zero before either P1 or P2 started execution. That is, include
X=0 as the first line of both P1 and P2. Then the results will vary depending on the speed of each process.
For example, suppose that P1 is faster and finishes before P2 starts up, then the answer would be 3
because X is reset to zero upon starting up P2. You could end up with 4 or 5 as the answer depending on
when the second process started executing.
Download