Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Interprocess Communication
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Seventh Edition By William Stallings.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Chapter 2.3 : Interprocess Communication
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 4.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating System Chapter 5. Concurrency: Mutual Exclusion and Synchronization Lynn Choi School of Electrical Engineering.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Background on the need for Synchronization
G.Anuradha Reference: William Stallings
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5 Mutual Exclusion(互斥) and Synchronization(同步)
Presentation transcript:

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall

Concurrency Multiple applications Structured applications Operating system structure

Key Terms

Difficulties of Concurrency Sharing of global resources – coordinated access to shared resources Operating system managing the allocation of resources optimally – do we know the future behaviour (needed resources) of processes? Difficult to locate programming errors

A Simple Example for Two Processes Process P1Process P2. chin = getchar();..chin = getchar(); chout = chin;. chout = chin; putchar(chout);..putchar(chout);.

Race Conditions Two processes want to access shared memory at the same time. The final result depends on who runs precisely when.

Potential Problems Data incoherency Deadlock: processes are frozen because of mutual dependency on each other Starvation: some of the processes are unable to make progress (i.e., to execute useful code)

Critical Regions Mutual exclusion using critical regions

Mutual exclusion Critical region: part of the program where shared memory is accessed. Four conditions for correct and efficient communication : 1. Mutual exclusion: No two processes simultaneously in critical region 2. No assumptions made about speeds or numbers of CPUs 3. Progress: No process running outside its critical region may block another process 4. Fairness, i.e., no starvation: No process must wait forever to enter its critical region (assuming fair scheduling!)

Strict Alternation Proposed solution to critical region problem (a) Process 0. (b) Process 1.

Petersons Solution Interested(process)=False => process is not in and does not want to enter critical section If both are interested, a process can enter only if it is the others turn.

Mutual Exclusion: Disabling Interrupts A process runs until it invokes an operating system service or until it is interrupted Disabling interrupts guarantees mutual exclusion Processor is limited in its ability to interleave programs Will not work in multiprocessor architecture Should a user process be allowed to disable interrupts ?

Mutual Exclusion: Compare&Swap Compare&Swap Instruction int compare_and_swap (int *word, int testval, int newval) { int oldval; oldval = *word; if (oldval == testval) *word = newval; return oldval; }

Mutual Exclusion: Exchange Exchange instruction void exchange (int register, int memory) { int temp; temp = memory; memory = register; register = temp; }

Mutual Exclusion

Mutual Exclusion Machine- Instruction: Advantages –Applicable to any number of processes on either a single processor or multiple processors sharing main memory –It is simple and therefore easy to verify –It can be used to support multiple critical sections

Mutual Exclusion Machine- Instruction: Disadvantages –Busy-waiting consumes processor time –Livelock is possible –Priority inversion problem

Semaphores Special variable called a semaphore is used for signalling If a process is waiting for a signal, it is blocked until that signal is sent

Semaphore Operations Semaphore is a variable that has an integer value –May be initialized to a nonnegative number –Wait (down) operation decrements semaphore value; if value negative, process is blocked –Signal (up) operation increments semaphore value; one of the blocked processes is unblocked

Semaphore Primitives

Binary Semaphore - Mutex

Mutual Exclusion Using Semaphores

Example of Semaphore Mechanism

Processes Using Semaphore

Producer/Consumer Problem One or more producers are generating data and placing these in a buffer A single consumer is taking items out of the buffer one at time Only one producer or consumer may access the buffer at any one time Producer cant add data into full buffer and consumer cant remove data from empty buffer

Producer producer: while (true) { /* produce item v */ b[in] = v; in++; }

Consumer consumer: while (true) { while (in <= out) /*do nothing */; w = b[out]; out++; /* consume item w */ }

Buffer

Incorrect Solution

Correct Solution with Binary Semaphores

Correct Solution with Counting Semaphores

Producer with Circular Buffer producer: while (true) { /* produce item v */ while ((in + 1) % n == out) /* do nothing */; b[in] = v; in = (in + 1) % n }

Consumer with Circular Buffer consumer: while (true) { while (in == out) /* do nothing */; w = b[out]; out = (out + 1) % n; /* consume item w */ }

Circular Buffer

Correct Solution with Bounded Buffer

Monitors Monitor is a software module Chief characteristics –Local data variables are accessible only by the monitor –Process enters monitor by invoking one of its procedures –Only one process may be executing in the monitor at a time

Structure of a Monitor

Solution Using Monitor

Solution Using Monitor (cont.)

Message Passing Enforce mutual exclusion Exchange information send (destination, message) receive (source, message)

Synchronization Sender and receiver may or may not be blocking (waiting for message) Blocking send, blocking receive –Both sender and receiver are blocked until message is delivered –Called a rendezvous

Synchronization (2) Nonblocking send, blocking receive –Sender continues on –Receiver is blocked until the requested message arrives Nonblocking send, nonblocking receive –Neither party is required to wait

Addressing Direct addressing –Send primitive includes a specific identifier of the destination process –Receive primitive could know ahead of time from which process a message is expected –Receive primitive could use source parameter to return a value when the receive operation has been performed

Addressing (2) Indirect addressing –Messages are sent to a shared data structure consisting of queues –Queues are called mailboxes –One process sends a message to the mailbox and the other process picks up the message from the mailbox

General Message Format

Mutual Exclusion Using Messages (blocking receive)

Producer/Consumer Messages

Readers/Writers Problem Any number of readers may simultaneously read the file Only one writer at a time may write to the file If a writer is writing to the file, no reader may read it

Readers Have Priority

Writers Have Priority

Message Passing