Cs 704 d set2

33
CS 704D Advanced Operating System Debasis Das

description

 

Transcript of Cs 704 d set2

Page 1: Cs 704 d set2

CS 704DAdvanced Operating

System

Debasis Das

Page 2: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

2

Process Synchronization

Page 3: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

3

TopicsConcept of processes

Concurrent processes

Threads

Overview of classical synchronization

problems

Monitors

Communicating Sequential processes

Page 4: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

4

Concept of Processes

Page 5: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

5

Denning’s Definitionof

Operating Systems(1983)

OS is a computer software that assists hardware to perform following process management functionsCreating and destroying processesControlling progress towards completionActing on arithmetic and other exceptionsAllocating hardware resourcesProviding communications by messages and

signals among processes

Page 6: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

6

Implicit & Explicit Tasking

Implicit tasking (system defined)Where the OS creates the processes to manage

its own workExplicit tasking(programmer defined)

Defined by programmer to create concurrent processes to make the execution of the program faster Driving I/O that have latency User convenience Multiprocessing Distributed computing

Page 7: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

7

Process RelationshipCompetition

Competes for resources for completionCareful resource allocation and protectionControlled sharing of data and exchange of

sync signalsCooperation

Shares resources, attributes; interactsFamily processes, parent-child processes

Page 8: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

8

Process Control BlockProcess idPriorityState (ready, running, suspended)Hardware state (processor registers, flags)Scheduling information, usage statisticsMemory management information (registers,

tables)I/O status (allocated devices, pending operations)File management information (open files, access

rights)Accounting information

Page 9: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

9

Process Lists

Linked lists of PCBs

One list for ready processes

One for suspended

One of running- trivial in case of uniprocessor

Page 10: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

10

Process SwitchMode switchRecord state of current process; PC, SP,PSW,

and all program accessible registersUpdate PCB data; state, reason for preemption

or suspension, measure processor usageInvoke scheduler to start a relevant process

Get PCB data to prepare running of the processRestore hardware state, calculate interrupt maskEstablish pointers to active files, resource listsUpdate memory management registers

Page 11: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

11

Process Switching Efficiency

Hardware assists

Have hardware set of registers identical in

structure to PCB and

Threads or lightweight processes

Page 12: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

12

ThreadsA lightweight process with reduced stateA group of related threads can share

resources such as memory and filesSeparate flow of control with its own stack

and hardware stateProgrammer driven concurrencySwitching between threads from different

processes costs the same as switching processes

Page 13: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

13

OS Services for

Process ManagementCreate (processID, attributes)Delete (processID)Fork/Join Abort (processID)Suspend (processID)Resume (processID)Delay (processID, time)Get_Attributes (processID, attribute_set)Change_Priority (processID, new_priority)

Page 14: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

14

Schedulers

Suspended queue

Suspended &Swapped out

queue

Batch Queue Ready Queue CPU

Interactiveprograms

Batchjobs

Long term scheduler

Medium termscheduler

Short term scheduler

Page 15: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

15

Scheduling performance Criteria

Processor utilization

Throughput

Turnaround time

Waiting time

Response time

Page 16: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

16

Scheduling Algorithms

Preemptive, Non preemptive

First come first served (FCFS)

Shortest remaining time next (SRTN)

Time slice or Round Robin or RR

Priority based event driven preemptive, (event

driven, ED)

Multiple level Queues, MLQ

MLQ with feedback

Page 17: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

17

Concurrent Processes

Page 18: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

18

Inter-process SynchronizationCooperating processes need to synchronize

with each other when resources need to be shared-shared data structures or physical device. Following interactions neededInter-process synchronization

For serially reusable resourcesInter-process signaling

Exchange of signals to help synchronizationInter-process communication

Exchanging data, reporting progress, accumulating collective result

Page 19: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

19

Problem Example{Process Keyboard}Echo= echo+1-----------------------------------{Process Display}}Echo=echo-1------------------------------------Load R, echoDec RStore Echo, R

Page 20: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

20

Desirable features of

Mutual ExclusionMutual exclusion between processes

accessing protected shared resourceRelative speeds and priorities of contending

processes can never be assumedCrashes outside the critical section should

not prevent other processes from accessing the shared resource

When contention arises access must be granted to one in finite time

Page 21: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

21

Basic ProtocolFollowing sequence must be used

(negotiation protocol)---winner goes ahead

(Critical section)--Only one process uses the

resource

(Release protocol)—Ownership released

Page 22: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

22

Algorithm #1Code as in Milan Milenkovic, p 93Problems

Identity of adversary is knownThere is a strict sequence (P1, P2)If one of them crashes, the other will have to

keep waitingFor multiple contending processes this could

get complex

Page 23: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

23

Algorithm #2Code as in Milan Milenkovic, p 96

Improvement

No turn taking is forced

Problems

More than one process may be in the critical section

Page 24: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

24

Algorithm #3

Code as in Milan Milenkovic, p 98

Improvement

No two processes can be in the CS at the same

time

Problem

Both can get into an indefinite loop

Page 25: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

25

SemaphoresWait; decrement value of semaphore if it is

non negative. Wait operation is indivisibleSignal; increment the value of semaphore,

operation is indivisible--------------------------------Binary semaphores and general or counting

semaphore

Page 26: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

26

Wait & signal operations

wait(s): while not (s>0) do (keeptesting) s:=s-1---------------------------------

signal(s): s:=s+1ImplementationCode in Milan Milenkovic, p 100Advantages

Meets all the criteria for mutual exclusion

Page 27: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

27

Properties & CharacteristicsProcesses need not even be aware of number

of competitorsModification of code in one process does not

need changes in anotherSemaphores can be in Programming

language or as service calls in OSCreate_Semaphore, Attach_To_Semaphore,

Wait, Signal, Close_Semaphore are some calls that can be implemented

Page 28: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

28

Service DisciplineRequired to avoid indefinite postponement or

livelock or starvation

Discipline policy has to avoid such starvation

“A request to enter a critical section must be

granted in finite time” must be enforced

FCFS policy ensure everyone gets a turn

Page 29: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

29

Semaphore Granularity

Serialization of processes by semaphore can have an impact on performance

Granularity of individual semaphores then is an important aspect

Finest granularity ensure there is no concurrency timing problemsProblems: potentially large storage overhead for

storing a large number of semaphores, processing overhead of too many wait & signals

Coarse granularity (controlling a set of resources within one Semaphore)Problem: reduces benefits of parallelism

Page 30: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

30

Hardware Supportfor

Concurrency Control

Semaphore implementation needs some hardware support

Pessimistic & Optimistic concurrency control

Disable/enable interrupts

Test and set instruction

Compare an Swap instruction

Page 31: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

31

Pessimistic & OptimisticConcurrency control

Pessimistic solutionBlock everythingUpdate global variableUnblock the part of the system blocked in first step---------------------------------------------------

Optimistic solutionRead the value of the global variable, prepare tentative

local update based on the value (global variable remains accessible to others)

Compare current value of global variable to the value of local update. If the global variable is unchanged, apply local update. If not discard local update. Go to earlier step

Page 32: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

32

Tools for Implementationof Semaphores #1

Disable/enable interrupts (but is pessimistic)DICritical sectionEI---------------------------

If users have access, it can cause chaosDoes not work in multiple processors case ban use by programmers, only system should

use for implementing the wait and signal methods

Page 33: Cs 704 d set2

MIT CS704D Advanced OS Class of 2011

33

Tools for Implementationof Semaphores #2

Test and Set instructionWait: TS S BNF Wait Return-----------------------------

Wait: Mov AL, 1 XCHG AL,S CMP AL, 0 JNZ Wait Ret