Set4(Synchronization)
3 Pages

Set4(Synchronization)

Course Number: CSE 410, Fall 2009

College/University: Texas A&M

Word Count: 2549

Rating:

Document Preview

CPSC-410/611 Operating Systems Process Synchronization Process Management: Synchronization Why? How? Examples Software solutions Hardware-supported solutions What? The Critical Section Problem The basic synchronization mechanism: Semaphores More sophisticated synchronization mechanisms: Monitors, Message Passing Classical synchronization problems Process Management: Synchronization Why? How? Examples...

Unformatted Document Excerpt
Coursehero >> Texas >> Texas A&M >> CSE 410

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Operating CPSC-410/611 Systems Process Synchronization Process Management: Synchronization Why? How? Examples Software solutions Hardware-supported solutions What? The Critical Section Problem The basic synchronization mechanism: Semaphores More sophisticated synchronization mechanisms: Monitors, Message Passing Classical synchronization problems Process Management: Synchronization Why? How? Examples Software solutions Hardware-supported solutions What? The Critical Section Problem The basic synchronization mechanism: Semaphores More sophisticated synchronization mechanisms: Monitors, Message Passing Classical synchronization problems 1 CPSC-410/611 Operating Systems Process Synchronization The Critical Section Problem: Example 1 void echo() { input(in, keyboard); out := in; output(out, display); } Process 1 Echo() input(in,keyboard) out = in; input(in,keyboard); out = in; output(out,display); char in; /* shared variables */ char out; Operation: Interleaved execution Process 2 Echo() output(out,display) Race condition ! The Critical Section Problem: Example 2 Producer-consumer with bounded, shared-memory, buffer. out circular buffer of size n int Item int in, out; buffer[n]; counter; in Producer: void deposit(Item * next) { while (counter == n) no_op; buffer[in] = next; in = (in+1) MOD n; counter = counter + 1; } Consumer: Item * remove() { while (counter == 0) no_op; next = buffer[out]; out = (out+1) MOD n; counter = counter - 1; return next; } 2 CPSC-410/611 Operating Systems Process Synchronization This Implementation is not Correct! Producer operation: on CPU: counter = counter + 1 reg1 = counter reg1 = reg1 + 1 counter = reg1 reg1 = counter reg1 = reg1 + 1 reg2 = counter reg2 = reg2 - 1 counter = reg1 counter = reg2 Race condition! Need to ensure that only one process can manipulate variable counter at a time : synchronization. Consumer counter = counter - 1 reg2 = counter reg2 = reg2 - 1 counter = reg2 interleaved execution: Critical Section Problem: Example 3 Insertion of an element into a list. new curr prev next prev next prev next 1. new curr prev next prev next prev next void insert(new, curr) { /*1*/ new.next = curr.next; /*2*/ new.prev = c.next.prev; /*3*/ curr.next = new; /*4*/ new.next.prev = new; } new curr prev next prev next prev 2. new curr prev next prev next prev next new curr 4. prev next prev next 3. prev next next 3 CPSC-410/611 Operating Systems Process Synchronization Interleaved Execution causes Errors! Process 1 new1.next = curr.next; new1.prev = c.next.prev; ... ... ... ... curr.next = new1; new.next.prev = new1; new1 curr prev next new2 next Process 2 ... ... new2.next = curr.next; new2.prev = c.next.prev; curr.next = new2; new.next.prev = new2; ... ... prev prev next prev next Must guarantee mutually exclusive access to list data structure! Process Management: Synchronization Why? How? Examples Software solutions Hardware-supported solutions What? The Critical Section Problem The basic synchronization mechanism: Semaphores More sophisticated synchronization mechanisms: Monitors, Message Passing Classical synchronization problems 4 CPSC-410/611 Operating Systems Process Synchronization Critical Sections Execution of critical section by processes must be mutually exclusive. Typically due to manipulation of shared variables. Need protocol to enforce mutual exclusion. while (TRUE) { enter section; critical section; exit section; remainder section; } Criteria for a Solution of the C.S. Problem 1. Only one process at a time can enter the critical section. 2. A process that halts in non-critical section cannot prevent other processes from entering the critical section. 3. A process requesting to enter a critical section should not be delayed indefinitely. 4. When no process is in a critical section, any process that requests to enter the critical section should be permitted to enter without delay. 5. Make no assumptions about the relative speed of processors (or their number). 6. A process remains within a critical section for a finite time only. 5 CPSC-410/611 Operating Systems Process Synchronization A (Wrong) Solution to the C.S. Problem Two processes P0 and P1 int turn; /* turn == i : Pi is allowed to enter c.s. */ Pi: while (TRUE) { while (turn != i) no_op; critical section; turn = j; remainder section; } Another Wrong Solution bool flag[2]; /* initialize to FALSE */ /* flag[i] == TRUE : Pi intends to enter c.s.*/ Pi: while (TRUE) { while (flag[j]) no_op; flag[i] = TRUE; critical section; flag[i] = FALSE; remainder section; } 6 CPSC-410/611 Operating Systems Process Synchronization Yet Another Wrong Solution bool flag[2]; /* initialize to FALSE */ /* flag[i] == TRUE : Pi intends to enter c.s.*/ while (TRUE) { flag[i] = TRUE; while (flag[j]) no_op; critical section; flag[i] = FALSE; remainder section; } A Combined Solution (Petersen) int turn; bool flag[2]; /* initialize to FALSE */ while (TRUE) { flag[i] = TRUE; turn = j; while (flag[j]) && (turn == j) no_op; critical section; flag[i] = FALSE; remainder section; } 7 CPSC-410/611 Operating Systems Process Synchronization Process Management: Synchronization Why? How? Examples Software solutions Hardware-supported solutions What? The Critical Section Problem The basic synchronization mechanism: Semaphores More sophisticated synchronization mechanisms: Monitors, Message Passing Classical synchronization problems Hardware Support For Synchronization Disallow interrupts simplicity widely used problem: interrupt service latency problem: what about multiprocessors? Atomic operations: Operations that check and modify memory areas in a single step (i.e. operation can not be interrupted) Test-And-Set Exchange, Swap, Compare-And-Swap 8 CPSC-410/611 Operating Systems Process Synchronization Test-And-Set bool TestAndSet(bool & var) { bool temp; temp = var; bool lock; /* init to FALSE */ var = TRUE; return temp; while (TRUE) { } while (TestAndSet(lock)) no_op; critical section; lock = FALSE; remainder section; } atomic! Mutual Exclusion with Test-And-Set Exchange (Swap) void Exchange(bool & a, bool & b){ bool temp; bool lock; /*init to FALSE */ temp = a; a = b; while (TRUE) { b = temp; } dummy = TRUE; do Exchange(lock, dummy); while(dummy); critical section; lock = FALSE; remainder section; } atomic! Mutual Exclusion with Exchange 9 CPSC-410/611 Operating Systems Process Synchronization Process Management: Synchronization Why? How? Examples Software solutions Hardware-supported solutions What? The Critical Section Problem The basic synchronization mechanism: Semaphores More sophisticated synchronization mechanisms: Monitors, Message Passing Classical synchronization problems Semaphores Problems with solutions above: Although requirements simple (mutual exclusion), addition to programs complex. Based on busy waiting. A Semaphore variable has two operations: V(Semaphore * s); /* Increment value of s by 1 in a single indivisible action. If value is not positive, then a process blocked by a P is unblocked*/ P(Semaphore * s); /* Decrement value of s by 1. If the value becomes negative, the process invoking the P operation is blocked. */ Binary semaphore: The value of s can be either 1 or 0 (TRUE or FALSE). General semaphore: The value of s can be any integer. 10 CPSC-410/611 Operating Systems Process Synchronization Effect of Semaphores Synchronization using semaphores: s.value = 0 P(s) V(s) Mutual exclusion with semaphores: BinSemaphore * s; /* init to TRUE*/ while (TRUE) { P(s); critical section; V(s); P(s) V(s) remainder section; } Implementation (with busy waiting) Binary Semaphores: P(BinSemaphore * s) { key = FALSE; do exchange(s.value, key); while (key == FALSE); } V(BinSemaphore * s) { s.value = TRUE; } General Semaphores: BinSemaphore * mutex /*TRUE*/ BinSemaphore * delay /*FALSE*/ P(Semaphore * s) { P(mutex); s.value = s.value - 1; if (s.value < 0) { V(mutex); P(delay); } else V(mutex); } V(Semaphore * s) { P(mutex); s.value = s.value + 1; if (s.value <= 0) V(delay); V(mutex); } 11 CPSC-410/611 Operating Systems Process Synchronization Implementation ("without" busy waiting) Semaphore bool lock; /* init to FALSE */ int value; PCBList * L; blocked processes P(Semaphore * s) { while (TestAndSet(lock)) no_op; s.value = s.value - 1; if (s.value < 0) { append(this_process, s.L); lock = FALSE; sleep(); } lock = FALSE; } V(Semaphore * s) { while (TestAndSet(lock)) no_op; s.value = s.value + 1; if (s.value <= 0) { PCB * p = remove(s.L); wakeup(p); } lock = FALSE; } Problems with Semaphores Deadlocks: Process is blocked waiting for an event only it can generate. P P 1 2 s.value = 1 q.value = 1 P(s) P(q) ... V(s) V(q) P(q) P(s) ... V(q) V(s) 12 CPSC-410/611 Operating Systems Process Synchronization Process Management: Synchronization Why? How? Examples Software solutions Hardware-supported solutions What? The Critical Section Problem The basic synchronization mechanism: Semaphores More sophisticated synchronization mechanisms: Monitors, Message Passing Classical synchronization problems Classical Problems: Producer-Consumer Semaphore * n; /* initialized to 0 */ BinSemaphore * mutex; /* initialized to TRUE */ Producer: while (TRUE) { produce item; P(mutex); Consumer: while (TRUE) { P(n); P(mutex); remove item; deposit item; V(mutex); V(mutex); V(n); } } consume item; 13 CPSC-410/611 Operating Systems Process Synchronization Producer-Consumer with Bounded Buffer Semaphore * full; /* initialized to 0 */ Semaphore * empty; /* initialized to n */ BinSemaphore * mutex; /* initialized to TRUE */ Classical Problems: Producer: while (TRUE) { produce item; P(mutex); P(empty); deposit item; V(mutex); V(full); } Consumer: while (TRUE) { P(full); P(mutex); remove item; V(mutex); V(empty); consume item; } Dining Philosophers 5 philosophers around a table, a plate in front of each philosopher, one chopstick between any two plates. When philosopher get hungry, he must grab both chopsticks in order to be able to eat. Semaphore * chopstick[4]; /* initialize to 1 */ while (TRUE) { P(chopstick[i]); P(chopstick[(i+1) mod 5]); eat ... V(chopstick[i]); V(chopstick[(i+1) mod 5]); think ... } Classical Problems: Problem: deadlock 14 CPSC-410/611 Operating Systems Process Synchronization The Barbershop barber shop (capacity 20) Classical Problems: cashier barber chairs (3) entry door standing room area sofa (capacity 4) exit door Semaphore * max_capacity; /* init to 20 */ Semaphore * sofa; /* init to 4 */ Semaphore * barber_chair; /* init to 3 */ Semaphore * coord; /* init to 3 */ Semaphore * cust_ready; /* init to 0 */ Semaphore * leave_b_chair; /* init to 0 */ Semaphore * payment; /* init to 0 */ Semaphore * receipt; /* init to 0 */ The Barbershop (cont) Process customer: P(max_capacity); <enter shop> P(sofa); <sit on sofa> P(barber_chair); <get up from sofa> V(sofa); <sit in barber chair> V(cust_ready); P(finished); <leave barber chair> V(leave_b_chair); <pay> V(payment); P(receipt); <exit shop> V(max_capacity); Process cashier: for(;;){ P(payment); P(coord); <accept pay> V(coord); V(receipt); } Process barber: for(;;){ P(cust_ready); P(coord); <cut hair> V(coord); V(finished); P(leave_b_chair); V(barber_chair); } 15 CPSC-410/611 Operating Systems Process Synchronization Process customer: P(max_capacity); <enter shop> P(mutex1); custnr := ++count; V(mutex1); P(sofa); <sit on sofa> P(barber_chair); <get up from sofa> V(sofa); <sit in barber chair> P(mutex2); enqueue(custnr); V(cust_ready); V(mutex2); P(finished[custnr]); <leave barber chair> V(leave_b_chair); <pay> V(payment); P(receipt); <exit shop> V(max_capacity); The Fair Barbershop Process cashier: for(;;){ P(payment); P(coord); <accept pay> V(coord); V(receipt); } Process barber: for(;;){ P(cust_ready); P(mutex2); dequeue(b_cust); V(mutex2); P(coord); <cut hair> V(coord); V(finished[b_cust]); P(leave_b_chair); V(barber_chair); } Classical Problems: Readers/Writers Multiple readers can access data element concurrently. Writers access data element exclusively. Semaphore * mutex, * wrt; int nreaders; /* initialized to 1 */ /* initialized to 0 */ Reader: P(mutex); nreaders = nreaders + 1; if (nreaders == 1) P(wrt); V(mutex); do the reading .... P(mutex); nreaders = nreaders - 1; if (nreaders = 0) V(wrt); V(mutex); Writer: P(wrt); do the writing ... V(wrt); 16 CPSC-410/611 Operating Systems Process Synchronization Incorrect Implementation of Readers/Writers monitor ReaderWriter{ int numberOfReaders = 0; int numberOfWriters = 0; boolean busy = FALSE; /* READERS */ procedure startRead() { while (numberOfWriters != 0); numberOfReaders = numberOfReaders + 1; } procedure finishRead() { numberOfReaders = numberOfReaders - 1; } /* WRITERS */ procedure startWrite() { numberOfWriters = numberOfWriters + 1; while (busy || (numberOfReaders > 0)); busy = TRUE; }; procedure finishWrite() { numberOfWriters = numberOfWriters - 1; busy = FALSE; }; }; A Correct Implementation monitor ReaderWriter{ int numberOfReaders = 0; int numberOfWriters = 0; boolean busy = FALSE; condition okToRead, okToWrite; /* READERS */ procedure startRead() { if (busy || (okToWrite.lqueue)) okToRead.wait; numberOfReaders = numberOfReaders + 1; okToRead.signal; } procedure finishRead() { numberOfReaders = numberOfReaders - 1; if (numberOfReaders = 0) okToWrite.signal; } /* WRITERS */ procedure startWrite() { if (busy || (numberOfReaders > 0)) okToWrite.wait; busy = TRUE; }; procedure finishWrite() { busy = FALSE; if (okToWrite.lqueue) okToWrite.signal; else okToRead.signal; }; }; 17 CPSC-410/611 Operating Systems Process Synchronization Process Management: Synchronization Why? How? Examples Software solutions Hardware-supported solutions What? The Critical Section Problem The basic synchronization mechanism: Semaphores Classical synchronization problems More sophisticated synchronization mechanisms: Monitors, Message Passing Higher-Level Synchronization Primitives Semaphores as the "GOTO" among the synchronization primitives. very powerful, but tricky to use. Need higher-abstraction primitives, for example: Monitors synchronized primitive in JAVA Protected Objects (Ada95) Conditional Critical Regions Message Passing 18 CPSC-410/611 Operating Systems Process Synchronization Monitors (Hoare / Brinch Hansen, 1973) Safe and effective sharing of abstract data types among several processes. Monitors can be modules, or objects. local variable accessible only through monitor's procedures process can entrer monitor only by invoking monitor procedure Only one process can be active in monitor. Additional synchronization through conditions (similar to semaphores) Condition c; c.cwait() : suspend execution of calling process and enqueue it on condition c. The monitor now is available for other processes. c.csignal() : resume a process enqueued on c. If none is enqueued, do nothing. cwait/csignal different from P/V: cwait always waits, csignal does nothing if nobody waits. Structure of Monitor local (shared) data c1 procedure 1 procedure 2 ... cm urgent queue procedure k operations initialization code blocked processes ... 19 CPSC-410/611 Operating Systems Process Synchronization Example: Binary Semaphore monitor BinSemaphore { bool condition locked; /* Initialize to FALSE */ idle; entry void P() { if (locked) idle.cwait(); locked = TRUE; } entry void ...
MOST POPULAR MATERIALS FROM CSE 410
MOST POPULAR MATERIALS FROM CSE
MOST POPULAR MATERIALS FROM Texas A&M