DOC PREVIEW
CU-Boulder CSCI 3753 - Synchronization

This preview shows page 1-2-3-19-20-39-40-41 out of 41 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

SynchronizationAnnouncementsFrom last time...Slide 4Slide 5Slide 6Critical SectionSlide 8Slide 9Using a Lock VariableBusy Wait ConditionUnsafe “Solution”Slide 13Atomic Lock ManipulationAtomic Lock ManipulationSlide 16Bounded Buffer ProblemBounded Buffer Problem (2)Bounded Buffer Problem (3)Readers-Writers ProblemReaders-Writers Problem (2)Readers-Writers Problem (3)Readers-Writers Problem (4)First SolutionFirst Solution (2)Writer PrecedenceWriter Precedence (2)The Sleepy BarberSleepy Barber (aka Bounded Buffer)Cigarette Smoker’s ProblemImplementing SemaphoresImplementing Semaphores: enter() & exit()Implementing Semaphores: Test and Set InstructionUsing the TS InstructionImplementing the General SemaphoreActive vs Passive SemaphoresSlide 37Slide 38Slide 39Slide 40Slide 41SynchronizationCSCI 3753 Operating SystemsSpring 2005Prof. Rick HanAnnouncements•HW #3 is online, due Friday Feb. 25 at 11 am•PA #2 is coming, assigned next Tuesday•Midterm is tentatively Thursday March 10•Read chapter 8From last time...•Many cases where processes and threads want to access shared common variables, buffers, etc.•Producer-Consumer with a shared bounded buffer in between–Producer increments counter++–Consumer decrements counter--•race conditions between producer and consumer–Machine-level instructions can be interleaved, resulting in unpredictable value of shared counter variableSynchronizationwhile(1) { while(counter==MAX); buffer[in] = nextdata; in = (in+1) % MAX; counter++;}DataBounded BuffercounterProducerProcessConsumerProcesswhile(1) { while(counter==0); getdata = buffer[out]; out = (out+1) % MAX; counter--;}Producer writes new data intobuffer and increments counterConsumer reads new data frombuffer and decrements countercounterupdatescan conflict!buffer[0]buffer[MAX]Synchronizationcounter++; can translate into several machine language instructions, e.g. reg1 = counter; reg1 = reg1 + 1; counter = reg1;counter--; can translate into several machine language instructions, e.g. reg2 = counter; reg2 = reg2 - 1; counter = reg2;If these low-level instructions are interleaved, e.g. the Producer processis context-switched out, and the Consumer process is context-switched in,and vice versa, then the results of counter’s value can be unpredictableSynchronization// counter++ reg1 = counter; reg1 = reg1 + 1; counter = reg1;// counter--; reg2 = counter; reg2 = reg2 - 1; counter = reg2;(1) [5](3) [6](2) [5](4) [4](5) [6](6) [4]•Suppose we have the following sequence of interleaving, where the brackets [value] denote the local value of counter in either the producer or consumer’s process. Let counter=5 initially.•At the end, counter = 4. But if steps (5) and (6) were reversed, then counter=6 !!!•Value of shared variable counter changes depending upon (an arbitrary) order of writes - very undesirable!Critical Section•Kernel data structures - e.g. access to list of open files subject to race condition•Kernel developer must ensure that no such race conditions occur•Identify critical sections in code where each process access shared variablesdo { entry section critical section (manipulate common var’s) exit section remainder code} while (1)mutual exclusion, bounded waiting, progressCritical Section•How do we protect access to critical sections?–want to prevent another process from executing while current process is modifying this variable - mutual exclusion–disable interrupts•bad, because I/O may be prevented•bad, because program may have an infinite loopCritical Section•Don’t disable interrupts for the entire time you’re in the critical section•Solution: Set a variable/flag called a lock to indicate that the critical section is busy, then unset the flag when critical section is done–doesn’t disable interrupts in the critical section–also Peterson’s solutionUsing a Lock Variableshared boolean lock = FALSE;shared int counter;Code for p1 Code for p2/* Acquire the lock */ /* Acquire the lock */ while(lock) ; while(lock) ; lock = TRUE; lock = TRUE;/* Execute critical sect */ /* Execute critical sect */ counter++; counter--;/* Release lock */ /* Release lock */ lock = FALSE; lock = FALSE;Busy Wait Conditionshared boolean lock = FALSE;shared int counter;Code for p1 Code for p2/* Acquire the lock */ /* Acquire the lock */ while(lock) ; while(lock) ; lock = TRUE; lock = TRUE;/* Execute critical sect */ /* Execute critical sect */ counter++; counter--;/* Release lock */ /* Release lock */ lock = FALSE; lock = FALSE;p1p2Blockedat whilelock = TRUElock = FALSEInterruptInterruptInterruptUnsafe “Solution”shared boolean lock = FALSE;shared double balance;Code for p1 Code for p2/* Acquire the lock */ /* Acquire the lock */ while(lock) ; while(lock) ; lock = TRUE; lock = TRUE;/* Execute critical sect */ /* Execute critical sect */ counter++; counter--;/* Release lock */ /* Release lock */ lock = FALSE; lock = FALSE;•Worse yet … another race condition …•Is it possible to solve the problem?Critical Sectionwhile (lock==TRUE) ;lock = TRUE;•test-and-set solution doesn’t work if process is interrupted right after while()•Want an atomic instruction testandset() that tests shared variable lock then sets it•Some hardware provides such an instructionAtomic Lock Manipulationshared boolean lock = FALSE;shared int counter;Code for p1 Code for p2/* Acquire the lock */ /* Acquire the lock */ while(TestandSet(&lock)) ; while(TestandSet(&lock)) ;/* Execute critical sect */ /* Execute critical sect */ counter++; counter--;/* Release lock */ /* Release lock */ lock = FALSE; lock = FALSE;______________________________________________________________boolean TestandSet(boolean *target) { // this is atomic, one machine instruction boolean rv = *target; *target = TRUE; return rv;}Atomic Lock Manipulation •The system is exclusively occupied for only a short time - the time to test and set the lock, and not for entire critical section–about 10 instructions•don’t have to disable and reenable interrupts - time-consuming•disadvantage: requires user to put in while()•low level assembly language manipulation of a machine instruction TSBounded Buffer ProblemProducerProducerConsumerConsumerEmpty PoolFull PoolBounded Buffer Problem


View Full Document

CU-Boulder CSCI 3753 - Synchronization

Download Synchronization
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 Synchronization 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 Synchronization 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?