Assignment-2 (TCS/TIT 503) Topics covered: Threads, Process Synchronisation, Semaphores Submission dates: 1)CS A:Monday,26/10/2015 (Time: between 12 pm to 1 pm) 2)CS B:Monday,26/10/2015(in the lecture of OS) 3)IT:Monday,26/10/2015 (Time: between 12 pm to 1 pm in the cabin of Mr. Dilip kumar) The assignments will not be submitted after the due timings on the same day also(applicable for all sections). Q1) Can a multithreaded solution using multiple user-level threads achieves better performance on a multiprocessor system than on a single-processor system? Q2) What can be the circumstances under which a multithreaded solution using multiple kernel threads provide better performance than a single-threaded solution on a singleprocessor system? Q3) A web server is implemented using the following 2 approaches. Approach 1: For each connection request, create a new separate process to handle that request. Approach 2: A single process is running on server to listen the incoming request and then for each connection request, a new thread is created. The web server has 1 GB memory space in total. Each process which is created on server takes 10 MB of memory space. Each thread takes 100 KB of memory space. a) Find the maximum number of connections which can be created in approach 1 and approach 2, assuming no memory space is taken by OS on server. b) Repeat the part (a), assuming 2 MB of memory space is taken by OS on server. c) Which of the approach: approach 1 or approach 2 seems to be better and why?
Q4) 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) L2:wait(V) L3:wait(V) printf("c") printf("a") printf("d") signal(v) printf("b") 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 is 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 C'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 A's that might be printed when this set of processes runs? d) Is CABABDDCABCABD a possible output sequence when this set of processes runs? e) Is CABACDBCABDD a possible output sequence when this set of processes runs? f) Is it possible for execution to be halted with either U or V having a non-zero value? Q5) The following pair of processes share a common variable X: Process A Process B int Y; int Z; A1: Y = X*2; B1: Z = X+1; A2: X = Y; B2: X = Z; X is set to 5 before either process begins execution. As usual, statements within a process are executed sequentially, but statements in process A may execute in any order with respect to statements in process B. A. How many different values of X are possible after both processes finish executing?
B. Suppose the programs are modified as follows to use a shared binary semaphore S: Process A Process B int Y; int Z; wait(s); wait(s); A1: Y = X*2; B1: Z = X+1; A2: X = Y; B2: X = Z; signal(s); signal(s); S is set to 1 before either process begins execution and, as before, X is set to 5. Now, how many different values of X are possible after both processes finish executing? C. Finally, suppose the programs are modified as follows to use a shared binary semaphore T: Process A Process B int Y; int Z; A1: Y = X*2; B1: wait(t); A2: X = Y; B2: Z = X+1; signal(t); X = Z; T is set to 0 before either process begins execution and, as before, X is set to 5. Now, how many different values of X are possible after both processes finish executing? Q6) The following pair of processes share a common set of variables: "counter", "tempa" and "tempb": Process A Process B...... A1: tempa = counter + 1; B1: tempb = counter + 2; A2: counter = tempa; B2: counter = tempb;...... The variable "counter" initially has the value 10 before either process begins to execute. A. What different values of "counter" are possible when both processes have finished executing? Give an order of execution of statements from processes A and B that would yield each of the values you give. For example, execution order A1, A2, B1, B2 would yield the value 13.
B. Modify the above programs for processes A and B by adding appropriate signal and wait operations on the binary semaphore "sync" such that the only possible final value of "counter" is 13. Indicate what should be the initial value of the s emaphore "sync". D. Modify the original programs for processes A and B by adding binary semaphores and signal and wait operations to guarantee that the final result of executing the two processes will be counter = 11. Give the initial values for every semaphore you introduce. Q7) Write the producer-consumer solution using monitors with the explanation. Q8) Both busy waiting and blocking methods can be used as means to address critical section problems and process synchronization. Describe how blocking is different from busy waiting, and discuss the advantages and disadvantages of each. Q9) Do semaphores solve the problem of process synchronization completely? What are some of the things that might still go wrong in a solution that requires synchronization using semaphore? Q10) Consider the following program fragment: semaphore s1=1,s2=1; wait(s1); a++; wait(s2); v++; wait(s2); wait(s1); (s1, s2 are counting semaphores). Now, consider two processes(or threads ) running this fragment of code simultaneously, can there be a deadlock? Why, or why not? Q11) Consider the following code: shared int flag[2]=0,0, turn=0; Process 1: Process 2: flag[0]=1; turn=1; while(flag[1]==1&&turn==0) ; critical section; flag[0]=0; flag[1]=1; turn=1; while(flag[0]==1&&turn==1) ; critical section; flag[1]=0;
check the following code and tell which of the 3 requirements (Mutual exclusion, bounded waiting, progress) is violated. Q12) Answer the following semaphore regarding counting semaphore. Give brief explanation for each. a)can there ever be a process blocked one semaphore with non negative value? b)can a semaphore have negative value without having any process blocked on it? Q13) Two processes X and Y need to access a critical section. Consider the following synchronization construct used by both the processes Process X: Process Y: / *other codefor process X * / while (true) P= true; Q= true; while (Q== true) / *criticalsection*/ P=false; / *other codefor process X*/ / *other codefor process Y * / while (true) Q= true; P= true; while (P== true) / *criticalsection*/ Q=false; / *other codefor process Y*/ Here, P and Q are shared variables and both are initialized to false. Which one of the following statements is true? (A) The proposed solution prevents deadlock but fails to guarantee mutual exclusion (B) The proposed solution guarantees mutual exclusion but fails to prevent deadlock (C) The proposed solution guarantees mutual exclusion and prevents deadlock (D) The proposed solution fails to prevent deadlock and fails to guarantee mutual exclusion Give proper explanation for your answer.
Q14) The following program consists of 3 concurrent processes and 3 binary semaphores. The semaphores are initialized as S0=1, S1=0, S2=0. Process P0 Process P1 Process P2 while (true) wait (S0); print (0); release (S1); release (S2); wait (S1); Release (S0); wait (S2); release (S0); How many times will process P0 print '0'? (a) At least twice (b) Exactly twice (c) Exactly thrice (d) Exactly once Give proper explanation for your answer. Q15) Consider a multiprocessor system and a multithreaded program written using the many-to-many threading model. Let the number of user-level threads in the program be more than the number of processors in the system. Discuss what will happen performance wise in the following scenarios. a. The number of kernel threads allocated to the program is less than the number of processors. b. The number of kernel threads allocated to the program is equal to the number of processors. c. The number of kernel threads allocated to the program is greater than the number of processors but less than the number of user level threads. Q16) A single-lane bridge connects the two villages,village A and Village B. Farmers in the two villages use this bridge to deliver their produce to the neighbouring town. The bridge can become deadlocked if both a northbound and a southbound farmer get on the bridge at the same time (Both village farmers are stubborn and are unable to back up). Using exactly one semaphore, design an algorithm that prevents deadlock. Do not be concerned about starvation and inefficiency. Q17) If the wait() and signal() semaphore operations are not executed atomically, then can mutual exclusion be violated? Why or why not?