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

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

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

Process Synchronization

Process Synchronization

Lesson 6: Process Synchronization

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

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

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

Chapter 6: Process Synchronization

CS370 Operating Systems

Synchronization Principles

Chapter 5: Process Synchronization

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

CS420: Operating Systems. Process Synchronization

CS370 Operating Systems

Process Synchronization

Chapter 5: Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

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

Process Synchronization

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 7: Process Synchronization!

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

Chapter 7: Process Synchronization. Background

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

Chapter 5: Process Synchronization

Process Synchronization

Chapter 6: Process Synchronization

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Module 6: Process Synchronization

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

IV. Process Synchronisation

Process Synchronization

CS370 Operating Systems

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

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

CSE Opera,ng System Principles

Real-Time Operating Systems M. 5. Process Synchronization

CSE 4/521 Introduction to Operating Systems

Process Coordination

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

Chapter 6: Process Synchronization

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Process Synchronization

Introduction to Operating Systems

Module 6: Process Synchronization

Lecture 3: Synchronization & Deadlocks

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

Process Synchronization

Chapter 6 Synchronization

Process/Thread Synchronization

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

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

Interprocess Communication By: Kaushik Vaghani

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

Synchronization Spinlocks - Semaphores

High-level Synchronization

Chapter 6 Process Synchronization

Process Synchronization (Part I)

Process/Thread Synchronization

Chapter 6: Process Synchronization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

CS3733: Operating Systems

Process Co-ordination OPERATING SYSTEMS

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 6: Synchronization

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

Dept. of CSE, York Univ. 1

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

CS370 Operating Systems Midterm Review

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Process Synchronization

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

Synchronization Principles I

Lecture 5: Inter-process Communication and Synchronization

Process Synchronization(2)

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Comp 310 Computer Systems and Organization

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

Process Synchronization(2)

Chapter 7 Process Synchronization

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

Achieving Synchronization or How to Build a Semaphore

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

CSC501 Operating Systems Principles. Process Synchronization

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

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

Process Synchronization

Process Synchronization(2)

Mutual Exclusion and Synchronization

Concurrency: Mutual Exclusion and Synchronization

Introduction to OS Synchronization MOS 2.3

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

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Concurrency: Mutual Exclusion and

Process Management And Synchronization

CS370 Operating Systems

Transcription:

Race Condition Synchronization CSCI 315 Operating Systems Design Department of Computer Science A race occurs when the correctness of a program depends on one thread reaching point x in its control flow before another thread reaches point y. Races usually occurs because programmers assume that threads will take some particular trajectory through the execution space, forgetting the golden rule that threaded programs must work correctly for any feasible trajectory. Computer Systems A Programmer s Perspective Randal Bryant and David O Hallaron Notice: The slides for this lecture have been largely based on those from an earlier edition of the course text Operating Systems Concepts, 8th ed., by Silberschatz, Galvin, and Gagne. Many, if not all, the illustrations contained in this presentation come from this source. CSCI 315 Operating Systems Design 2 The Synchronization Problem Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. The Critical-Section Problem Solution 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.) CSCI 315 Operating Systems Design 3 CSCI 315 Operating Systems Design 7 Typical Process P i Peterson s Solution! entry section! exit section while (TRUE); int turn; boolean flag[2];! flag[i] = TRUE;! turn = j;! while (flag[j] && turn == j);!! critical section! flag[i] = FALSE;!! remainder section while (TRUE); CSCI 315 Operating Systems Design 8 CSCI 315 Operating Systems Design 9

Using Locks! acquire lock! release lock while (TRUE); 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: Test memory word and set value. Swap the contents of two memory words. CSCI 315 Operating Systems Design 10 CSCI 315 Operating Systems Design 11 TestAndSet boolean TestAndSet(boolean *target) {! boolean ret_val = *target;!!! *target = TRUE;!! return ret_val; Lock with TestAndSet boolean lock = FALSE;! while (TestAndSet(&lock));! lock = FALSE; while (TRUE); CSCI 315 Operating Systems Design 12 CSCI 315 Operating Systems Design 13 CompareAndSwap int CompareAndSwap (int *value, int expected, int new_value){ int temp = *value; if (*value == expected) *value = new_value; return temp; Lock with CompareAndSwap boolean lock = 0;! while(compareandswap(&lock,0,1)!= 0);! lock = 0; while (TRUE); CSCI 315 Operating Systems Design 14 CSCI 315 Operating Systems Design 15

How are we meeting requirements? Do the solutions above provide: Mutual exclusion? Progress? Bounded waiting? 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 (also known as mutex locks). Note that one can implement a counting semaphore S as a binary semaphore. Provides mutual exclusion: semaphore S(1); // initialized to 1 wait(s); // or acquire(s) or P(S) criticalsection(); signal(s); // or release(s) or V(P) CSCI 315 Operating Systems Design 16 CSCI 315 Operating Systems Design 17 Semaphore Implementation typedef struct {! int value;! struct process *list; semaphore; Semaphore Implementation wait(semaphore *S) {! S->value--;! if (S->value < 0) {!!! add process to S->list!!! block();! signal(semaphore *S) {! S->value++;! if (S->value <= 0) {!!! remove a process P from S->list!!! wakeup(p);! CSCI 315 Operating Systems Design 18 CSCI 315 Operating Systems Design 19 Semaphore Implementation Must guarantee that no two processes can execute signal() and wait() on the same semaphore at the same time. The implementation becomes the critical section problem: Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied Applications may spend lots of time in critical section int n; mutex access; init(&access,1); semaphore empty; init(&empty,n); semaphore full; init(&full,0); access shared buffer capacity n items

// produce item and save wait(&empty); // add item and save signal(&full); while (true); Producer // produce item and save wait(&empty); // add item and save signal(&full); while (true); critical section Consumer wait(&full); // remove item and save signal(&empty); // consume save item while (true); critical section wait(&full); // remove item and save signal(&empty); // consume save item while (true); Monitor Semaphores are low-level synchronization resources. A programmer s honest mistake can compromise the entire system (well, that is almost always true). We should want a solution that reduces risk. The solution can take the shape of high-level language constructs, as the monitor type: monitor mname { // shared variables declaration procedure P1 ( ) { procedure Pn ( ) { init code ( ) {. A procedure can access only local variables defined within the monitor. There cannot be concurrent access to procedures within the monitor (only one process/thread can be active in the monitor at any given time). Condition variables: queues are associated with variables. Primitives for synchronization are wait and signal. Monitor CSCI 315 Operating Systems Design 21 CSCI 315 Operating Systems Design 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!! acquire(s);!!acquire(q);!! acquire(q);!!acquire(s);!!.!!.!!.!!.!!.!!.!! release(s);!!release(q);!! release(q);!!release(s); Starvation indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. CSCI 315 Operating Systems Design 23 CSCI 315 Operating Systems Design 24 thinking hungry eating State diagram for a philosopher CSCI 315 Operating Systems Design 25 CSCI 315 Operating Systems Design 26 CSCI 315 Operating Systems Design 27 CSCI 315 Operating Systems Design 28

CSCI 315 Operating Systems Design 29 CSCI 315 Operating Systems Design 30 Limit to Concurrency What is the maximum number of philosophers that can be eating at any point in time? CSCI 315 Operating Systems Design 31 CSCI 315 Operating Systems Design 32 Philosopher s Behavior Grab chopstick on left Grab chopstick on right Eat Put down chopstick on right Put down chopstick on left How well does this work? CSCI 315 Operating Systems Design 33 CSCI 315 Operating Systems Design 34

Question: How many philosophers can eat at once? How can we generalize this answer for n philosophers and n chopsticks? Question: What happens if the programmer initializes the semaphores incorrectly? (Say, two semaphores start out a zero instead of one.) Question: How can we formulate a solution to the problem so that there is no deadlock or starvation? CSCI 315 Operating Systems Design 35