Concurrency: Mutual Exclusion and Synchronization Chapter 5.

Slides:



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

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Seventh Edition By William Stallings.
Concurrency: mutual exclusion and synchronization Slides are mainly taken from «Operating Systems: Internals and Design Principles”, 8/E William Stallings.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Seventh Edition By William Stallings.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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 
Computer Systems/Operating Systems - Class 8
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Enforcing Mutual Exclusion Message Passing. Peterson’s Algorithm for Processes P0 and P1 void P0() { while( true ) { flag[ 0 ] = false; /* remainder */
Concurrency: Mutual Exclusion and Synchronization Why we need Mutual Exclusion? Classical examples: Bank Transactions:Read Account (A); Compute A = A +
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
Concurrency: Mutual Exclusion and Synchronization
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Operating System 5 CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZATION.
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 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
Concurrency, Mutual Exclusion and Synchronization.
Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
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.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
3 Chapter 5. Theme of OS Design? Management of processes and threads Multiprogramming Multiprocessing Distributed processing.
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.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-4 Process Communication Department of Computer Science and Software.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Background on the need for Synchronization
G.Anuradha Reference: William Stallings
Concurrency.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
OPERATING SYSTEM CONCEPTS
Concurrency These slides are intended to help a teacher develop a presentation. This PowerPoint covers the entire chapter and includes too many slides.
Concurrency: Mutual Exclusion and Synchronization
Critical Section and Critical Resources
Chapter 5: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
2P13 Week 3.
Operating System 5 CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZATION
“The Little Book on Semaphores” Allen B. Downey
Operating Systems Concepts
Chapter 5 Mutual Exclusion(互斥) and Synchronization(同步)
Presentation transcript:

Concurrency: Mutual Exclusion and Synchronization Chapter 5

Concurrency Communication among processes Sharing of and competing for resources Synchronization of multiple processes Allocation of processor time

Concurrency Multiple applications –Multiprogramming Structured application –Application can be a set of concurrent processes Operating-system structure –Operating system is a set of processes or threads

Difficulties with Concurrency Sharing global resources Management of allocation of resources Programming errors difficult to locate

A Simple Example void echo() { in = getchar(); chout = in; putchar(chout); }

A Simple Example Process P1Process P2. in = getchar();.. in = getchar();. chout = in;. putchar(chout); chout = in;. putchar(chout);.

A Simple Example Process P1Process P2. in = getchar();.. in = getchar(); chout = in; putchar(chout);.. putchar(chout);.

Race condition A situation in which multiple threads or processes read and write a shared data item and the final result depends on the relative timing of their execution.

Operating System Concerns Keep track of active processes Allocate and deallocate resources –Processor time –Memory –Files –I/O devices Protect data and resources Result of process must be independent of the speed of execution of other concurrent processes

Process Interaction Processes unaware of each other: –Independent process not intended to work together. Competition. Processes indirectly aware of each other: –Share access to some object, such as an I/O buffer. Cooperation. Process directly aware of each other: –Communicate each other by PID and are designed to work jointly. Cooperation.

Competition Among Processes for Resources Three control problems: –Mutual Exclusion Critical sections –Only one program at a time is allowed in its critical section –Example only one process at a time is allowed to send command to the printer –Deadlock –Starvation

Mutual exclusion Requirement that when one process is in a critical section that accesses shared resources, no other process may be in a critical section that accesses any of those shared resources.

Critical section Section of code within a process Requires access to shared resources May not be executed while another process is in a corresponding section of code.

Deadlock Two or more processes are unable to proceed because each is waiting for one of the others to do something.

Starvation Runnable process is overlooked indefinitely by the scheduler. Although it is able to proceed, it is never chosen.

Cooperation Among Processes by Sharing Processes interact with other processes without being explicitly aware of them. Writing must be mutually exclusive Critical sections are used to provide data integrity –entercritical() –exitcritical()

Cooperation Among Processes by Communication Messages are passed –Mutual exclusion is not a control requirement Possible to have deadlock –Each process waiting for a message from the other process Possible to have starvation –Two processes sending message to each other while another process waits for a message

Requirements for Mutual Exclusion Only one process at a time is allowed in the critical section for a resource A process that halts in its non-critical section must do so without interfering with other processes No deadlock or starvation

Requirements for Mutual Exclusion A process must not be delayed access to a critical section when there is no other process using it No assumptions are made about relative process speeds or number of processes A process remains inside its critical section for a finite time only

First Attempt Busy Waiting –Process is always checking to see if it can enter the critical section –Process can do nothing productive until it gets permission to enter its critical section

Second Attempt Each process can examine the other’s status but cannot alter it When a process wants to enter the critical section it checks the other processes first If no other process is in the critical section, it sets its status for the critical section This method does not guarantee mutual exclusion Each process can check the flags and then proceed to enter the critical section at the same time

Correct Solution Each process gets a turn at the critical section If a process wants the critical section, it sets its flag and may have to wait for its turn

Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent Wait (Proberen) and signal (Verhogen) operations cannot be interrupted Queue is used to hold processes waiting on the semaphore

Semaphores Semaphore is a variable that has an integer value –May be initialized to a nonnegative number –Wait operation decrements the semaphore value, if the value becomes negative, the process is blocked. –Signal operation increments semaphore value. If the value is less than or equal to zero, then a process blocked by semWait is unblocked.

Semaphores Binary semaphore or mutex –May be initialized to 0 or 1 –Wait operation checks the semaphore value. If the value is 0 the process is blocked. If the value is 1 then the value is changed to 0 and the process continues. –Signal operation checks to see if any process are blocked on this semaphore. If so, then a process is unblocked. If no processes are blocked, then the value of the semaphore is set to 1.

Semaphores A semaphore whose definition includes the FIFO policy is called strong semaphore. A weak semaphore does not specify the order in which the processes are removed from the queue.

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 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 */ }

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

Monitors Monitor supports synchronization by the use of conditional variables that are contained within the monitor and accessible only within the monitor.

Monitors Condition variables are operated by: –cwait(c): Suspend execution of the calling process on condition c. The monitor is available for use by another process. –csignal(c): Resume execution of some process blocked after a cwait on the same condition.

Monitors Advantages: –All of the synchronization functions are confined to the monitor. –Easier to verify that the synchronization has been done correctly. –Easier to detect bugs. –Once a monitor is correctly programmed, access to the protected resource is correct for access from all process. Contrast: With semaphores, resource access is correct only if all of the process that access the resource are programmed correctly.

Message Passing Enforce mutual exclusion Cooperating processes may need to exchange information Lends itself to implementation in Distributed systems, shared-memory multiprocessor and uniprocessor 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 –Tight synchronization.

Synchronization Nonblocking send, blocking receive –Sender continues processing such as sending messages as quickly as possible –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 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

Message Format

Dining Philosophers Problem