150 likes | 275 Views
Ch 7 B. 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.
E N D
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.
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. • void signal() • Increment the semaphore's count, releasing one thread if any are blocked waiting on the count.
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; }
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 =1b.T1: P2 determines that value of S =1c.T2: P1 decrements S by 1 and enters critical sectiond.T3: P3 decrements S by 1 and enters critical section
Question • Consider this solution to the readers-writers problem • 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
Question Please correct all errors in the following solution for the Bounded-Buffer problem. The buffer has size N, and is initially empty?. //Should be empty=N //Should be full=0 //Order should be switched //Order should be switched
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 bodyP1(…) { . . . } procedurebodyP2 (…) { . . . } procedure bodyPn(…) { . . . } { initialization code } } Operating System Concepts
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
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
Question • If several processes are suspended on condition x and andx.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
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(){ busy = false; } }
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
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