cs 704 d set3
DESCRIPTION
TRANSCRIPT
CS 704DAdvanced Operating
System
Debasis Das
MIT CS704D Advanced OS Class of 2011
2
Tools for Implementationof Semaphores #3
Compare & Swap InstructionsHelps consistent update of global variable
ImplementationCompare Oldreg, GlobvarSet condition codesIf (Oldreg=Globvar) Then Globvar Newreg Else OldregGlobvar
MIT CS704D Advanced OS Class of 2011
3
Queuing Implementationof Semaphore
Pz ….. Py Px Semaphore
Wait(s): If not (s>0) then suspend caller at s else s:= s+1Signal (s): if queue is not empty (at least one process is waiting) then resume process from the queue at s else s:=s+1
MIT CS704D Advanced OS Class of 2011
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
5
Producers & Consumers
One class of processes produce data itemsThe other class consumes/uses this dataThey may have different rates and thus cause
synchronization problemsSynchronizations required so that producers &
consumers are able to operate concurrentlySuch that items produced are consumed in the
same orderDisplay, keyboard was an example
Processes may be a combination of both producer and consumer
MIT CS704D Advanced OS Class of 2011
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 achievedA semaphore “Producer” can take care of that
We assume buffer manipulation does not cause problemsThis is not really a valid assumption in multiple
producer, consumer situations
MIT CS704D Advanced OS Class of 2011
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
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
9
Unbounded case Icount=produced-consumedNecessary that icount cannot be less than
zero and more than the capacityThen
Condition mayproduce : icount < capacity as also mayconsume: icount>0
MIT CS704D Advanced OS Class of 2011
10
Readers & WritersReaders and consumers are processes that
operate against some common data structureReaders are pure readers, only reads parts or
all of the data structureWriters 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
11
The sync ProblemGiven 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
12
Example Synchronization
High concurrencyAllows high number of readers to access
common resourceWriter waits for the wait semaphoreReader process makes it possible for multiple
readers to workReadercount really tracks if even one reader
is activeNext round, the writer gets turn only when
all readers finished reading, that may be unfair
MIT CS704D Advanced OS Class of 2011
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
14
Inter-process Communication
& Synchronization
MIT CS704D Advanced OS Class of 2011
15
Semaphore ProblemsSync 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
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
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
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
19
Monitor
Enforce concurrency
Access, modify shared variable(s)
ProcessesProcesses
MIT CS704D Advanced OS Class of 2011
20
Monitors-Plus MinusCan regulate a group of related resources tooIf this is too many, the serialization overhead
could be too muchSame things can be done at kernel levelSerialization overheads will cause problems very
quickly as kernel controls all kinds of resourcesWriting, building, debugging such monolithic
structures could be problematicMonitors can create a deadlockMonitor disciplines may restrict application
programmers
MIT CS704D Advanced OS Class of 2011
21
MessagesA collection of data, execution commands,
sometimes even codeInterchanged between sending and receiving
processesSender id
Receiver id
Length
……
Type
Message body
Header
MIT CS704D Advanced OS Class of 2011
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
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 messageSync 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 messagesExample; producer waits for a message
through mayproduce mailbox, a consumer gets a mayconsume
MIT CS704D Advanced OS Class of 2011
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
25
DeadlocksA 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
26
Necessary ConditionsMutual 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
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 processIt can only be relinquished by the ownerSingle resource multiple instances, multiple
resources of single instanceConsumable 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
28
Deadlock Prevention-1Hold-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 executionAsks 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
29
Deadlock Prevention-2No-preemption issue can obviously be
solved by allowing preemptionOS will need to save the state of the processFor 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
30
Deadlock Prevention-3Circular 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
DisadvantagesAll resources must be acquired up frontLower degree of concurrency and lower
utilization of resources
MIT CS704D Advanced OS Class of 2011
31
Deadlock AvoidanceGrant resources only if the request is not
likely to cause deadlocksA resource allocator must examine the
implicationsAll processes must proclaim maximum needWhen 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
32
Deadlock detection & Recovery
If the general resource graph has a cycle or a knot then deadlock exists
Rollback or restarting can be optionsState needs to be knownSome systems have check pointing or
journaling system, one could use that
MIT CS704D Advanced OS Class of 2011
33
Combined ApproachTypical classes of devices
Swap areaJob resources & assignable devicesMain 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
34
Combined PoliciesSwap space: advance booking of all swap space.
Dead lock detection is not possibleJob 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