08.07.2015 Views

Topic Notes: Process Synchronization - Courses

Topic Notes: Process Synchronization - Courses

Topic Notes: Process Synchronization - Courses

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CS 322 Operating Systems Spring 2008Where R is a CPU register and X is a memory location. After the instruction completes (atomically),the value that was in memory at X is copied into R, and the value of X is set to 1. R containsa 0 if X previously contained a 0. If two processes do this operation concurrently, only one willactually get the 0.The Intel x86 BTS instruction sets a single bit in memory and sets the carry bit of the status wordto the previous value. This is equivalent.Think about how you might implement an atomic test and set on, for example, a microprogrammedarchitecture.Any of these can be used to implement a “test and set” function as described above.Armed with this atomic test-and-set, we can make a simple mutual exclution solution for anynumber of processes, with just a single shared variable:• Shared databoolean lock = false;• <strong>Process</strong> P iwhile (1) {while (TestAndSet(&lock)); /* busy wait *//* critical section */lock = false;}/* non-critical section */This satisfies mutual exclusion and progress, but not bounded waiting (a process can leave the CSand come back around and grab the lock again before others who may be waiting ever get a chanceto look).A solution that does satisfy bounded waiting is still fairly complicated:• Shared databoolean lock=false;boolean waiting[n]; /* all initialized to false */• <strong>Process</strong> P i and its local data11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!