© 2004, D. J. Foreman 1 Mutual Exclusion © 2004, D. J. Foreman 2  Mutual exclusion ■ Critical sections ■ Primitives  Implementing it  Dekker's alg.

Slides:



Advertisements
Similar presentations
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
Advertisements

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: 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.
Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
China’s Software Industry August 2006 Instructor: Hengming Zou, Ph.D.
Producer-Consumer One common situation: Producers and consumers communicate via a buffer.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
OS Spring’04 Concurrency Operating Systems Spring 2004.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Classical Synchronization Problems. Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread.
IPC and Classical Synchronization Problems
Slide 8-1 Copyright © 2004 Pearson Education, Inc. Basic Synchronization Principles.
Basic Synchronization Principles. Concurrency Value of concurrency – speed & economics But few widely-accepted concurrent programming languages (Java.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Classical Synchronization Problems. Announcements CS 414 grades and solutions available in CMS soon. –Average 74.3 –High of 95. –Score out of 100 pts.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Dining Philosophers, Monitors, and Condition Variables CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency, Mutual Exclusion and 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.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1.  Independent process  Cannot affect or be affected by the other processes in the system  Does not share any data with other processes  Interacting.
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
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.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Basic Synchronization Principles 1. 2  Independent process  Cannot affect or be affected by the other processes in the system  Does not share any.
Process Synchronization 1. while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced;
Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
More on Classic Synchronization Problems, Monitors, and Deadlock
Chapter 5: Process Synchronization
Basic Synchronization Principles
Basic Synchronization Principles
The Critical-Section Problem (Two-Process Solution)
Topic 6 (Textbook - Chapter 5) Process Synchronization
Lecture 2 Part 2 Process Synchronization
Steve’s Concurrency Slides
Outline Announcements Basic Synchronization Principles – continued
Chapter 6: Synchronization Tools
CIS 720 Lecture 6.
Outline Please turn in Homework #2 Announcements
Steve’s Concurrency Slides
Operating Systems {week 10}
Presentation transcript:

© 2004, D. J. Foreman 1 Mutual Exclusion

© 2004, D. J. Foreman 2  Mutual exclusion ■ Critical sections ■ Primitives  Implementing it  Dekker's alg  Peterson's alg  Lamport's alg  H/W solutions  Intro to semaphores

© 2004, D. J. Foreman 3 Dijkstra Semaphore  Invented in the 1960s  Conceptual OS mechanism, with no specific implementation defined (could be enter() / exit() )  Basis of all contemporary OS synchronization mechanisms

© 2004, D. J. Foreman 4 Solution Constraints  Processes p 0 & p 1 enter critical sections  Mutual exclusion: Only one process at a time in the CS  Only processes competing for a CS are involved in resolving who enters the CS  Once a process attempts to enter its CS, it cannot be postponed indefinitely  After requesting entry, only a bounded number of other processes may enter before the requesting process

© 2004, D. J. Foreman 5 Notation  Let fork(proc, N, arg1, arg2, …, argN)be a command to create a process, and to have it execute using the given N arguments  Canonical problem: Proc_0() {proc_1() { while(TRUE) { while(TRUE { <compute section>; <critical section>; }} fork(proc_0, 0); fork(proc_1, 0);

© 2004, D. J. Foreman 6 Solution Assumptions  Memory read/writes are indivisible (simultaneous attempts result in some arbitrary order of access)  There is no priority among the processes  Relative speeds of the processes/processors is unknown  Processes are cyclic and sequential

© 2004, D. J. Foreman 7 Sharing Two Variables proc_A() { while(TRUE) { ; update(x); /* Signal proc_B */ V(s1); ; /* Wait for proc_B */ P(s2); retrieve(y); } semaphore s1 = 0; semaphore s2 = 0; fork(proc_A, 0); fork(proc_B, 0); proc_B() { while(TRUE) { /* Wait for proc_A */ P(s1); retrieve(x); ; update(y); /* Signal proc_A */ V(s2); ; }

© 2004, D. J. Foreman 8 Device Controller Synchronization  The semaphore principle is logically used with the busy and done flags in a controller  Driver signals controller with a V(busy), then waits for completion with P(done)  Controller waits for work with P(busy), then announces completion with V(done)

© 2004, D. J. Foreman 9 Bounded Buffer Problem Producer Consumer Empty Pool Full Pool

© 2004, D. J. Foreman 10 Bounded Buffer Problem (2) producer() { buf_type *next, *here; while(TRUE) { produce_item(next); /* Claim an empty */ P(empty); P(mutex); here = obtain(empty); V(mutex); copy_buffer(next, here); P(mutex); release(here, fullPool); V(mutex); /* Signal a full buffer */ V(full); } semaphore mutex = 1; semaphore full = 0; /* A general (counting) semaphore */ semaphore empty = N; /* A general (counting) semaphore */ buf_type buffer[N]; fork(producer, 0); fork(consumer, 0); consumer() { buf_type *next, *here; while(TRUE) { /* Claim full buffer */ P(mutex); P(full); here = obtain(full); V(mutex); copy_buffer(here, next); P(mutex); release(here, emptyPool); V(mutex); /* Signal an empty buffer */ V(empty); consume_item(next); }

© 2004, D. J. Foreman 11 Bounded Buffer Problem (3) producer() { buf_type *next, *here; while(TRUE) { produce_item(next); /* Claim an empty */ P(empty); P(mutex); here = obtain(empty); V(mutex); copy_buffer(next, here); P(mutex); release(here, fullPool); V(mutex); /* Signal a full buffer */ V(full); } semaphore mutex = 1; semaphore full = 0; /* A general (counting) semaphore */ semaphore empty = N; /* A general (counting) semaphore */ buf_type buffer[N]; fork(producer, 0); fork(consumer, 0); consumer() { buf_type *next, *here; while(TRUE) { /* Claim full buffer */ P(full); P(mutex); here = obtain(full); V(mutex); copy_buffer(here, next); P(mutex); release(here, emptyPool); V(mutex); /* Signal an empty buffer */ V(empty); consume_item(next); }

© 2004, D. J. Foreman 12 Implementing Semaphores  Minimize effect on the I/O system  Processes are only blocked on their own critical sections (not critical sections that they should not care about)  If disabling interrupts, be sure to bound the time they are disabled

© 2004, D. J. Foreman 13 Implementing Semaphores: enter() & exit() class semaphore { int value; public: semaphore(int v = 1) { value = v;}; P(){ disableInterrupts(); while(value == 0) { enableInterrupts(); disableInterrupts(); } value--; enableInterrupts(); }; V(){ disableInterrupts(); value++; enableInterrupts(); };

© 2004, D. J. Foreman 14 Implementing Semaphores: Test and Set Instruction FALSE m Primary Memory … R3 … Data Register CC Register (a)Before Executing TS TRUE m Primary Memory FALSE R3 =0 Data Register CC Register (b) After Executing TS  TS(m): [Reg_i = memory[m]; memory[m] = TRUE;]

© 2004, D. J. Foreman 15 Using the TS Instruction boolean s = FALSE;... while(TS(s)) ; s = FALSE;... semaphore s = 1;... P(s) ; V(s);...

© 2004, D. J. Foreman 16 Implementing the General Semaphore struct semaphore { int value = ; boolean mutex = FALSE; boolean hold = TRUE; }; shared struct semaphore s; P(struct semaphore s) { while(TS(s.mutex)) ; s.value--; if(s.value < 0) ( s.mutex = FALSE; while(TS(s.hold)) ; } else s.mutex = FALSE; } V(struct semaphore s) { while(TS(s.mutex)) ; s.value++; if(s.value <= 0) ( while(!s.hold) ; s.hold = FALSE; } s.mutex = FALSE; }

© 2004, D. J. Foreman 17 Active vs Passive Semaphores  A process can dominate the semaphore ■ Performs V operation, but continues to execute ■ Performs another P operation before releasing the CPU ■ Called a passive implementation of V  Active implementation calls scheduler as part of the V operation. ■ Changes semantics of semaphore! ■ Cause people to rethink solutions