Critical Section and Critical Resources

Slides:



Advertisements
Similar presentations
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Advertisements

Ch 7 B.
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.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
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.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
6/12/2015Page 1 Inter-Process Communication and Synchronization B. Ramamurthy.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
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.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
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 Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
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.
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.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
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 5.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
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.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Chapter 6: Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Xinu Semaphores.
Chapter 6: Process Synchronization
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy cse321-fall2014 9/20/2018.
Chapter 6: Process Synchronization
Inter-Process Communication and Synchronization
Concurrency: Mutual Exclusion and Synchronization
Critical Section and Critical Resources
Chapter 5: Process Synchronization
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy cse321-fall /27/2018.
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Module 7a: Classic Synchronization
Critical section problem
Concurrency: Mutual Exclusion and Process Synchronization
Mutual Exclusion, Synchronization and Classical InterProcess Communication (IPC) Problems B.Ramamurthy CSE421 2/18/2019 B.Ramamurthy.
Chapter 6 Synchronization Principles
Concurrency, Mutual Exclusion and Synchronization
Presentation transcript:

Critical Section and Critical Resources B. Ramamurthy

Issues in Concurrency Concurrent processes /threads help in improving performance However when they share resources, it is required that the access to the resource is protected from inconsistency and incorrect states. These shared resources are called critical resources and the code accessing the resource is called the critical section. The access to the resource needs to be mutually exclusive among concurrent processes. Semaphores are kernel level primitives to help implement mutual exclusion.

Principles of Concurrency Interleaving and overlapping the execution of processes. Consider two processes P1 and P2 executing the function echo: { input (in, keyboard); out = in; output (out, display); } 11/28/2018

...Concurrency (contd.) P1 invokes echo, after it inputs into in , gets interrupted (switched). P2 invokes echo, inputs into in and completes the execution and exits. When P1 returns in is overwritten and gone. Result: first ch is lost and second ch is written twice. This type of situation is even more probable in multiprocessing systems where real concurrency is realizable thru’ multiple processes executing on multiple processors. Solution: Controlled access to shared resource Protect the shared resource : in buffer; “critical resource” one process/shared code. “critical region” 11/28/2018

Semaphores Semaphore is kernel level primitive for realizing mutual exclusion. Attributes: semaphore value, Operations on semaphore: init, wait, signal Considered an kernel resource, a limited number available: a limited number of instances (objects) of semaphore is allowed. Can easily implement mutual exclusion among any number of processes. Can also be used for synchronization. 11/28/2018

Critical Section of n Processes Shared data: Semaphore mutex; //initially mutex = 1 Process Pi: do { mutex.wait(); critical section mutex.signal(); remainder section } while (1); 11/28/2018

Semaphore Implementation Define a semaphore as a class: class Semaphore { int value; // semaphore value ProcessQueue L; // process queue //operations wait() signal() } In addition, two simple utility operations: block() suspends the process that invokes it. Wakeup() resumes the execution of a blocked process P. 11/28/2018

Semantics of wait and signal Semaphore operations now defined as S.wait(): S.value--; if (S.value < 0) { add this process to S.L; block(); // block a process } S.signal(): S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(); // wake a process 11/28/2018

Semaphores for CS Semaphore is initialized to 1. The first process that executes a wait() will be able to immediately enter the critical section (CS). (S.wait() makes S value zero.) Now other processes wanting to enter the CS will each execute the wait() thus decrementing the value of S, and will get blocked on S. (If at any time value of S is negative, its absolute value gives the number of processes waiting blocked. ) When a process in CS departs, it executes S.signal() which increments the value of S, and will wake up any one of the processes blocked. The queue could be FIFO or priority queue. 11/28/2018

Two Types of Semaphores 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. ex: nachos Can implement a counting semaphore using a binary semaphore. 11/28/2018

Semaphore for Synchronization Execute B in Pj only after A executed in Pi Use semaphore flag initialized to 0 Code: Pi Pj   A flag.wait() flag.signal() B 11/28/2018

Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem 11/28/2018

Producer/Consumer problem repeat produce item v; b[in] = v; in = in + 1; forever; Consumer repeat while (in <= out) nop; w = b[out]; out = out + 1; consume w; forever; 11/28/2018

Solution for P/C using Semaphores Producer repeat produce item v; MUTEX.wait(); b[in] = v; in = in + 1; MUTEX.signal(); forever; What if Producer is slow or late? Consumer repeat while (in <= out) nop; MUTEX.wait(); w = b[out]; out = out + 1; MUTEX.signal(); consume w; forever; Ans: Consumer will busy-wait at the while statement. 11/28/2018

P/C: improved solution Producer repeat produce item v; MUTEX.wait(); b[in] = v; in = in + 1; MUTEX.signal(); AVAIL.signal(); forever; What will be the initial values of MUTEX and AVAIL? Consumer repeat AVAIL.wait(); MUTEX.wait(); w = b[out]; out = out + 1; MUTEX.signal(); consume w; forever; ANS: Initially MUTEX = 1, AVAIL = 0. 11/28/2018

P/C problem: Bounded buffer Producer repeat produce item v; while((in+1)%n == out) NOP; b[in] = v; in = ( in + 1)% n; forever; How to enforce bufsize? Consumer repeat while (in == out) NOP; w = b[out]; out = (out + 1)%n; consume w; forever; ANS: Using another counting semaphore. 11/28/2018

P/C: Bounded Buffer solution Producer repeat produce item v; BUFSIZE.wait(); MUTEX.wait(); b[in] = v; in = (in + 1)%n; MUTEX.signal(); AVAIL.signal(); forever; What is the initial value of BUFSIZE? Consumer repeat AVAIL.wait(); MUTEX.wait(); w = b[out]; out = (out + 1)%n; MUTEX.signal(); BUFSIZE.signal(); consume w; forever; ANS: size of the bounded buffer. 11/28/2018

Semaphores - comments Intuitively easy to use. wait() and signal() are to be implemented as atomic operations. Difficulties: signal() and wait() may be exchanged inadvertently by the programmer. This may result in deadlock or violation of mutual exclusion. signal() and wait() may be left out. Related wait() and signal() may be scattered all over the code among the processes. 11/28/2018