preview

Midterm 2 Solutions Essay

Better Essays

CSCI 4061: Introduction to Operating Systems Fall 2008 Mid-Term Exam II Sample Solution NAME: STUDENT ID: General Instructions: • Write your name and student ID clearly above. • You have 1 hour and 15 minutes to write the exam. No extra time will be given. • There are 4 questions in the exam, all with subparts. The questions combine for a maximum of 100 points. • You must write your answers clearly in the space provided for each question. You might use the backside of each page, as well as any additional sheets as required. If you are using additional space, you must clearly label the question no. that you are answering. Any loose sheets must have your name and student ID written clearly. • The exam is open book/open notes, however, …show more content…

The threads run concurrently, and their order of execution or the interleaving of their instructions is non-deterministic. For each of the following, show how you will modify the code for thread i using semaphores to achieve the desired execution behavior. Note: For each semaphore that you use, show where you will add its wait and/or signal operations, and also specify its initial value. Also Note: You can use pseudocode instead of POSIX/C syntax for your solution. (a) (6 pts) Have each thread execute its code (both foo and bar) in a mutually exclusive manner. The order in which the threads execute does not matter. Ans: This is a classical critical section problem, and we basically need a mutex lock here. Recall that a semaphore with initial value of 1 can be used identically to a mutex lock (since it allows only 1 thread to be in the critical section at a time). So the solution is as follows. Declare a global semaphore: semaphore sem=1; Code for thread i: wait(sem); foo(i); bar(i); signal(sem); (b) (12 pts) Have each thread execute foo in a mutually exclusive manner, but allow upto 5 of them to execute bar concurrently. The order in which the threads execute does not matter. Ans: Here, executing foo is again a classical critical section problem, that can be solved similar to part (a). However, executing bar allows multiple threads to be in the critical section, and this can be achieved by initializing the semaphore

Get Access