Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

Similar documents
Synchronization. Before We Begin. Synchronization. Example of a Race Condition. CSE 120: Principles of Operating Systems. Lecture 4.

CSE 120: Principles of Operating Systems. Lecture 4. Synchronization. October 7, Prof. Joe Pasquale

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

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

Process/Thread Synchronization

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

Process/Thread Synchronization

Process Synchronization

Section I Section Real Time Systems. Processes. 1.4 Inter-Processes Communication (part 1)

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

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

Introduction to OS Synchronization MOS 2.3

Concept of a process

Process Synchronization(2)

Part II Process Management Chapter 6: Process Synchronization

Dept. of CSE, York Univ. 1

OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL. Chap. 2.2 Interprocess Communication

UNIX Input/Output Buffering

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

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Chapter 6: Process Synchronization

Locks. Dongkun Shin, SKKU

Resource management. Real-Time Systems. Resource management. Resource management

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

Process Synchronization

SWEN-220 Mathematical Models of Software. Process Synchronization Critical Section & Semaphores

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018

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

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

Concurrency: a crash course

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

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

IT 540 Operating Systems ECE519 Advanced Operating Systems

Concurrency. Chapter 5

Synchronization Spinlocks - Semaphores

Introduction to Operating Systems

Concurrency: Locks. Announcements

CS420: Operating Systems. Process Synchronization

Process Synchronization

C09: Process Synchronization

CS 153 Design of Operating Systems Winter 2016

Process Management And Synchronization

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Interprocess Communication and Synchronization

CS370 Operating Systems

Synchronization for Concurrent Tasks

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

Chapter 6: Process Synchronization

Chapter 8. Basic Synchronization Principles

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

Threads and Synchronization. Kevin Webb Swarthmore College February 15, 2018

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

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

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

CSCI 447 Operating Systems Filip Jagodzinski

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

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

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

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

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

EECS 482 Introduction to Operating Systems

Last Class: Synchronization

Achieving Synchronization or How to Build a Semaphore

CSE 153 Design of Operating Systems

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

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

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

Synchronization Principles

Process Synchronization(2)

EE458 - Embedded Systems Lecture 8 Semaphores

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

CS4411 Intro. to Operating Systems Exam 1 Fall points 9 pages

Week 3. Locks & Semaphores

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

Process Synchronization (Part I)

Lecture 6. Process Synchronization

Lecture 4: Inter-Process Communication (Jan 27, 2005)

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Prof. Dr. Hasan Hüseyin

PROCESS SYNCHRONIZATION

Concurrency: Mutual Exclusion and Synchronization

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Process Coordination

IV. Process Synchronisation

Part III Synchronization Software and Hardware Solutions

Lecture 8: September 30

Resource Sharing & Management

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Chapter 5 Asynchronous Concurrent Execution

Chapters 5 and 6 Concurrency

Process Synchronization Mechanisms

Lesson 6: Process Synchronization

Programming Languages

Synchronization Principles I

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

Dekker s algorithms for Semaphores Implementation. ALI TARIQ AL-KHAYYAT ( ) Submitted to : Prof. Dr. Huseyin Balik

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Transcription:

CSE 120: Principles of Operating Systems Lecture 4 Synchronization January 23, 2006 Prof. Joe Pasquale Department of Computer Science and Engineering University of California, San Diego Before We Begin Read Chapter 6 (Process Synchronization) Programming Assignment #2 Due Friday January 27 Homework Assignment #2 Due Sunday January 29 2006 by Joseph Pasquale 1 2 Synchronization Credit/Debit Problem: Race Condition Synchronize: to (arrange events to) happen at same time Say runs first read $1000 into b Process synchronization When one process has to wait for another Events in processes that occur at the same time Uses of synchronization Prevent race conditions Wait for resources to become available Process Credit (int a) { int b b = ReadBalance () b = b + a WriteBalance (b) PrintReceipt (b) } Switch to read $1000 into b Debit by $900 Write $900 critical sections Switch to Credit by $100 Write $1100 Process Debit (int a) { int b b = ReadBalance () b = b - a WriteBalance (b) PrintReceipt (b) } 3 Bank just lost $100! 4

To Avoid Race Conditions Identify critical sections Sections of code executed by multiple processes Must run atomically with respect to each other Process 0 --critical- --section- Process 1 --critical- --section- Four Rules for Mutual Exclusion 1. No two processes inside critical sections at same time 2. No process outside critical section may block others 3. No process may wait forever to enter critical section 4. No assumptions about speeds or number of CPU s Enforce mutual exclusion Only one process active in a critical section 5 6 How to Achieve Mutual Exclusion? Surround critical section with entry/exit code < entry code > < exit code > Entry code should act as a barrier < entry code > < exit code > If another process is in critical section, block Otherwise, allow process to proceed Possible Solution: Software Lock? Lock indicates whether any process is in critical section shared int lock = OPEN while ( lock == CLOSED ) while ( lock == CLOSED ) lock = CLOSED lock = CLOSED lock = OPEN lock = OPEN Race condition in while loop (breaks Rule 1) Exit code should act to release other entry barriers 7 8

Possible Solution: Take Turns? Alternate which process can enter critical section shared int turn = Possible Solution: State Intention? Each process states it wants to enter critical section shared boolean flag[2] = {FALSE, FALSE} while ( turn!= ) while ( turn!= ) flag[ ] = TRUE flag[ ] = TRUE while ( flag[ ] ) while ( flag[ ] ) turn = turn = flag[ ] = FALSE flag[ ] = FALSE Prevents entry even if OK to enter (breaks Rule 2) Race condition: prevents entry forever (breaks Rule 3) 9 10 flag[ ] = TRUE turn = while ( flag[ ] && turn == ) flag[ ] = FALSE Peterson s Solution If there is competition, take turns otherwise, enter shared int turn shared boolean flag[2] = {FALSE, FALSE} flag[ ] = TRUE turn = while ( flag[ ] && turn == ) flag[ ] = FALSE Works! Extends to n processes. Problem: busy-waiting 11 What About Disabling Interrupts? By disabling interrupts, context-switching can t occur DisableInterrupts () EnableInterrupts () Too restrictive (breaks Rule 2) locks out all processes even those not in critical section DisableInterrupts () EnableInterrupts () Doesn t work on a multiprocessor (breaks Rule 4) 12

Hardware Solution: TSET Instruction Hardware instruction: TSET mem simultaneously test mem and set it to 1 { reg! mem, mem! 1, set cond code } atomically C function: tset (lock) sets lock to 1, returns orig value shared int lock = 0 while ( tset (lock) == 1 ) lock = 0 while ( tset (lock) == 1 ) lock = 0 Simple, works for n processes (but still busy-waits!) 13 Semaphores Semaphore: synchronization variable Has integer value List of waiting processes Works like a gate If sem > 0, gate is open value indicates number of processes that can enter Else, gate is closed possibly with waiting processes sem = 3 sem = 2 critical section sem = 1 sem = 0 14 Semaphore Operations sem s = n /* declare and initialize */ wait (sem s) Decrement s If s < 0, block process (and associate with s) signal (sem s) Increment s If blocked processes, unblock (any) one of them No other operations permitted (e.g., can t test value) An Incorrect Definition sem s = n /* declare and initialize */ wait (sem s) If s == 0, block process (and associate with s) Decrement s (note: occurs after process unblocks) signal (sem s) Increment s If blocked processes, unblock (any) one of them WHERE IS THE RACE CONDITION? 15 16

Mutual Exclusion with Semaphores Use a mutex semaphore initialized to 1 Synchronization with Semaphores Use a synch semaphore initialized to 0 sem mutex = 1 sem synch = 0 wait (mutex) signal (mutex) wait (mutex) signal (mutex) < to be done before > wait (synch) signal (synch) < to be done after > Allows a process to wait for another before proceeding Allows only one process to enter critical section Simple, works for n processes, no busy-waiting (really?) Semaphores provide pure synchronization No way for a process to tell it blocked I.e., no information transfer 17 18 Atomicity of Semaphore Operations wait (s) and signal (s) are atomic operations Their bodies are critical sections Mutual exclusion achieved using a lower-level mechanism Test-and-set locks Disabling interrupts (on a uniprocessor) Therefore, problems such as busy-waiting still exist But at a lower-level For brief (and known) periods of time 19