ch06

download ch06

of 48

  • date post

    17-Aug-2014
  • Category

    Documents

  • view

    228
  • download

    5

Embed Size (px)

Transcript of ch06

School of Computing Science Simon Fraser University

CMPT 300: Operating Systems I Ch 6: Process Synchronization Dr. Mohamed Hefeeda

1

ObjectivesUnderstand The Critical-Section Problem And its hardware and software solutions

2

Consumer-Producer ProblemClassic example of process coordination Two processes sharing a buffer One places items into the buffer (producer) Must wait if the buffer is full

The other takes items from the buffer (consumer) Must wait if buffer is empty

Solution: Keep a counter on number of items in the buffer

3

Producer Processwhile (true) { /* produce an item in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }4

Consumer Processwhile (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /*consume item in nextConsumed */ } What can go wrong with this solution?5

Race Conditioncount++ could be implemented as register1 = count register1 = register1 + 1 count = register1 count-- could be implemented as register2 = count register2 = register2 - 1 count = register2 Consider this execution interleaving with count = 5 initially: S0: producer executes register1 = count {register1 = 5} S1: producer executes register1 = register1 + 1 {register1 = 6} S2: consumer executes register2 = count {register2 = 5} S3: consumer executes register2 = register2 - 1 {register2 = 4} S4: producer executes count = register1 {count = 6 } S5: consumer executes count = register2 {count = 4}6

Race ConditionOccurs when multiple processes manipulate shared data concurrently and the result depends on the particular order of manipulation Data inconsistency may arise Solution idea Mark code segment that manipulates shared data as critical section If a process is executing its critical section, no other processes can execute their critical sections

More formally, any method that solves the Critical-Section Problem must satisfy three requirements 7

Critical-Section (CS) Problem1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then selection of the process that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted

y Assumptions y Each process executes at a nonzero speed y No restriction on the relative speed of the N processes8

Solutions for CS ProblemDisable interrupts during running CS Currently running code would execute without preemption Possible only on uniprocessor systems. Why? Every processor has its own interrupts Disabling interrupts in all processors is inefficient

Any problems with this solution even on uniprocessor systems? Users could make CS arbitrary large unresponsive system

Solutions using software only Solutions using hardware support9

Petersons SolutionSoftware solution; no hardware support Two process solution Assume LOAD and STORE instructions are atomic (i.e., cannot be interrupted) may not always be true in modern computers

The two processes share two variables: int turn; Boolean flag[2];

turn indicates whose turn it is to enter critical section The flag array indicates whether a process is ready to enter critical section flag[i] = true ==> process Pi is ready10

Algorithm for Process Piwhile (true) { flag[i] = TRUE; turn = j; while (flag[j] && turn == j) ; CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION }

Does this algorithm satisfy the three requirements? Yes. Show this as an exercise.11

Synchronization HardwareMany systems provide hardware support for critical section code more efficient and easier for programmers Modern machines provide special atomic (noninterruptable) hardware instructions Either test a memory word and set value Or swap contents of two memory words

12

TestAndndSet InstructionDefinition: boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv; }

13

Solution using TestAndSetShared boolean variable lock, initialized to false while (true) { while ( TestAndSet (&lock ) ) ; /* do nothing // critical section lock = FALSE; // remainder section }Does this algorithm satisfy the three requirements? NO. A process can wait indefinitely for another faster process that is accessing its CS. Check Fig 6.8 for a modified version.14

Swap InstructionDefinition: void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp; }

15

Solution using SwapShared boolean variable lock initialized to FALSE; Each process has a local boolean variable key while (true) { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section lock = FALSE; // remainder section }16

SemaphoreMuch easier to use than hardware-based solutions Semaphore S integer variable Two standard operations to modify S:wait() signal()

These two operations are indivisible (atomic)

17

Semaphore Operationswait (S) { while (S