Synchronization Principles II

Similar documents
Chapter 7: Process Synchronization!

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

Chapter 7: Process Synchronization. Background

CHAPTER 6: PROCESS SYNCHRONIZATION

High-level Synchronization

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

Chapter 6: Process Synchronization

Synchronization Principles

Process Synchronization

Lesson 6: Process Synchronization

Chapter 6: Process Synchronization

Process Synchronization

Synchronization Principles I

Introduction to Operating Systems

CS370 Operating Systems

Process Synchronization

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

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

Semaphores (by Dijkstra)

Process Coordination

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

Chapter 5: Process Synchronization

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

Process Synchronization

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization

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

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Synchronization

Module 6: Process Synchronization

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

Chapter 6: Process Synchronization

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

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

Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

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

Lecture 5: Inter-process Communication and Synchronization

Chapter 6: Process Synchronization

Lecture 3: Synchronization & Deadlocks

Module 6: Process Synchronization

Chapter 6 Synchronization

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Process Co-ordination OPERATING SYSTEMS

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

Process Synchronization

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Real-Time Operating Systems M. 5. Process Synchronization

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

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

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

Silberschatz and Galvin Chapter 6

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

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

Interprocess Communication By: Kaushik Vaghani

CS370 Operating Systems

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

CSE 4/521 Introduction to Operating Systems

Process Synchronization(2)

Process Synchronization(2)

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

CSC501 Operating Systems Principles. Process Synchronization

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

CSE Opera,ng System Principles

Chapter 7 Process Synchronization

Dept. of CSE, York Univ. 1

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 6: Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

UNIT II PROCESS MANAGEMENT 9

Chapter 6: Process Synchronization

Process Synchronization

Process Management And Synchronization

CS370 Operating Systems

Process Synchronization(2)

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

CS370 Opera;ng Systems Midterm Review. Yashwant K Malaiya Spring 2018

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

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

CS3502 OPERATING SYSTEMS

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

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

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

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

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

Synchronization for Concurrent Tasks

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

PESIT Bangalore South Campus

CS370 Operating Systems

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

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

Chapter 6 Process Synchronization

Synchronization Basic Problem:

Dealing with Issues for Interprocess Communication

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

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

Transcription:

CSC 256/456: Operating Systems Synchronization Principles II John Criswell University of Rochester 1

Synchronization Issues Race conditions and the need for synchronization Critical Section Problem Mutual Exclusion Progress Bounded waiting Busy waiting vs. Blocking 2

The Good counter++ counter register2 = counter; Counter 4 register2 = register2-1; counter = register2; register1 = counter; register1 = register1 + 1; counter = register1; 3

The Good counter++ counter register2 = counter; Counter 4 register2 = register2-1; counter = register2; register1 = counter; register1 = register1 + 1; counter = register1; Counter 4 3

The Other Good counter++ register1 = counter; counter Counter 4 register1 = register1 + 1; counter = register1; register2 = counter; register2 = register2-1; counter = register2; 4

The Other Good counter++ register1 = counter; counter Counter 4 register1 = register1 + 1; counter = register1; register2 = counter; register2 = register2-1; counter = register2; Counter 4 4

The Ugly counter++ register1 = counter; counter Counter 4 register1 = register1 + 1; register2 = counter; register2 = register2-1; counter = register2; counter = register1; 5

The Ugly counter++ register1 = counter; counter Counter 4 register1 = register1 + 1; register2 = counter; register2 = register2-1; counter = register2; counter = register1; Counter 5 5

Building Synchronization Mechanisms Atomic hardware instructions Mutex (binary semaphore) Semaphore 6

Outline Classical synchronization problems Bounded buffer Multiple reader/single writers Dining philosophers Monitors Condition variables 7

Classical Synchronization Problems 8

Three Classical Problems Bounded Buffer Problem Multiple Readers/Single Writer Problem Dining Philosophers Problem 9

Semaphores on My Mind Think of a semaphore as a count of available resources When the count hits zero, a process must wait wait() (or down()) decreases the count of resources signal() (or up()) increases the count of resources 10

Bounded Buffer Problem Shared Data typedef struct { } item; item buffer[buffer_size]; int in=0, out=0; int counter = 0; Out Counter In Producer process item nextproduced; while (1) { while (counter==buffer_size) ; /* do nothing */ buffer[in] = nextproduced; in = (in+1) % BUFFER_SIZE; counter++; } Consumer process item nextconsumed; while (1) { while (counter==0) ; /* do nothing */ nextconsumed = buffer[out]; out = (out+1) % BUFFER_SIZE; counter--; } 11

Bounded Buffer Solution Shared Data int n; semaphore mutex = 1; semaphore empty = n; semaphore full = 0; Out Counter In Semaphore Initialized To Purpose mutex 1 Controls access to the buffer empty n Number of empty slots full 0 Number of full slots 12

Bounded Buffer Solution Producer process Consumer process while (1) { produce an item; wait (empty); wait (mutex); add nextp to buffer; signal (mutex); signal (full); } while (1) { wait (full); wait (mutex); remove an item from buffer; signal (mutex); signal (empty); consume item; } 13

Can you have a race condition with only reads? 14

Multiple Readers/Single Writer Shared resource Network routing tables Database Want to permit concurrent readers Writers need exclusive access 15

Multiple Reader/Single Writer Shared Data int read_count = 0; semaphore mutex = 1; semaphore rw_mutex = 1; Semaphore Initialized To Purpose mutex 1 Controls access to read_count rw_mutex 1 Controls write access 16

Multiple Reader/Single Writer Solution while (1) { Reader Process wait (mutex); read_count++; if (read_count == 1) wait (rw_mutex); signal (mutex); read(); wait (mutex); while (1) { wait (rw_mutex); write(); signal (rw_mutex); } Writer Process read_count ; if (read_count == 0) signal (rw_mutex); signal (mutex); } 17

Dining-Philosophers Problem Philosopher i (1 i 5): while (1) { eat; think; } Eating requires both chopsticks (both left and right) Need to avoid starvation 18

Dining Philosophers Solution Philosopher i while(1) {... wait(chopstick[i]); wait(chopstick[(i+1) % 5]); Shared Data semaphore chopstick[5]; Initially all values are 1; eat; signal(chopstick[i]); signal(chopstick[(i+1) % 5]);... think;... }; 19

Dining Philosophers Solution Philosopher i while(1) {... wait(chopstick[i]); wait(chopstick[(i+1) % 5]); Shared Data semaphore chopstick[5]; Initially all values are 1; eat; signal(chopstick[i]); signal(chopstick[(i+1) % 5]);... think; Any potential problems?... }; 19

Deadlock Each philosopher picks up his/her left chopstick 20

Potential Solutions for Deadlock Problem Limit eating to 4 philosophers Require chopsticks to be picked up in pairs Requires a critical section Make the pickup algorithm asymmetric Even philosophers pick up left stick then right stick Odd philosophers pick up right stick then left stick 21

Summary Mechanisms for solving synchronization Atomic hardware instructions Mutex Semaphore Using these mechanisms is fraught with peril 22

Monitors 23

Monitors High-level programming synchronization construct Private variables Procedures monitor name { shared variable declarations procedure body P1 (...) {... } procedure body Pn (...) { Only 1 runs at a time Can only access Monitor variables Formal parameters 24... } { initialization code } }

Condition Variables Makes monitors more flexible Operations monitor name { condition x; condition y; wait(): Suspends process signal(): Wakes up process procedure body P1 (...) { x.wait ();... } procedure body Pn (...) {... x.signal(); } } 25

Condition Variable Details var.wait() Puts process to sleep Waits until someone signals on the same variable var.signal() Wakes up a process waiting on variable var Does nothing if no process is waiting on var 26

Condition Variable Semantics On signal, can t have two processes in monitor Solutions: Waiting process runs, signaling process blocks Signal must be followed by monitor exit Waiting process continues to wait until signaling process exits monitors 27

Dining Philosophers Solution 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((i+4)%5); test((i+1)%5); } void test (int i) { if (state[(i+4)%5]=eating && state[(i+1)%5]=eating && state[i] == HUNGRY) { state[i] = EATING; self[i].signal(); } } void init() { for (int i=0; i<5; i++) state[i] = THINKING; } } 28

Other Synchronization Methods 29

Functional Languages Pure functional languages have no shared memory Once a value is created, it is never modified No race conditions Compiler uses dependencies to parallelize code 30

Actor Languages Use message passing instead of shared memory Actors send messages to each other Concurrency issues still exist Messages can be asynchronous Messages can be reordered Implementation may use shared memory synchronization 31

Wait-free Data Structures Removes the need to wait Operation performs in finite number of steps regardless of the speed of other processes Consensus number Ranks ability to implement wait-free systems with hardware atomic operations data structure consensus <= atomic op consensus 32

Consensus Numbers Atomic Op Consensus Number swap 2 fetch and add 2 test and set 2 compare and swap infinite 33

A Note About Systems vs. Theory Synchronization is a combination of Systems and Theory Theory Explain requirements Prove that approaches work properly Illuminate limitations of approaches (e.g., consensus numbers) Systems Hardware support Understand which requirements are important in practice 34

Theory and Systems work together 35

Credits and Disclaimer Parts of the lecture slides contain original work from Gary Nutt, Andrew S. Tanenbaum, and Kai Shen. The slides are intended for the sole purpose of instruction of operating systems at the University of Rochester. All copyrighted materials belong to their original owner(s). Consensus information from Wait-Free Synchronization by Maurice Herlihy 36