2. Tools for Implementationof Semaphores #3 Compare & Swap Instructions Helps consistent update of global variable Implementation Compare Oldreg, Globvar Set condition codes If (Oldreg=Globvar) Then Globvar Newreg Else OldregGlobvar MIT CS704D Advanced OS Class of 2011 2
3. Queuing Implementationof Semaphore MIT CS704D Advanced OS Class of 2011 3 Semaphore Pz ….. Py Px Wait(s): If not (s>0) then suspend caller at s else s:= s+1 Signal (s): if queue is not empty (at least one process is waiting) then resume process from the queue at s else s:=s+1
4. Overview of Classical Synchronization problems Producers and consumers With unbounded buffers With bounded buffers Readers and writers MIT CS704D Advanced OS Class of 2011 4
5. Producers & Consumers One class of processes produce data items The other class consumes/uses this data They may have different rates and thus cause synchronization problems Synchronizations required so that producers & consumers are able to operate concurrently Such that items produced are consumed in the same order Display, keyboard was an example Processes may be a combination of both producer and consumer MIT CS704D Advanced OS Class of 2011 5
6. Producers & Consumers(unbounded buffer case) If we can prevent a process trying to consume something before at least one item is available, sync is achieved A semaphore “Producer” can take care of that We assume buffer manipulation does not cause problems This is not really a valid assumption in multiple producer, consumer situations MIT CS704D Advanced OS Class of 2011 6
7. Unbounded Buffer Case A mutex controlling buffer access can manage the situation well How the mutex is used exactly can have unintended implications If the waiting on “producer” is placed within the critical section, there can be deadlocks Initially, for example, when nothing has been produced and a consumer is scheduled, the consumer will get through to the critical section Then wait forever on Producer as a producer process cannot get into the critical section MIT CS704D Advanced OS Class of 2011 7
8. Producers & Consumers(bounded buffer case) Additional management issues are that the buffers are to be controlled Producers should not produce when buffer is full, it will overwrite some existing data Consumers, similarly will consume wrong data if buffer is empty These conditions have to be controlled MIT CS704D Advanced OS Class of 2011 8
9. Unbounded case Icount=produced-consumed Necessary that icount cannot be less than zero and more than the capacity Then Condition mayproduce : icount < capacity as also mayconsume: icount>0 MIT CS704D Advanced OS Class of 2011 9
10. Readers & Writers Readers and consumers are processes that operate against some common data structure Readers are pure readers, only reads parts or all of the data structure Writers write and thus modify the data structure. It can also read the structure or parts of it Readers thus can safely get interleaved with other readers But writes cannot be interleaved with other readers or writers MIT CS704D Advanced OS Class of 2011 10
11. The sync Problem Given a universe of readers that read a common data structure, and a universe of writers that modify the same common data structure A sync mechanism needs to be devised to control readers and writers to ensure consistency of common data and maintain as high a concurrency as possible MIT CS704D Advanced OS Class of 2011 11
12. Example Synchronization High concurrency Allows high number of readers to access common resource Writer waits for the wait semaphore Reader process makes it possible for multiple readers to work Readercount really tracks if even one reader is active Next round, the writer gets turn only when all readers finished reading, that may be unfair MIT CS704D Advanced OS Class of 2011 12
13. Suggested Modifications(according to C A R Hoare) A reader should not start if there’s a writer waiting, preventing starvation for writers All readers waiting at the end of a write cycle should be given priority, preventing starvation of readers MIT CS704D Advanced OS Class of 2011 13
15. Semaphore Problems Sync and system integration depend on strict following of the discipline and implementation. Forgetting of either of wait and signal mechanism, reversing or going around it will cause problems in the system Semaphores control access to shared resources but cannot prevent misuse of the same by some process granted access to these global variables. MIT CS704D Advanced OS Class of 2011 15
16. Critical Regions & Conditional critical Regions Strong typing and compile time checks can prevent some of the problems For example var mutex : shared T; and critical section as follows region mutex do The compiler can ensure wait and signals are introduced properly, no probability of errors MIT CS704D Advanced OS Class of 2011 16
17. Why condionality required Sometimes a process getting access to the CS may still need some condition to be fulfilled and thus block other processing entering the CS Conditional CS construct can prevent such problems MIT CS704D Advanced OS Class of 2011 17
18. Conditional Critical Region var mutex: shared T; begin region v do begin await condition end; End; Special queue is maintained, allowed only when the condition is met MIT CS704D Advanced OS Class of 2011 18
19. Monitor MIT CS704D Advanced OS Class of 2011 19 Enforce concurrency Processes Processes Access, modify shared variable(s)
20. Monitors-Plus Minus Can regulate a group of related resources too If this is too many, the serialization overhead could be too much Same things can be done at kernel level Serialization overheads will cause problems very quickly as kernel controls all kinds of resources Writing, building, debugging such monolithic structures could be problematic Monitors can create a deadlock Monitor disciplines may restrict application programmers MIT CS704D Advanced OS Class of 2011 20
21. Messages A collection of data, execution commands, sometimes even code Interchanged between sending and receiving processes MIT CS704D Advanced OS Class of 2011 21 Sender id Receiver id Length Header Type …… Message body
22. Issues in Message Implementation Naming Direct, indirect (mailbox) Copying Copy message, pass pointer Synchronous/asynchronous Synchronous can be blocking, asynchronous can cause runaway, indefinite postponement Length Fixed or variable length (overhead vs. flexibility) MIT CS704D Advanced OS Class of 2011 22
23. Inter-process Communication & sync with Messages Assume buffered message, infinite channel capacity, indirect naming (via mailboxes) Sender send s the message & continues, receiver will be suspended if no message Sync through semaphore like operation of messages. Signal send a message to waiting on semaphore, wait is just waiting to receive a message Sync also can be through messages Example; producer waits for a message through mayproduce mailbox, a consumer gets a mayconsume MIT CS704D Advanced OS Class of 2011 23
24. Interrupt Signaling via Message A message (signal) can initiate a set of waiting interrupt service processes Interrupt service need not be treated differently from other processes Hardware interrupts that need guaranteed response time, may be a problem All software interrupts can be handled this way MIT CS704D Advanced OS Class of 2011 24
25. Deadlocks A deadlock is a situation where processes are permanently blocked as a result of each process having acquired a subset of the resources needed for its completion and waiting for the release of the remaining resources held by others in the same group- thus making it impossible for any of the processes to proceed. MIT CS704D Advanced OS Class of 2011 25
26. Necessary Conditions Mutual exclusion. Shared resources are used exclusively by at most one process at a time. Hold & Wait. Resources already allocated are held by the process and waits for the balance to be acquired No preemption. Resources are released only when given up by the owner Circular waiting. Each process hold one or more resources being requested by the next process in the chain MIT CS704D Advanced OS Class of 2011 26
27. Reusable & Consumable Resources Reusable. Resources that can be safely used by one process at any time. It is either available or allocated to a process It can only be relinquished by the owner Single resource multiple instances, multiple resources of single instance Consumable resources. Once consumed, these do not exist any more; example messages. Deadlocks can happen, such as a receiver waiting for a message. OS must intervene to break such deadlocks. MIT CS704D Advanced OS Class of 2011 27
28. Deadlock Prevention-1 Hold-an-wait condition can be resolved by forcing release of all other held resources when the process requests for a resource that is not available. Request all resources prior to execution Asks for resources as needed but relinquishes resources held by it when a requested resource is not available Overestimation of resources, holding on to resources longer than necessary Reduces concurrency, resources are underutilized MIT CS704D Advanced OS Class of 2011 28
29. Deadlock Prevention-2 No-preemptionissue can obviously be solved by allowing preemption OS will need to save the state of the process For some resources the preemption may not be a problem, like CPU and memory pages but resources like files cannot be safely preempted without corrupting the system Apply such policies only when the benefits of deadlock prevention is more than the cost of save & restore of state of some resources MIT CS704D Advanced OS Class of 2011 29
30. Deadlock Prevention-3 Circular wait. Request resources of a higher class only after the resources from a lower class has been acquired successfully. All requests in a given class must be acquired through a single request. The prescribed ordering can be checked at compile time, avoiding run time problems Disadvantages All resources must be acquired up front Lower degree of concurrency and lower utilization of resources MIT CS704D Advanced OS Class of 2011 30
31. Deadlock Avoidance Grant resources only if the request is not likely to cause deadlocks A resource allocator must examine the implications All processes must proclaim maximum need When requested, the resource allocator must check if the other executing processes can safely complete (they have resource allocation pending) . If not the process should wait. MIT CS704D Advanced OS Class of 2011 31
32. Deadlock detection & Recovery If the general resource graph has a cycle or a knot then deadlock exists Rollback or restarting can be options State needs to be known Some systems have check pointing or journaling system, one could use that MIT CS704D Advanced OS Class of 2011 32
33. Combined Approach Typical classes of devices Swap area Job resources & assignable devices Main memory (by page, segment etc.) Internal resources such as I/O channels, buffer pool etc. Deadlock prevention between the main classes, deadlock handling within each class is used MIT CS704D Advanced OS Class of 2011 33
34. Combined Policies Swap space: advance booking of all swap space. Dead lock detection is not possible Job resources: pre-claiming of resources, Resource ordering also is possible. Detection combined with recovery is undesirable, as can have repercussions on file resources Main memory : preemption is used but not avoidance as that has run time overheads and resource underutilization Internal system resources : avoidance or detection will have performance penalties. Prevention by means of resource ordering is typically done MIT CS704D Advanced OS Class of 2011 34