Synchronization Classic Problems

Similar documents
Classic Problems of Synchronization

Process Synchronization

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

Process Coordination

Synchronization Basic Problem:

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

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Process Synchronization(2)

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

Process Synchronization(2)

Synchronization. Peter J. Denning CS471/CS571. Copyright 2001, by Peter Denning

Chapter 7: Process Synchronization. Background. Illustration

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

Process Synchronization(2)

CSE 4/521 Introduction to Operating Systems

Process Synchronization

Chapter 7: Process Synchronization. Background

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

CS370 Operating Systems

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

CS370 Operating Systems

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

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

Module 6: Process Synchronization

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

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Semaphores (by Dijkstra)

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

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CSE Opera,ng System Principles

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

Multitasking / Multithreading system Supports multiple tasks

Lesson 6: Process Synchronization

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Process Synchronization

Lecture 6 (cont.): Semaphores and Monitors

Concurrency and Synchronisation

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

Concurrency and Synchronisation

CS3733: Operating Systems

Process Synchronization

CS 153 Design of Operating Systems Winter 2016

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

Chapter 6: Process Synchronization

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

OS Process Synchronization!

9/29/2014. CS341: Operating System Mid Semester Model Solution Uploaded Semaphore ADT: wait(), signal()

Concurrency. Chapter 5

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

Chapter 7 Process Synchronization

A Brief Review for the Final. Final Examination

CS370 Operating Systems Midterm Review

Operating Systems. Lecture 8

Introduction to Operating Systems

Process Synchronization

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

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

High-level Synchronization

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

Chapter 6: Process Synchronization

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

Chapter 6: Process Synchronization

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

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization

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

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

Process Synchronization. studykorner.org

CS 318 Principles of Operating Systems

Process Synchronization

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Process Synchronization Examples

CS 318 Principles of Operating Systems

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

Synchronization COMPSCI 386

Department of CSIT ( G G University, Bilaspur ) Model Answer 2013 (Even Semester) - AR-7307

CSE 120 Principles of Operating Systems Spring 2016

Process Synchronization Mechanisms

Chapter 5: Process Synchronization

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

Operating systems. Lecture 12

Process Co-ordination OPERATING SYSTEMS

Interprocess Communication By: Kaushik Vaghani

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

Synchronization Spinlocks - Semaphores

Lecture 5: Inter-process Communication and Synchronization

CS3502 OPERATING SYSTEMS

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CS 3723 Operating Systems: Final Review

Synchronization. Dr. Yingwu Zhu

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

Real-Time Operating Systems M. 5. Process Synchronization

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1 1

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

AC59/AT59 OPERATING SYSTEMS & SYSTEMS SOFTWARE DECEMBER 2012

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

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

CHAPTER 6: PROCESS SYNCHRONIZATION

Using Semaphores CS 241. March 14, University of Illinois

Transcription:

CS 4410 Operating Systems Synchronization Classic Problems Summer 2013 Cornell University 1

Today What practical problems can we solve with semaphores? Bounded-Buffer Problem Producer-Consumer Problem 2

Producer-Consumer Problem Arises when two or more threads communicate with each other. And, some threads produce data and other threads consume this data. Real example: Production line 3

Producer-Consumer Problem Start by imagining an unbounded (infinite) buffer Producer process writes data to buffer Writes to In and moves rightwards Consumer process reads data from buffer Reads from Out and moves rightwards Should not try to consume if there is no data Out In 4

Producer-Consumer Problem Bounded buffer: size N Access entry 0 N-1, then wrap around to 0 again Producer process writes data to buffer Must not write more than N items more than consumer ate Consumer process reads data from buffer Should not try to consume if there is no data 0 1 N-1 In Out 5

Producer-Consumer Problem Multiple producer-threads. Multiple consumer-threads. One bounded buffer with N entries. All threads modify the same buffer. Requirements: No production when all N entries are full. No consumption when no entry is full. Only one thread should modify the buffer at any time. 6

Producer-Consumer Problem Solving with semaphores: We ll use counters to track how much data is in the buffer One counter counts as we add data and stops a producer if there are N objects in the buffer. A second counter counts as we remove data and stops a consumer if there are 0 in the buffer. Idea: since general semaphores can count for us, we don t need a separate counter variable. We'll use a mutex to protect the update of the buffer ( In and Out pointers). 7

Producer-Consumer Problem Shared pointers: In, Out Shared Semaphores: mutex, empty, full; mutex = 1; /* for mutual exclusion*/ empty = N; /* number empty buf entries */ full = 0; /* number full buf entries */ do { Producer do { Consumer //produce item //update In //consume item //update Out } while (true); } while (true); 8

Producer-Consumer Problem Shared pointers: In, Out Shared Semaphores: mutex, empty, full; mutex = 1; /* for mutual exclusion*/ empty = N; /* number empty buf entries */ full = 0; /* number full buf entries */ do { Producer wait(empty); do { Consumer wait(full); //produce item //update In signal(full); } while (true); //consume item //update Out signal(empty); } while (true); 9

Producer-Consumer Problem Shared pointers: In, Out Shared Semaphores: mutex, empty, full; mutex = 1; /* for mutual exclusion*/ empty = N; /* number empty buf entries */ full = 0; /* number full buf entries */ Producer do { wait(empty); wait(mutex); //produce item //update In signal(mutex); signal(full); } while (true); do { Consumer wait(full); wait(mutex); //consume item //update Out signal(mutex); signal(empty); } while (true); 10

Readers and Writers In this problem, threads share data that some threads read and other threads write. Goal: allow multiple concurrent readers but only a single writer at a time, and if a writer is active, readers wait for it to finish. 11

Readers-Writers Problem Access to a database A reader is a thread that needs to look at the database but won t change it. A writer is a thread that modifies the database. Making an airline reservation When you browse to look at flight schedules the web site is acting as a reader on your behalf. When you reserve a seat, the web site has to write into the database to make the reservation. 12

Readers-Writers Problem Many reader-threads. Many writer-threads. One piece of data. Multiple threads try to access that data. Requirements: Multiple readers may access the data at the same time. If a writer accesses the data, no other thread may access the data. What happens when multiple readers and one writer are waiting to access the data? 13

Readers-Writers Problem mutex = Semaphore(1) wrt = Semaphore(1) readcount = 0; Reader do{ Writer do{ /*reading is performed*/ /*writing is performed*/ }while(true) }while(true) 14

Readers-Writers Problem mutex = Semaphore(1) wrt = Semaphore(1) readcount = 0; Reader do{ Writer do{ wait(wrt); /*writing is performed*/ signal(wrt); }while(true) wait(wrt); /*reading is performed*/ signal(wrt); }while(true) 15

Readers-Writers Problem mutex = Semaphore(1) wrt = Semaphore(1) readcount = 0; Writer do{ wait(wrt); /*writing is performed*/ signal(wrt); }while(true) Reader do{ wait(mutex); readcount++; if (reardcount == 1) wait(wrt); signal(mutex); /*reading is performed*/ wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex); }while(true) 16

Readers-Writers Notes If there is a writer First reader blocks on wrl Other readers block on mutex Once a reader is active, all readers get to go through Which reader gets in first? The last reader to exit signals a writer If no writer, then readers can continue If readers and writers are waiting on wrl, and writer exits Who gets to go in first? Why doesn t a writer need to use mutex? Is the previous solution fair? Readers can starve writers! Building a fair solution is tricky! 17

Today Which practical problems can we solve with semaphores? Producers-Consumers Problem Readers-Writers Problem