Unformatted text preview:

BackgroundBounded-BufferBounded-BufferBounded-BufferBounded BufferBounded BufferBounded BufferBounded BufferRace ConditionThe Critical-Section ProblemMutual Exclusion: Conditions for SolutionInitial Attempts to Solve ProblemAlgorithm 1Algorithm 2Algorithm 3Solution to Critical Section ProblemSynchronization HardwareMutual Exclusion with Test-and-SetSemaphoresSemaphore ImplementationImplementationCritical Section of n ProcessesSemaphore as a General Synchronization ToolDeadlock and StarvationClassical Problems of SynchronizationBounded-Buffer ProblemBounded-Buffer Problem Producer ProcessBounded-Buffer Problem Consumer ProcessReaders-Writers ProblemReaders-Writers Problem Writer ProcessReaders-Writers Problem Reader ProcessDining-Philosophers ProblemDining-Philosophers Problem: A non-solutionHigh-level synchronization mechanismsMonitorsMonitorsSchematic View of a MonitorMonitor With Condition VariablesProducer-Consumer using monitorsDining Philosophers ExampleDining PhilosophersDining PhilosophersCooperating concurrent processesSynchronization MechanismsJava thread synchronization callsMutual exclusion in JavaProducer consumer using JavaProducer consumer using Java cont’dThe Reader/Writer Problem11Background Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. Bounded Buffer problem (also called producer consumer problem)2Bounded-Buffer  Shared data#define BUFFER_SIZE 10typedef struct {. . .} item;item buffer[BUFFER_SIZE];int in = 0;int out = 0;int counter = 0;23Bounded-Buffer  Producer process item nextProduced;while (1) {while (counter == BUFFER_SIZE); /* do nothing */buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;counter++;}4Bounded-Buffer  Consumer process item nextConsumed;while (1) {while (counter == 0); /* do nothing */nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;counter--;}35Bounded Buffer The statementscounter++;counter--;must be performed atomically. Atomic operation means an operation that completes in its entirety without interruption.6Bounded Buffer The statement “count++” may be implemented in machine language as:register1 = counterregister1 = register1 + 1counter = register1 The statement “count--” may be implemented as:register2 = counterregister2 = register2 – 1counter = register247Bounded Buffer If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved. Interleaving depends upon how the producer and consumer processes are scheduled.8Bounded Buffer Assume counter is initially 5. One interleaving of statements is:producer: register1 = counter (register1 = 5)producer: register1 = register1 + 1 (register1 = 6)consumer: register2 = counter (register2 = 5)consumer: register2 = register2 – 1 (register2 = 4)producer: counter = register1 (counter = 6)consumer: counter = register2 (counter = 4) The value of count may be either 4 or 6, where the correct result should be 5.59Race Condition Race condition: The situation where several processes access – and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized.10The Critical-Section Problem n processes all competing to use some shared data Each process has a code segment, called critical section, in which the shared data is accessed. Problem – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section.611Mutual Exclusion: Conditions for SolutionFour conditions to provide mutual exclusion1. No two processes simultaneously in critical region2. No assumptions made about speeds or numbers of CPUs3. No process running outside its critical region may block another process4. No process must wait forever to enter its critical region12Initial Attempts to Solve Problem Only 2 processes, P0and P1 General structure of process Pi(other process Pj)do {entry sectioncritical sectionexit sectionreminder section} while (1); Processes may share some common variables to synchronize their actions.713Algorithm 1 Shared variables: o int turn;initially turn = 0o turn = i ⇒ Pican enter its critical section Process Pido {while (turn != i) ;critical sectionturn = j;reminder section} while (1); Satisfies mutual exclusion, but not progress14Algorithm 2 Shared variableso boolean flag[2];initially flag [0] = flag [1] = false.o flag [i] = true ⇒ Piready to enter its critical section Process Pido {flag[i] := true;while (flag[j]) ;critical sectionflag [i] = false;remainder section} while (1); Satisfies mutual exclusion, but not progress requirement.815Algorithm 3 Combined shared variables of algorithms 1 and 2. Process Pido {flag [i]:= true;turn = j;while (flag [j] and turn = j) ;critical sectionflag [i] = false;remainder section} while (1); Meets all three requirements; solves the critical-section problem for two processes.16Solution to Critical Section Problem Peterson’s solution works for two processes N-process solution: Bakery algorithm o Will discuss in next class Both Peterson’s solution and Bakery algorithm are software solutions, i.e. no hardware support needed917Synchronization Hardware Test and modify the content of a word atomically.boolean TestAndSet(boolean &target) {boolean rv = target;target = true;return rv;}18Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Process Pido {while (TestAndSet(lock)) ;critical sectionlock = false;remainder section}1019Semaphores Synchronization tool that does not require busy waiting.o Uses blocking synchronization can only be accessed via two indivisible (atomic) operations: wait() and signal() Each semaphore has an integer value and a queue associated with it20Semaphore Implementation Define a semaphore as a recordtypedef struct {int value;struct process *L;} semaphore; Assume two simple operations:o block suspends the process that invokes it.o wakeup(P) resumes the execution of a blocked process P.1121Implementation Semaphore operations defined as wait(S):S.value--;if (S.value < 0) { add this process to S.L;block;}signal(S): S.value++;if (S.value <= 0) {remove a process P from S.L;wakeup(P);}22Critical Section of n Processes


View Full Document

MASON CS 475 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?