King Saud University College of Computer & Information Sciences Information Technology Department IT 325: Operating Systems Assignment 6 Summer Semester 1433/1434H – 2012/2013 Q1) The following is a set of three interacting processes that can access two shared semaphores: semaphore U = 3; semaphore V = 0; [Process 1] [Process 2] [Process 3] L1:wait(U) type("A") signal(V) goto L1 L2:wait(V) type("B") type("C") signal(V) goto L2 L3:wait(V) type("D") goto L3 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 the set of processes runs? B. Assuming execution is eventually halted, how many D's are printed when this set of processes runs? C. What is the smallest number of B's that might be printed when this set of processes runs? D. Is ADBCAABCDD a possible output sequence when this set of processes runs? E. Is ABCBADCDBCAD a possible output sequence when this set of processes runs? 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 wait(X) X=0 print "4" signal(Y) Y=1 signal(Y) Y=2 Thread 2 wait(Z) STUCK print "3" signal(Z) signal(Y) signal(X) Thread 3 wait(Y) Y=1 print "2" signal(X) X=1 signal(X) X=2 Thread 4 wait(Y) Y=0 wait(X) X=1 print "1" Q3) Answer the following questions with the pseudo-code below: program producer_consumer; semaphore s1=0; semaphore s2=1; semaphore s3=0; semaphore s4=10; producer() { for (i=0; i<50; i++){ wait(s4); wait(s2); produce; signal (s2); signal (s3); }//end of for loop wait (s1); remove all semaphores } main() { producer(); consumer(); } i) What is the purpose of semaphore s1? ii) What is the purpose of semaphore s2? iii) What is the purpose of semaphore s3? iv) What is the purpose of semaphore s4? consumer () { for (i=0; i<50; i++){ wait(s3); wait(s2); consume; signal (s2); signal (s4); }//end of for loop signal (s1); }