Chapter 6 Synchronization Principles

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch 7 B.
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.
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.
Intertask Communication and Synchronization In this context, the terms “task” and “process” are used interchangeably.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
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.
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.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
CS3530 OPERATING SYSTEMS Summer 2014 Synchronization Chapter 6.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
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.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
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.
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.
Web Server Architecture Client Main Thread for(j=0;j
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
OPERATING SYSTEMS CS 3502 Fall 2017
Chapter 6: Process Synchronization
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
Process Synchronization
Classical Synchronization Problems
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Inter-Process Communication and Synchronization
Critical Section and Critical Resources
Chapter 5: Process Synchronization
Critical Section and Critical Resources
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Monitor Giving credit where it is due:
CSE 153 Design of Operating Systems Winter 2019
Chapter 6: Synchronization Tools
“The Little Book on Semaphores” Allen B. Downey
CSE 542: Operating Systems
Presentation transcript:

Chapter 6 Synchronization Principles

6.1 Introduction Synchronization as a Concept The coordination of the activities of the processes Processes compete for resources Processes cooperate

6.2.1 No synchronization Race Condition: Two or more processes access and manipulate the same data item together The outcome of the execution depends on the “speed” of the processes and the particular order in which each process accesses the shared data item Results are generally incorrect Race condition needs to be avoided.

6.2.2 Mutual Exclusion Only one process is allowed to access a shared resource, all other processes must wait. This condition is called mutual exclusion.

Mutual Exclusion A synchronization principle needed when a group of processes share a resource Each process should access the resource in a mutual exclusive manner This means only one process at a time can access a shared resource This is the most basic synchronization principle

Critical Section A critical section is a portion of code in a process, in which the process accesses a shared resource

6.2.3 Critical Section

Critical Sections in Two Processes

Mutual Exclusion and Critical Sections Coordinate the group of processes that access shared resources such that only one process can execute its critical section at any time During this time interval, the other processes are excluded from executing their critical sections

Example of Critical Sections Shared resource: Printer buffer Two processes: Producer: produces a character, places the character in buffer. Consumer: removes a character from the buffer, consumes the character. CRITICAL SECTION 2.2.4.1 The Producer-Consumer Problem The producer-consumer is not only a classical problem in Computer Science, but one that appears over and over again in all types of applications. Two process: Producer and consumer Producer process produces information which is consumed by the consumer Printer driver produces characters Line-printer consumes characters There are many ways to implement the buffer, It could be seen as a circular queue, whether as a sequential list or a linked list. The details of the access or implementation of the data structure are not important in this discussion. Whatever the implementation, access to the list (the producer depositing a character or the consumer obtaining a character) must be considered a critical section. Access to the list must be mutually exclusive. When the producer is accessing the list to deposit a character, the consumer must not be permitted to obtain a character. When the consumer is accessing the list to obtain a character, the producer must not be permitted to deposit a character.

6.3 Approaches for Implementing Synchronization Busy waiting Hardware support Operating system support

6.4 Semaphores A semaphore is similar to a traffic light A software synchronization tool that can be used in the critical section problem solution It is an abstract data type implemented as a class provided by the operating system.

Semaphores Objects Are objects that, must be initialized and can be manipulated only with two atomic operations: wait and signal.

Semaphore Class class Semaphore { private int sem; private Pqueue sem_q; // semaphore queue public Semaphore (int initval); public wait (); // P() public signal (); // V() } // end of class Semaphore

Creating a Semaphore Object // declaration of object ref Semaphore mutex; // create object and initialize mutex = new Semaphore (1);

Types of Semaphores Binary semaphore: the value of the integer attribute, sem, is either 0 or 1. Counting semaphore: the value of the attribute can take any integer value > 0

6.5 Synchronization with Semaphores Include synchronization mechanisms that regulate access to a shared resource, used in: the entry section exit section Are used to allow a process to execute its critical section when no other process is already in its critical section, thus providing exclusive access to a shared resource

6.5.1 Critical Section Problem Entry section check if any other process is executing its C.S., if so then the current process must wait otherwise set flags and proceed 2. Critical Section 3. Exit section clear flags to allow any waiting process to enter its critical section.

Entry and Exit Sections Implemented with a binary semaphore A semaphore object referenced by mutex, is used here with the two operations: wait and signal. mutex.wait(); [critical section] mutex.signal();

Processes Synchronized by Semaphores A waiting process is blocked and woken up later. Processes waiting on a semaphore are kept in the semaphore queue. The wait and signal operations are implemented using the system calls sleep() and wakeup().

6.5.2 Event Ordering Event ordering is a type of synchronization based on process cooperation, where the process exchange synchronization signals in order to coordinate the executions. // process P1 … write(x); exord.signal(); // process P2 … exord.wait(); read(x);

Example in Execution Ordering Assume two processes P1 and P2 need to synchronize their executions in the following order: in P1, write(x) must be executed before P2 executes read(x). This synchronization problem can be solved with semaphores The following is a solution that uses Semaphore exord, initialized to zero:

6.6 Synchronization Case Studies Bounded-buffer problem – also called producer/consumer problem Readers-writers problem Dining philosophers

6.6.1 Bounded-Buffer Problem There are two processes that execute continuously and a shared buffer The Producer process generates data items The Consumer process takes these data items and consumes them Buffer - a container of N slots, filled by the producer process and emptied by the consumer process

Producer-Consumer Problem

Producer Consumer Problem(2) Competition between the two processes to access the shared buffer Cooperation of the two processes in order to exchange data through the buffer

Synchronization The producer and consumer processes must be synchronized: Both processes attempt mutual exclusive access to the data buffer The producer must wait to insert a new data item if buffer is full The consumer process must wait to remove a data item if buffer is empty

Semaphores Used A binary semaphore for the mutual exclusive access to the data buffer A counting semaphore to count the number of full slots in the buffer A counting semaphore to count the number of empty slots in the buffer

Data Declarations for Solution // Shared data int N = 100; // size of buffer char buffer [N]; // buffer implementation char nextp, nextc; Semaphorec full, empty; // counting semaphores Semaphoreb mutex; // binary semaphore

Initializing Semaphore Objects full = new Semaphorec(0); // counting semaphore obj empty = new Semaphorec( N); // counting sem obj mutex = new Semaphoreb(1); // binary semaphore obj

Implementation of Producer Producer process while(true) { . . . // produce a data item empty.wait(); // any empty slots? Decrease empty slots mutex.wait(); // attempt exclusive access to buffer // instructions to insert data item into buffer mutex.signal(); // release exclusive access to buffer full.signal(); // increment full slots }

Implementation of Consumer Consumer process while(true) { . . . full.wait(); // any full slots? Decrease full slots mutex.wait(); // attempt exclusive access to buffer … // remove a data item from buffer and put in nextc mutex.signal(); // release exclusive access to buffer empty.signal(); // increment empty slots // consume the data item in nextc … }

6.6.2 Synchronization with Semaphores in Java The Java programming language has facilities for defining, creating, and manipulating Java threads. Java also provides a mechanism for basic exclusive access to shared resources. The wait/notify mechanism is used to synchronize Java threads. http://www.doc.ic.ac.uk/~jnm/book/book_applets/BoundedBuffer.html

6.9 IPC Mechanism A set of mechanisms provided by the OS that enable processes to exchange data Data is formatted as messages Transfer of messages requires synchronization Asynchronous communication (acomm) Synchronous communication (scomm)

6.9.1 Asynchronous Communication Similar to consumer-producer problem Indirect communication between sneder and receiver processes No direct interaction between processes A mailbox stores the message Sender does not need to wait (?) Receiver waits until message becomes available

Asynchronous Communication Mailboxes are used as intermediate storage of message. Mailboxes are maintained by OS.

6.9.2 Synchronous Communication Direct interaction between processes Processes have to wait until the communication occurs The processes have to participate at the same time during the communication interval A channel is established between the processes

Synchronous Communication Channels are the communication media to link two (or more) processes in order to transfer message from sender process to the receiver process.

6.11 Summary Synchronization Semaphores and monitors are most common used synchronization tools Case study: producer-consumer problem (also known as the bounded-buffer problem) IPC Asynchronous communication – mailbox Synchronous communication – channel