Process Synchronization

Similar documents
1. Motivation (Race Condition)

Chapter 6: Process Synchronization

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Chapter 6: Process Synchronization

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Process Synchronization

Chapter 7: Process Synchronization!

CSE 4/521 Introduction to Operating Systems

Interprocess Communication By: Kaushik Vaghani

Lesson 6: Process Synchronization

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

Synchronization Principles

Prof. Hui Jiang Dept of Computer Science and Engineering York University

Process Synchronization(2)

Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 7: Process Synchronization. Background

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

CS420: Operating Systems. Process Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Process Synchronization

Process Coordination

Process Synchronization(2)

CS370 Operating Systems

Process Synchronization

Process Management And Synchronization

Multitasking / Multithreading system Supports multiple tasks

Process Synchronization

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

CS370 Operating Systems

Chapter 6 Process Synchronization

Chapter 5: Process Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Concept of a process

Process Synchronization(2)

Process Co-ordination OPERATING SYSTEMS

Process Synchronization

Chapter 5: Process Synchronization

Concurrency. Chapter 5

OS Process Synchronization!

CSC501 Operating Systems Principles. Process Synchronization

Process Synchronization

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Synchronization. studykorner.org

Module 6: Process Synchronization

Dept. of CSE, York Univ. 1

CS370 Operating Systems

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Module 6: Process Synchronization

Chapter 6: Process Synchronization

IV. Process Synchronisation

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Chapter 6 Synchronization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

PROCESS SYNCHRONIZATION

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Introduction to Operating Systems

UNIT 2 Basic Concepts of CPU Scheduling. UNIT -02/Lecture 01

CS3733: Operating Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

High-level Synchronization

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Concurrency: Mutual Exclusion and Synchronization

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

Dealing with Issues for Interprocess Communication

Lecture 6. Process Synchronization

UNIT II PROCESS MANAGEMENT 9

CS604 - Operating System Solved Subjective Midterm Papers For Midterm Exam Preparation

Process Synchronization

Synchronization for Concurrent Tasks

Real-Time Operating Systems M. 5. Process Synchronization

Subject: Operating System (BTCOC403) Class: S.Y.B.Tech. (Computer Engineering)

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Process/Thread Synchronization

9/29/2014. CS341: Operating System Mid Semester Model Solution Uploaded Semaphore ADT: wait(), signal()

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

CSE Opera,ng System Principles

Chapter 7 Process Synchronization

CSE Traditional Operating Systems deal with typical system software designed to be:

CS370 Operating Systems

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Transcription:

Process Synchronization Concurrent access to shared data in the data section of a multi-thread process, in the shared memory of multiple processes, or in a shared file Although every example in this chapter is for main memory data, we can easily use the techniques for the shared data in a shared file (lower level statements = load/op/store = read/op/write). 1. Motivation (Race Condition) P1: A=A+1; P2: A=A-1; Expect A=6 Expect A=4 A=5 Figure 6.1 Concurrent access to shared data Implementation of the statement A=A+1; in machine language: Step 1.1: Register1=A Step 1.2: Register1=Register1+1 Step 1.3: A=Register1 Implementation of the statement A=A-1; in machine language: Step 2.1: Register2=A Step 2.2: Register2=Register2-1 Step 2.3: A=Register2 Race Condition: Order1: Step1.1 2.1 1.2 2.2 1.3 2.3 A=4 Order2: Step2.1 1.1 2.2 1.2 2.3 1.3 A=6 Order3: Step1.1 1.2 1.3 2.1 2.2 2.3 A=5 Inconsistency of shared data A Critical Section: A segment of Code Section that changes, updates, or writes shared data. The execution of concurrent critical sections that access the same data must be mutually exclusive. 1

2. Solutions to the Critical Section Problem repeat Entry Section Critical Section Exit Section Remainder Section until false Figure 6.2 Process model 2.1 Requirements Mutual Exclusion: If there are n cooperative processes or threads that update the same data, only one of them can be in its critical section at a time and no other one will be allowed to enter its critical section before current one completes its critical section. Progress: A process that is currently in its remainder section should not participate in the decision of which waiting process will enter its critical section. This decision cannot be postponed indefinitely. If Mutual Exclusion is not guaranteed, then Progress is meaningless Bounded Waiting: Once a process P has made a request to enter its critical section, the number of other process (or threads) that are allowed to access the shared data before P must be bounded. If Progress is not guaranteed, then Bounded Waiting is meaningless. 2.2 Semaphore Solutions A synchronization tool Can solve complex synchronization problems Definition: A semaphore S is an integer (or binary) variable that is accessed through two atomic operations wait (i.e., P) and signal (i.e., V). S is integer or binary semaphore 2

o wait(s): while S <= 0 do nothing; (in binary, while S==0 do nothing ) S=S-1; (in binary, S=0 ) o signal(s): S=S+1; (in binary, S=1 ) Solving mutual exclusion with semaphore: Repeat Critical Section signal(mutex) Remainder Section Until false Solving synchronization problem using semaphores: We want to execute the code segment 2 of process 2 after the code segment 1 of process 1. Initial value of semaphore sync is 0. Process 1 Process 2 Segment1 wait(sync) Signal(sync) Segment2 Busy waiting o All the previous solutions to critical section require busy waiting. o Busy waiting: Waiting for an event by spinning through a tight loop (e.g., while <cond.> do nothing) that polls for the event on each pass. wastes CPU time 3

Semaphore without Busy Waiting o To overcome busy waiting, we modify the definition of semaphore, wait(), and signal() to block and resume processes to increase CPU utilization. o New Semaphore Definition: Type semaphore = record value:integer; L: List of processes; End; Each semaphore structure has an integer value and a list (queue) of processes. o wait(): S.value=S.value-1; If S.value < 0 Add calling process to S.L; Block the calling process; End If semaphore value is < 0, Pi blocks and is put on the semaphore queue. If semaphore value is >= 0, Pi enters its CS. -S.value is the number of processes in S.L (i.e., semaphore queue) o signal(): S.value=S.value+1 If S.value <= 0 Remove P from S.L; Wakeup process P; end Wakes up the next process in the semaphore queue. Change its state from block (i.e., waiting) to ready. o Semaphore list (queue) S.L can be implemented using any queuing strategy (e.g., FIFO, LIFO, Priority, ) wait() and signal() execute atomically o Uniprocessor: disable interrupt o Multiprocessor: special instruction or software CS solution. In latter case, the CS is wait() or signal() call. Software CS solution cause another spinlock & busy waiting problem. Satisfies mutual exclusion and progress. Bounded waiting is dependent on the queuing strategy of the semaphore list. For example, LIFO queue may not satisfy bounded waiting requirement. 4

Deadlock & Starvation o Deadlock: deadly embraced processes o e.g., Process 0 Process 1 wait(s) wait(q) wait(q) wait(s) signal(s) signal(q) signal(q) signal(s) o Starvation: occurs when a process is constantly denied access to a resource. o e.g., LIFO style S.L 3. Classical Examples In this section, we skip the bounded-buffer problem, since the example can make you confused. 3.1 Readers-Writers Problem Approach 1: no reader should be waiting unless a writer has the permission to write. Approach 2: if a writer is waiting to write, then no new reader can start reading. Solution to either variant will lead to starvation. 3.2 Dining-Philosophers Problem Represent each chopstick by a semaphore. Use wait() to grab a chopstick and signal() to release it. If all philosophers are trying to eat at the same time, deadlock will occur. Some solutions to avoid deadlock: o # of philosophers < # of chairs. o Each philosopher picks up chopsticks only if both are available (pick up both chopsticks in a single critical section) o Odd-numbered philosophers pick up the left chopstick first and then the right chopstick, whereas even-numbered philosophers pick up the right chopstick first and then the left one. 5

4. Use Carefully Incorrect use of semaphores can result in serious errors that are difficult to detect. Example 1: signal(mutex); Critical section or omit wait() before critical section several processes may be executing in their critical section simultaneously Example 2: Critical section or omit signal() after critical section deadlock, no process can execute in its critical section. 6