Process Synchronization

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

Process Synchronization

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

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

CS420: Operating Systems. Process Synchronization

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

Chapter 6: Process Synchronization

Dept. of CSE, York Univ. 1

CS370 Operating Systems

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

Lesson 6: Process Synchronization

Chapter 7: Process Synchronization!

Process Synchronization

Synchronization Principles

Process Synchronization (Part I)

Module 6: Process Synchronization

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Synchronization

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

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

Chapter 7: Process Synchronization. Background. Illustration

CHAPTER 6: PROCESS SYNCHRONIZATION

Process Synchronization

Process Synchronization

Chapter 6 Synchronization

Chapter 7: Process Synchronization. Background

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

Process Synchronization

Chapter 5: Process Synchronization

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

Chapter 6: Process Synchronization

Process Coordination

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

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

IV. Process Synchronisation

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Lecture 3: Synchronization & Deadlocks

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

CS370 Operating Systems

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

Chapter 5: Process Synchronization

Chapter 6: Process Synchronization

CSE Opera,ng System Principles

Synchronization for Concurrent Tasks

Mutual Exclusion and Synchronization

Process Synchronization

Chapter 5: Process Synchronization

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

Process Synchronization - I

Synchronization Principles I

Introduction to Operating Systems

Chapter 6: Synchronization

Lecture 5: Inter-process Communication and Synchronization

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

Roadmap. Shared Variables: count=0, buffer[] Producer: Background. Consumer: while (1) { Race Condition. Race Condition.

Chapter 6 Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

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

CS370 Operating Systems

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

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

Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast. Synchronization

Part II Process Management Chapter 6: Process Synchronization

Last class: Today: CPU Scheduling. Start synchronization

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Chapter 5 Asynchronous Concurrent Execution

Operating Systems CMPSC 473. Synchronization February 21, Lecture 11 Instructor: Trent Jaeger

Concurrency: Mutual Exclusion and

Process Synchronization

1. Motivation (Race Condition)

Concurrency: Mutual Exclusion and Synchronization

Real-Time Operating Systems M. 5. Process Synchronization

CSCI 447 Operating Systems Filip Jagodzinski

UNIT II PROCESS MANAGEMENT 9

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

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

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

Synchronization Spinlocks - Semaphores

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Synchronization I. Jo, Heeseung

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

Chapter 6 Process Synchronization

Comp 310 Computer Systems and Organization

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Process/Thread Synchronization

PESIT Bangalore South Campus

UNIT-II PROCESS SYNCHRONIZATION

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

Process/Thread Synchronization

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

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Process Synchronization

Chapter 7 Process Synchronization

10/17/2011. Cooperating Processes. Synchronization 1. Example: Producer Consumer (3) Example

Transcription:

Process Synchronization Concurrent access to shared data may result in data inconsistency Multiple threads in a single process Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes page 1

Background 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. page 2

Producer/Consumer Producer: while (true) { /* produce an item and put in nextproduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextproduced; in = (in + 1) % BUFFER_SIZE; count++; } Consumer:while (1) { while (count == 0) ; // do nothing nextconsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextconsumed */ } page 3

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: T0: producer execute register1 = count {register1 = 5} T1: producer execute register1 = register1 + 1 {register1 = 6} T2: consumer execute register2 = count {register2 = 5} T3: consumer execute register2 = register2-1 {register2 = 4} T4: producer execute count = register1 {count = 6 } T5: consumer execute count = register2 {count = 4} After concurrent execution, count can be 4, 5 or 6 page 4

Critical section Segment of code where threads are updating common variables is called a critical section Solution is to force only one thread inside the critical section at any one time Define a section before critical section, called entry section and a section at the end called end section. We can implement mechanisms in the entry section that ensures that only one thread is inside the critical section. End section can then tell someone in entry section to continue. page 5

Solution to Critical-Section Problem Solution must satisfy three requirements: 1. Mutual Exclusion - If process Pi 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 only those processes that are not executing in their remainder section can participate in the decision on which will enter its critical section next and this selection 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 page 6

Classic s/w soln: Peterson s Solution Restricted to two processes Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted (not true for modern processors) The two threads 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! page 7

Algorithm for Process P i do { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION } while (TRUE); 1) Mutual exclusion because only way thread enter critical section when flag[j] == FALSE or turn == TRUE 2) Only way to enter section is by flipping flag[] inside loop 3) turn = j allows the other thread to make progress page 8

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 Have to wait for disable to propagate to all processors Operating systems using this not broadly scalable Modern machines provide special atomic hardware instructions Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words page 9

Solution using TestAndSet Definition of TestAndSet: boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: } Shared boolean variable lock., initialized to false. Solution: do { while ( TestAndSet (&lock )) ; /* do nothing // critical section lock = FALSE; // remainder section } while ( TRUE); page 10

Solution using Swap Definition of Swap: void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: } 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 ); // critical section lock = FALSE; // remainder section } while ( TRUE); page 11

Solution with Test And Set and bounded wait boolean waiting[n]; boolean lock; initialized to false Pi can enter critical section iff waiting[i] == false or key == false do { waiting[i] = TRUE; key = TRUE; while (waiting[i] && key) key = TestAndSet (&lock); waiting[i] = FALSE; // critical section j = (i + 1) % n; while ((j!= i) &&!waiting[j]) j = (j + 1) % n; if (j == i) lock = FALSE; else waiting[j] = FALSE; // remainder section } while (TRUE); page 12