Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch 7 B.

Similar presentations

Presentation on theme: "Ch 7 B."— Presentation transcript:

1 Ch 7 B

2 Q 7.1 What is the meaning of the term busy waiting?
a process is waiting for a condition to be satisfied in a tight loop without relinquishing the processor. Alternatively, a process could wait by relinquishing the processor, and block on a condition (e.g., I/O, semaphore) and wait to be awakened at some appropriate time in the future. Can busy waiting be avoided altogether? Explain your answer?. Busy waiting can be avoided but increase the overhead putting a process to sleep and having to wake it up when the appropriate program state is reached.

3 Semaphore The Semaphore class provides the following operations: Semaphore(char* debugName, int initialValue) void wait(S) void signal() void wait() Decrement the semaphore's count, blocking the caller if the count is zero. Increment the semaphore's count, releasing one thread if any are blocked waiting on the count.

4 The typical way to use semaphore
When programming, to define a semaphore, just simply: Semaphore sema(“choose_a_name”, 1); Then, use wait() and signal() to implement Mutual Exclusion void example() { // do something sema.wait(); // do something here in critical section sema.signal(); //… return; }

5 Question 7.7 Show that, if the wait() and signal() operations are not executed atomically, then mutual exclusion may be violated. Answer Suppose the value of semaphore S = 1 and processes P1 and P2 execute wait(S) concurrently. a. T0: P1 determines that value of S =1 b. T1: P2 determines that value of S =1 c. T2: P1 decrements S by 1 and enters critical section d. T3: P3 decrements S by 1 and enters critical section

6 Question Consider this solution to the readers-writers problem
do { wait (wrt) ; // writing is performed signal (wrt) ; } while (true) wait (mutex) ; readcount ++ ; if (readcount == 1) wait(wrt) ; signal (mutex) // reading is performed readcount - - ; if (readcount == 0) signal(wrt) ; signal (mutex) ; What is the purpose of the semaphore “wrt”? To guarantee mutual exclusion to the critical section What is the purpose of the semaphore “mutex”? To guarantee mutual exclusion when updating the shared variable readcount Suppose a writer process is inside its critical section, while another writer and n readers are waiting outside their critical sections. Which semaphores are they waiting on, respectively? the writer is waiting on wrt , the 1st reader is waiting on wrt and the other n-1 readers are waiting on mutex

7 Question Please correct all errors in the following solution for the Bounded-Buffer problem. The buffer has size N, and is initially empty?. Initilization semaphore mutex, empty, full; mutex=1; empty=0; full=N; //Should be empty=N //Should be full=0 Producer Consumer do{ // Produce an item in nextp wait(mutex) wait(empty) // Add nextp to buffer …. signal (mutex); signal (full); }while(true); wait (mutex); wait (full); // remove an item from buffer to nextc signal (empty); // consume the item in nextc } while(true); //Order should be switched //Order should be switched

8 Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name { shared variable declarations procedure body P1 (…) { . . . } procedure body P2 (…) { procedure body Pn (…) { initialization code Operating System Concepts

9 Monitors To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y; Condition variable can only be used with the operations wait and signal. The operation x.wait(); means that the process invoking this operation is suspended until another process invokes x.signal(); The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect. Operating System Concepts

10 Monitor with condition variables

11 Question The signal() operation is used with semaphores and monitors. Explain the key difference in its runtime behavior in the two cases. (Hint: consider how this affects the wait() operation in another process) ? Monitor When the signal() operation is used in monitors, if a signal is performed and if there are no waiting processes, the signal is simply ignored and the system does not remember the fact that the signal took place. If a subsequent wait operation is performed, then the corresponding thread simply blocks. Semaphore In semaphores, on the other hand, every signal results in a corresponding increment of the semaphore value even if there are no process waiting. A future wait() operation could immediately succeed because of the earlier increment

12 Question If several processes are suspended on condition x and and x.singnal() operation is executed by some process , how we determine which suspended process should be resumed next FCFS Conditional wait construct X.wait(c) , where c is a priority number for each process

13 Monitor to allocate single resource
monitor ResourceAllocation { boolean busy; condition x; void acquire(int time) { if(busy) x.wait(time); busy =true; } void release() { busy = false; x.signal(); Void init(){

14 Question 7.14 Consider a system consisting of processes P1, P2, ..., Pn, each of which has a unique priority number. Write a monitor that allocates three identical line printers to these processes, using the priority numbers for deciding the order of allocation

15 7.14 Answer type resource = monitor var P: array[3] of boolean; X: condition; procedure acquire (id: integer, printer-id: integer); begin if P[0] and P[1] and P[2] then X.wait(id) if not P[0] then printer-id := 0; else if not P[1] then printer-id := 1; else printer-id := 2; P[printer-id]:=true; end procedure release (printer-id: integer) begin P[printer-id]:=false; X.signal; end begin P[0] := P[1] := P[2] := false; end

Download ppt "Ch 7 B."

Similar presentations

Ads by Google