Cs 704 d set3

34
CS 704D Advanced Operating System Debasis Das

description

 

Transcript of Cs 704 d set3

Page 1: Cs 704 d set3

CS 704DAdvanced Operating

System

Debasis Das

Page 2: Cs 704 d set3

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

Page 3: Cs 704 d set3

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

Page 4: Cs 704 d set3

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

Page 5: Cs 704 d set3

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

Page 6: Cs 704 d set3

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

Page 7: Cs 704 d set3

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

Page 8: Cs 704 d set3

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

Page 9: Cs 704 d set3

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

Page 10: Cs 704 d set3

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

Page 11: Cs 704 d set3

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

Page 12: Cs 704 d set3

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

Page 13: Cs 704 d set3

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

Page 14: Cs 704 d set3

MIT CS704D Advanced OS Class of 2011

14

Inter-process Communication

& Synchronization

Page 15: Cs 704 d set3

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.

Page 16: Cs 704 d set3

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

Page 17: Cs 704 d set3

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

Page 18: Cs 704 d set3

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

Page 19: Cs 704 d set3

MIT CS704D Advanced OS Class of 2011

19

Monitor

Enforce concurrency

Access, modify shared variable(s)

ProcessesProcesses

Page 20: Cs 704 d set3

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

Page 21: Cs 704 d set3

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

Page 22: Cs 704 d set3

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)

Page 23: Cs 704 d set3

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

Page 24: Cs 704 d set3

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

Page 25: Cs 704 d set3

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.

Page 26: Cs 704 d set3

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

Page 27: Cs 704 d set3

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.

Page 28: Cs 704 d set3

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

Page 29: Cs 704 d set3

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

Page 30: Cs 704 d set3

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

Page 31: Cs 704 d set3

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.

Page 32: Cs 704 d set3

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

Page 33: Cs 704 d set3

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

Page 34: Cs 704 d set3

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