Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Similar documents
CENG334 Introduction to Operating Systems

CS 4410 Operating Systems. Review 1. Summer 2016 Cornell University

Operating Systems ECE344

CS 153 Design of Operating Systems Winter 2016

Opera&ng Systems ECE344

CSE 451: Operating Systems Spring Module 10 Semaphores, Condition Variables, and Monitors

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

Critical Section Problem: Example

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

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

7: Interprocess Communication

Chapter 6: Process Synchronization

EECS 482 Introduction to Operating Systems

Threading and Synchronization. Fahd Albinali

Critical Section Problem: Example

Semaphores. Blocking in semaphores. Two types of semaphores. Example: Bounded buffer problem. Binary semaphore usage

Concurrency and Synchronisation

Chapter 8. Basic Synchronization Principles

Concurrency pros and cons. Concurrent Programming Problems. Mutual Exclusion. Concurrency is good for users

Concurrency and Synchronisation

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

Basic Synchronization Principles

Process Synchronization. studykorner.org

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

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

Process Synchronization

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

Concept of a process

Outline for Today. 5 Dining Philosophers. Template for Philosopher. Naive Solution. Objective: Administrative details:

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

2 Threads vs. Processes

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

CS Operating Systems

CS Operating Systems

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Synchronization COMPSCI 386

Concurrency: a crash course

Classic Problems of Synchronization

Synchronization Classic Problems

5 Dining Philosophers. Template for Philosopher. Naive Solution. while (food available) /*pick up forks*/ eat; /*put down forks*/ think awhile;

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

CS3502 OPERATING SYSTEMS

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

Concurrency and Synchronisation. Leonid Ryzhyk

Interprocess Communication By: Kaushik Vaghani

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

Dealing with Issues for Interprocess Communication

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

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Process Management And Synchronization

Process Synchronization

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

Interprocess Communication and Synchronization

Outline for Today. Readers/Writers Problem

Programming in Parallel COMP755

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

Process synchronization

What's wrong with Semaphores?

Operating Systems. User OS. Kernel & Device Drivers. Interface Programs. Interprocess Communication (IPC)

CSC501 Operating Systems Principles. Process Synchronization

Process Synchronization

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Synchronization for Concurrent Tasks

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

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

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

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

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

C09: Process Synchronization

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

Concurrency. Chapter 5

Lecture 6 (cont.): Semaphores and Monitors

Introduction to Operating Systems

Chapter 6: Process Synchronization

PROCESS SYNCHRONIZATION

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Synchronization Exercise

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Synchronization I. Jo, Heeseung

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

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

Process Synchronization

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

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

There are 8 total numbered pages, 6 Questions. You have 60 minutes. Budget your time carefully!

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

CS370 Operating Systems

IV. Process Synchronisation

Process Synchronization(2)

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Synchronization in Concurrent Programming. Amit Gupta

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

EECS 482 Introduction to Operating Systems

Semaphores (by Dijkstra)

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

Lesson 6: Process Synchronization

Transcription:

Synchronization Design, Spring 2011 Department of Computer Science

Synchronization Basic problem: Threads are concurrently accessing shared variables The access should be controlled for predictable result. Solution: Need a mechanism to control the access Low-Level Mechanism Locks Higher Level Mechanism Semaphores Mutexes Condition Variables Monitors 2

Higher-Level Synchronization primitives Locks are useful for mutual exclusion. But programs have different requirements. Examples: Say we had a shared variable where any number of threads could read but only one thread could write. How would you do this with locks? What s wrong with this code? 3

Semaphores Semaphore Higher level construct Shared Counter Operations on Semaphores: P() or wait() or down() Atomically wait for semaphore value to become > 0, then decrement it V() or signal() or up() Atomically increments semaphore by 1 4

Semaphore Example Semaphores can be used to implement Mutual Exclusion Semaphore my_semaphore = 1 // Initialize to nonzero int withdraw(account, amount) { P(my_semaphore) balance = get_balance() balance = balance amount; put_balance(balance,account); V(my_semaphore) CRITICAL SECTION A semaphore where the counter value is only 0 or 1 is called a binary semaphore. A Binary Semaphore similar to lock 5

Simple Semaphore Implementation What is wrong with the above code? 6

Simple Semaphore Implementation 7

Semaphore Implementation How do we ensure that the semaphore operations are atomic? This is similar to Lock: One Approach: Make them System Calls and ensure only one P() or V() operation can be executed by any process at a time This effectively puts a lock around the P() and V() operations Since system calls are executed in privileged mode, interrupts could be disabled to preserve atomicity Second Approach: Use hardware support: Say the CPU had atomic P() and V() operations 8

Why are semaphores any better than Lock? A binary semaphore is basically a lock The real value of Semaphores becomes apparent when the counter can be initialized to a value other than 0 or 1 Say we initialize a semaphore s value to 50 What does this mean about P() and V() operations? 9

The Producer/Consumer Problem Producer pushes items into a buffer Consumer pulls items from the buffer Producer needs to wait when buffer is full Consumer needs to wait when the buffer is empty 10

One Implementation int count=0 -> Shared Variable Producer(){ int item; while(true) { item = produce(); if(count==n) sleep(); insert_item(item); count = count + 1; if(count == 1) Needs to be atomic wakeup(consumer); Consumer(){ int item; while(true) { if(count==0) sleep(); item = remove_item(); count = count-1; if(count == N-1) wakeup(producer) consume(item); What is the problem with this code? Context Switching? Lost wakeup problem! 11

A Fix using Semaphore Semaphore mutex=1; Semaphore empty=n; Semaphore full=0; Producer { int item; while(true) { item=produce(); P(empty); p(mutex); insert_item(item); v(mutex); v(full); Consumer { while(true){ p(full); p(mutex); item = remove_item(); v(mutex); v(empty); consume(item); Does the order matter? 12

Readers/Writers Problem Want any number of threads to read simultaneously But only one thread should be able to write to a object at a time semaphore wrt = 1 int readcount = 0 writer() { P(wrt) do_write() V(wrt) reader(){ readcount ++; if(readcount == 1) p(wrt); do_read(); readcount --; if(readcount ==0 ) V(wrt); Where is the race condition? 13

Readers/Writers Fixed semaphore mutex = 1 semaphore wrt = 1 int readcount = 0 writer() { P(wrt) do_write() V(wrt) This is also called : First Readers Writers Problem reader(){ p(mutex) readcount ++; if(readcount == 1) p(wrt); v(mutex) do_read(); p(mutex) readcount --; if(readcount ==0 ) V(wrt); v(mutex) - Can lead to writer starvation 14

Second Readers/Writers Problem -No writer should starve (The readers could starve). -If a writer is waiting, no new reader can enter the shared memory semaphore mutex = 1 semaphore wrt = 1, read = 1 semaphore mutex2 = 1, mutex3 =1 int readcount = 0 int writecount = 0 writer() { P(mutex2) writecount ++ If(writecount == 1) p(read) V(mutex2) P(wrt) do_write() V(wrt) P(mutex2) writecount If(writecount==0) v(read) Only when all writers finish, the writer releases the read lock reader(){ p(mutex3) p(read) p(mutex) readcount ++; if(readcount == 1) p(wrt); v(mutex) v(read) V(mutex3) do_read(); p(mutex) readcount --; if(readcount ==0 ) V(wrt); v(mutex) 15

Issues with Semaphore Unlike locks, P() and V() do not have to be paired Therefore, it is a lot easier to get into trouble with semaphore User needs to ensure its correctness Wouldn t it be nice if we had a clean, well-defined language support for synchronization.. Java does! 16

Java Synchronization Support : Mutexes Every Java object can be used as a mutex Compiler ensures that the lock is released before exiting the synchronized block Even if there is an exception. 17

Java Condition Variables A conditional variable represents some condition that a thread can: Wait on, until the condition occurs Notify, other waiting threads that the condition has occurred Three operations on Condition Variables wait(): Block on the condition variable notify(): Wake up one thread waiting on a CV notifyall(): Wake up all threads waiting on a CV 18

Revisiting Producer/Consumer int count=0 -> Shared Variable Producer(){ int item; while(true) { item = produce(); lock->acquire() if(count==n) sleep(); insert_item(item); count = count + 1; if(count == 1) wakeup(consumer); lock->release() Consumer(){ int item; while(true) { lock->acquire() if(count==0) sleep(); item = remove_item(); count = count-1; if(count == N-1) wakeup(producer) lock->release() Whats wrong with this? 19 consume(item);

Producer/Consumer Fix-1 int count=0 -> Shared Variable Producer(){ int item; while(true) { item = produce(); lock->acquire() if(count==n){ lock->release() sleep(); insert_item(item); count = count + 1; if(count == 1) lock->release() wakeup(consumer); Whats wrong with this? 20 Consumer(){ int item; while(true) { lock->acquire() if(count==0){ lock->release() sleep(); item = remove_item(); count = count-1; if(count == N-1) lock->release() consume(item); wakeup(producer)