Module 7a: Classic Synchronization

Slides:



Advertisements
Similar presentations
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Advertisements

Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Synchronization Principles Gordon College Stephen Brinton.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Process Synchronization
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
What we will cover… Process Synchronization Basic Concepts
02/11/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CSE Operating System Principles Synchronization.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
Chapter 6: Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
Chapter 5: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Chapter 6: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Synchronization Tools
Presentation transcript:

Module 7a: Classic Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors

Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Shared-memory solution to bounded-butter problem (Chapter 4) has a race condition on the class data count.

Race Condition The Producer calls while (true) { while (count == BUFFER_SIZE) ; // do nothing // produce an item and put in nextProduced buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; }

Race Condition The Consumer calls while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; // consume the item in nextConsumed }

Race Condition count++ 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: S0: producer execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4}

Critical-Section In concurrent programming a critical section is a piece of code that accesses a shared resource that must not be concurrently accessed by more than one thread of execution. That part of the program where the shared memory is accessed is called the Critical Section. To avoid race conditions and flawed results, one must identify codes in Critical Sections  in each thread.

Solution to Critical-Section Problem 1. 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 only those processes that are not executing in their remainder sections can participate in the decision on which will enter its critical section next, and this selection 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

Algorithm 1 Threads share a common integer variable turn If turn==i, thread i is allowed to execute Does not satisfy progress requirement Why?

Two-task Solution ALGORITHM NO. 1 Let the processes share common variable turn. If turn = 0 process 0 will enter if turn = 1 process 1 will enter the critical section CODE FOR PROCESS i Int turn; Do{ while (turn != i); critical section turn = j; remainder section }while(true); MUTUAL EXCLUSION IS PRESERVED PROGRESS REQUIREMENT NOT SATISFIED THERE IS A STRICT ALTERNATION

Algorithm 2 Add more state information Boolean flags to indicate thread’s interest in entering critical section Progress requirement still not met Why?

Two-task Solution ALGORITHM NO. 2 CODE FOR PROCESS i Boolean flag[2]={false, false}; Do{ flag [i]= true; while (flag[ j]); critical section flag [i] =false; remainder section }while (true); MUTUAL EXCLUSION IS PRESERVED PROGRESS REQUIREMENT NOT SATISFIED DEAD LOCK IS POSSIBLE SWITCHING THE ORDER OF SETTING AND TESTING WILL NOT SOLVE THE PROBLEM RATHER MUTUAL EXCLUSION WILL BE VIOLATED

T0: Po sets flag [0]=true T1: P1 sets flag[1]= true Note that now Po and P1 are looping forever in their respective while statements

Algorithm 3 Combine ideas from 1 and 2 Does it meet critical section requirements?

Multiple Process Solution CODE FOR PROCESS i Boolean choosing [n]; //Initialized to false Int number [n]; // Initialized to 0 (a ,b) < (c ,d) if a<c or a ==c and b< d Max(a0 ,a1, a2, …, an-1)is a number k, such that k>= ai for i=0,1, 2, 3.. n-1 Do{ choosing [I]= true; number [I] =max(number [0] ,number [1], ……. ,Number [n-1] ) +1; choosing [I]= false; for (J =0;J<n ;j ++){ while( choosing [J] ) ; // IF THE PROCESS J IS GETTING NUMBER while ( ( number [J] !=0 ) && (number [J]) < (number [I] ) ; // IF J HAS GOT A NUMBER AND NO OF J IS LESS critical section number [I] =0; remainder section }while (true);

Synchronization Hardware Many systems provide hardware support for critical section code Uniprocessors – could disable interrupts Currently running code would execute without preemption Generally too inefficient on multiprocessor systems Operating systems using this not broadly scalable Modern machines provide special atomic hardware instructions Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words

Synchronization Hardware TEST AND SET INSTRUCTION SETS THE ARGUMENT TO TRUE AND RETURNS THE OLD VALUE Boolean lock=false; Do{ while ( TestAndSet (lock ) ); critical section lock = false; remainder section }while (true) BOUNDED WAITING REQUIREMENT IS NOT SATISFIED

Synchronization Hardware SWAP INSTRUCTION SWAPS THE CONTENTS OF TWO MEMORY WORDS Boolean lock=false; Do{ Key =true; while ( key == true) swap ( lock , key ); critical section lock = false; remainder section }while (true) BOUNDED WAITING REQUIREMENT IS NOT SATISFIED

Synchronization Hardware TEST AND SET INSTRUCTION Boolean waiting [ n]; // initialized to false Boolean lock ; // initialized to false Do{ waiting [i] =true; key = true; while ( waiting [ i ] && key ) key = TestAndSet ( lock ); waiting [ i ]= false; critical section j = (i+1) % n; while ( ( j!=i) && !wating [j]) j= (j +1) % n; if ( j==i) lock =false; else waiting [ j]=false; remainder section }while (true)

Semaphore Synchronization tool that does not require busy waiting (spin lock) Semaphore S – integer variable Two standard operations modify S: acquire() and release() Originally called P() and V() Less complicated Can only be accessed via two indivisible (atomic) operations acquire(S) { while S <= 0 ; // no-op S--; } release(S) { S++;

Semaphore as General Synchronization Tool Counting semaphore – integer value can range over an unrestricted domain Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement Also known as mutex locks Can implement a counting semaphore S as a binary semaphore Provides mutual exclusion Semaphore S; // initialized to 1 acquire(S); criticalSection(); release(S);

Semaphore Implementation Must guarantee that no two processes can execute acquire() and release() on the same semaphore at the same time Thus implementation becomes the critical section problem Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied Applications may spend lots of time in critical sections Performance issues addressed throughout this lecture

Semaphore Implementation Must guarantee that no two processes can execute acquire() and release() on the same semaphore at the same time Thus implementation becomes the critical section problem Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied Applications may spend lots of time in critical sections Performance issues addressed throughout this lecture

Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P0 P1 acquire(S); acquire(Q); acquire(Q); acquire(S); . . release(S); release(Q); release(Q); release(S); Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.

Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

Dining-Philosophers Problem Shared data Semaphore chopStick[] = new Semaphore[5];

Dining-Philosophers Problem (Cont.) Philosopher i: while (true) { // get left chopstick chopStick[i].acquire(); // get right chopstick chopStick[(i + 1) % 5].acquire(); eating(); // return left chopstick chopStick[i].release(); // return right chopstick chopStick[(i + 1) % 5].release(); thinking(); }

Monitors A monitor is a high-level abstraction that provides thread safety Only one thread may be active within the monitor at a time monitor monitor-name { // variable declarations public entry p1(…) { … } public entry p2(…) {

Condition Variables condition x, y; A thread that invokes x.wait is suspended until another thread invokes x.signal

Monitor with condition variables

Condition Variable Solution to Dining Philosophers monitor DiningPhilosophers { int[] state = new int[5]; static final int THINKING = 0; static final int HUNGRY = 1; static final int EATING = 2; condition[] self = new condition[5]; public diningPhilosophers { for (int i = 0; i < 5; i++) state[i] = THINKING; } public entry pickUp(int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait; // Continued on Next Slide

Solution to Dining Philosophers (cont) public entry putDown(int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); } private test(int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING; self[i].signal;