It325 Assignment 7

Only available on StudyMode
  • Topic: Death Penalty, Mutual exclusion, Sleeping barber problem
  • Pages : 6 (1251 words )
  • Download(s) : 113
  • Published : February 18, 2013
Open Document
Text Preview
King Saud University
College of Computer & Information Sciences
Information Technology Department
IT 325: Operating Systems

Assignment 7
first Semester 1433/1434H – 2012/2013

Q1) The following is a set of three interacting processes that can access two shared semaphores:
semaphore U = 2;
semaphore V = 0;

[Process 1] [Process 2] [Process 3]

L1:wait(U) L2:wait(V) L3:wait(V)
type("B") type("D") type("A")
signal(V) type("C") goto L3
goto L1 signal(V)
goto L2
Within each process the statements are executed sequentially, but statements from different processes can be interleaved in any order that's consistent with the constraints imposed by the semaphores. When answering the questions below assume that once execution begins, the processes will be allowed to run until all 3 processes are stuck in a wait() statement, at which point execution is halted. A. Assuming execution is eventually halted, how many A's are printed when this set of processes runs? B. Assuming execution is eventually halted, how many B's are printed when the set of processes runs? C. What is the smallest number of D's that might be printed when this set of processes runs?

D. Is BDCDCAABDCBDCA a possible output sequence when this set of processes runs? E. Is BDCDBACBDCAA a possible output sequence when this set of processes runs?

Yes: start: U=3 V=0
type C: U=2 V=1
type A: U=2 V=0
type B: U=2 V=1
type A: U=2 V=0
type C: U=1 V=1
type D: U=1 V=0
type B: U=1 V=1
type C: U=0 V=2
type A: U=0 V=1
type B: U=0 V=2
type D: U=0 V=1
type D: U=0 V=0
Q2) X, Y and Z are shared semaphores.
X = 1
Y = 0
Z = 0

The following 4 pseudo-coded threads are started. What is the output? Do all threads complete execution?

Thread 1| Thread 2| Thread 3| Thread 4|
wait(X)print "4"signal(Y)signal(Y)| wait(Z)print "3"signal(Z)signal(Y)signal(X)| wait(Y)print "2"signal(X)signal(X)| wait(Y)wait(X)print "1"|

Q3) Answer the following questions with the pseudo-code below: i) What is the purpose of semaphore a?
ii) What is the purpose of semaphore b?
iii) What is the purpose of semaphore c?
iv) What is the purpose of semaphore d?

Q4) Consider the following proposed solution to the dining philosophers problem:

semaphore fork[5] = 1,1,1,1,1; /* each semaphore is initialized to 1 */ philosopher (int i) /* philosopher i=0,1,2,3,4 executes this code */ {
while(1) {
think();
wait(fork[i]);
wait(fork[(i+1)%5]);
eat();
signal(fork[(i+1)%5]);
signal(fork[i]);

}
}
The above solution may lead to deadlock. Add a single semaphore to make the solution deadlock free. Be sure to indicate the initial value of the semaphore.

Answer:
Q1) The following is a set of three interacting processes that can access two shared semaphores:
semaphore U = 2;
semaphore V = 0;

[Process 1] [Process 2] [Process 3]

L1:wait(U) L2:wait(V) L3:wait(V)
type("B") type("D") type("A")
signal(V) type("C") goto L3
goto L1 signal(V)
goto L2
Within each process the statements are executed sequentially, but statements from different processes can be interleaved in any order that's consistent with the constraints imposed by the semaphores. When answering the questions below assume that once execution begins, the processes will be allowed to run until all 3...
tracking img