Process Synchronization

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

Chapter 6: Process Synchronization

Synchronization Principles

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

Module 6: Process Synchronization

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

Lesson 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Synchronization

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Process Synchronization

Chapter 6: Process Synchronization

Lecture 3: Synchronization & Deadlocks

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization!

Chapter 5: Process Synchronization

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

Process Synchronization

CSE Opera,ng System Principles

Chapter 7: Process Synchronization. Background. Illustration

Introduction to Operating Systems

Process Synchronization

Chapter 7: Process Synchronization. Background

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

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

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

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

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

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

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

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

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

Chapter 5: Process Synchronization

Chapter 6 Synchronization

CS370 Operating Systems

CS370 Operating Systems

Chapter 6: Process Synchronization

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

Process Synchronization

Chapter 5: Process Synchronization

CS370 Operating Systems

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

Chapter 6: Process 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

Process Synchronization

CS420: Operating Systems. Process Synchronization

Real-Time Operating Systems M. 5. Process Synchronization

IV. Process Synchronisation

Chapter 6 Process Synchronization

Chapter 6: Synchronization

Lecture 5: Inter-process Communication and Synchronization

Module 6: Process Synchronization

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Interprocess Communication By: Kaushik Vaghani

PESIT Bangalore South Campus

Process Co-ordination OPERATING SYSTEMS

Process Coordination

Process Synchronization. studykorner.org

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

UNIT II PROCESS MANAGEMENT 9

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

Process Synchronization(2)

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

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

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

Process Synchronization(2)

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

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

CSE 4/521 Introduction to Operating Systems

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Process Synchronization

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

Process Synchronization(2)

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

Process Management And Synchronization

Synchronization Principles II

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Silberschatz and Galvin Chapter 6

CS3502 OPERATING SYSTEMS

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

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

Chapter 7 Process Synchronization

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

$ %! 0,-./ + %/ 0"/ C (" &() + A &B' 7! .+ N!! O8K + 8 N. (Monitors) 3+!

OS Process Synchronization!

Dept. of CSE, York Univ. 1

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

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

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

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

CSC501 Operating Systems Principles. Process Synchronization

1. Motivation (Race Condition)

9/30/2014. CS341: Operating System High Level Construct: Monitor Deadlock Conditions Prevention, Avoidance Detection and Recovery

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Process Synchronization

Transcription:

Process Synchronization Chapter 6 2015 Prof. Amr El-Kadi

Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers. We can do so by having an integer count that keeps track of the number of full buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer. 2015 Prof. Amr El-Kadi 2

Producer 2015 Prof. Amr El-Kadi 3

Consumer 2015 Prof. Amr El-Kadi 4

Race Condition count++ could be implemented as register1 = count register1 = register1 + 1 count = register1 count-- could be implemented as register2 = count register2 = register2-1 count = register2 Consider this execution interleaving with count = 5 initially: S0: producer execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2-1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4} 2015 Prof. Amr El-Kadi 5

Solution to Critical-Section Problem 1. Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes 2015 Prof. Amr El-Kadi 6

Critical-Section Problem 1. Race Condition - When there is concurrent access to shared data and the final outcome depends upon order of execution. 2. Critical Section - Section of code where shared data is accessed. 3. Entry Section - Code that requests permission to enter its critical section. 4. Exit Section - Code that is run after exiting the critical section 2015 Prof. Amr El-Kadi 7

Structure of a Typical Process 2015 Prof. Amr El-Kadi 8

Peterson s Solution Two process solution Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted. The two processes share two variables: int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical section. The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process P i is ready! 2015 Prof. Amr El-Kadi 9

Algorithm for Process P i 2015 Prof. Amr El-Kadi 10

Critical Section Using Locks 2015 Prof. Amr El-Kadi 11

Synchronization Hardware Many systems provide hardware support for critical section code Uniprocessors could disable interrupts Currently running code would execute without preemption Generally too inefficient on multiprocessor systems Operating systems using this not broadly scalable Modern machines provide special atomic hardware instructions Atomic = non-interruptible Either test memory word and set value Or swap contents of two memory words 2015 Prof. Amr El-Kadi 12

Solution to Critical-section Problem Using Locks do { acquire lock critical section release lock remainder section } while (TRUE); 2015 Prof. Amr El-Kadi 13

TestAndndSet Instruction Definition: boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: } 2015 Prof. Amr El-Kadi 14

Solution using TestAndSet Shared boolean variable lock., initialized to false. Solution: do { while ( TestAndSet (&lock )) ; // do nothing // critical section lock = FALSE; // remainder section } while (TRUE); 2015 Prof. Amr El-Kadi 15

Swap Instruction Definition: void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: } 2015 Prof. Amr El-Kadi 16

Solution using Swap Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key Solution: do { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); lock = FALSE; } while (TRUE); // critical section // remainder section 2015 Prof. Amr El-Kadi 17

Semaphore Synchronization tool that does not require busy waiting Semaphore S integer variable Two standard indivisible (atomic) operations modify S: wait() and signal() Originally called P() and V() Less complicated 2015 Prof. Amr El-Kadi 18

Semaphore as General Synchronization Tool 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 Also known as mutex locks Semaphore mutex; // initialized to 1 do { wait (mutex); // Critical Section signal (mutex); // remainder section } while (TRUE); 2015 Prof. Amr El-Kadi 19

Semaphore Implementation Must guarantee that no two processes can execute wait () and signal() on the same semaphore at the same time implementation becomes the critical section problem where the wait () and signal () code are placed in the critical section. Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied Note that applications may spend lots of time in critical sections and therefore this is not a good solution. 2015 Prof. Amr El-Kadi 20

Semaphore Implementation with no Busy waiting With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items: value (of type integer) pointer to next record in the list Two operations: block place the process invoking the operation on the appropriate waiting queue. wakeup remove one of processes in the waiting queue and place it in the ready queue. 2015 Prof. Amr El-Kadi 21

Semaphore Implementation with no Busy waiting (Cont.) Implementation of wait(): wait(semaphore *S) { S->value--; if (S->value < 0) { add this process to S->list; block(); } } Implementation of signal(): signal(semaphore *S) { S->value++; if (S->value <= 0) { remove a process P from S->list; wakeup(p); } } 2015 Prof. Amr El-Kadi 22

Deadlock and Starvation Deadlock two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(s); wait(q); wait(q); wait (S);...... signal(s); signal(q); signal(q); signal (S); Starvation indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. 2015 Prof. Amr El-Kadi 23

Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem 2015 Prof. Amr El-Kadi 24

Bounded-Buffer Problem N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N. 2015 Prof. Amr El-Kadi 25

Bounded-Buffer Problem The structure of the producer process do { // produce an item in nextp wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); } while (TRUE); 2015 Prof. Amr El-Kadi 26

Bounded-Buffer Problem The structure of the consumer process do { wait (full); wait (mutex); // remove an item from buffer to nextc signal (mutex); signal (empty); // consume the item in nextc } while (TRUE); 2015 Prof. Amr El-Kadi 27

Readers-Writers Problem A data set is shared among a number of concurrent processes Readers only read the data set; they do not perform any updates Writers can both read and write. Problem allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time. Shared Data Data set Semaphore mutex initialized to 1 Semaphore wrt initialized to 1 Integer readcount initialized to 0 2015 Prof. Amr El-Kadi 28

Readers-Writers Problem The structure of a writer process do { wait (wrt) ; signal (wrt) ; } while (TRUE); // writing is performed 2015 Prof. Amr El-Kadi 29

Readers-Writers Problem The structure of a reader process do { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex) // reading is performed wait (mutex) ; readcount - - ; if (readcount == 0) signal (wrt) ; signal (mutex) ; } while (TRUE); 2015 Prof. Amr El-Kadi 30

Dining-Philosophers Problem Shared data Bowl of rice (data set) Semaphore chopstick [5] initialized to 1 2015 Prof. Amr El-Kadi 31

Dining-Philosophers Problem (Cont.) The structure of Philosopher i: do { wait ( chopstick[i] ); wait ( chopstick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think } while (TRUE); 2015 Prof. Amr El-Kadi 32

Problems with Semaphores Correct use of semaphore operations: mutex.acquire(). mutex.release() mutex. acquire() mutex. acquire() Omitting of mutex. acquire() or mutex.release() (or both) 2015 Prof. Amr El-Kadi 33

Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within the monitor at a time 2015 Prof. Amr El-Kadi 34

Syntax of a Monitor 2015 Prof. Amr El-Kadi 35

Schematic view of a Monitor 2015 Prof. Amr El-Kadi 36

Condition Variables Condition x, y; Two operations on a condition variable: x.wait () a process that invokes the operation is suspended. x.signal () resumes one of processes (if any) that invoked x.wait () 2015 Prof. Amr El-Kadi 37

Monitor with Condition Variables 2015 Prof. Amr El-Kadi 38

Solution to Dining Philosophers monitor DP { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i]!= EATING) self [i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); } 2015 Prof. Amr El-Kadi 39

Solution to Dining Philosophers (cont) void test (int i) { if ( (state[(i + 4) % 5]!= EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5]!= EATING) ) { state[i] = EATING ; self[i].signal () ; } } } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } 2015 Prof. Amr El-Kadi 40

Solution to Dining Philosophers (cont) Each philosopher I invokes the operations pickup() and putdown() in the following sequence: DiningPhilosophters.pickup (i); EAT DiningPhilosophers.putdown (i); 2015 Prof. Amr El-Kadi 41