Lecture 13: Producer-Consumer and Semaphores

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Interprocess Communication
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Classic Synchronization Problems
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
Interprocess Communication
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Review: Producer-Consumer using Semaphores #define N 100// number of slots in the buffer Semaphore mutex = 1;// controls access to critical region Semaphore.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
Semaphores The producer-consumer problem using semaphores.
Jonathan Walpole Computer Science Portland State University
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
CY2003 Computer Systems Lecture 06 Interprocess Communication Monitors.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
Semaphores Reference –text: Tanenbaum ch
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Semaphores Reference text: Tanenbaum ch
Process Synchronization: Semaphores
Background on the need for Synchronization
Interprocess Communication (3)
Chapter 5: Process Synchronization
Lecture 12: Peterson’s Solution and Hardware Support
The Producer-Consumer Problem OR, The Bounded-Buffer Problem
Lecture 14: Pthreads Mutex and Condition Variables
Topic 6 (Textbook - Chapter 5) Process Synchronization
Thread Implementations; MUTEX
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Chapter 6: Process Synchronization
Implementing Mutual Exclusion
Lecture 14: Pthreads Mutex and Condition Variables
Lecture 12: Peterson’s Solution and Hardware Support
Lecture 13: Producer-Consumer and Semaphores
Thread Implementations; MUTEX
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Semaphores Reference text: Tanenbaum ch
Interprocess Communication & Synchronization
Synchronization, Part 2 Semaphores
Process Synchronization
Presentation transcript:

Lecture 13: Producer-Consumer and Semaphores

Review: Mutual Exclusion Solutions Software solution Disabling interrupts Strict alternation Peterson’s solution Hardware solution TSL/XCHG Key issue Busy waiting

Review: Mitigate Busy Waiting /* set lock to non-zero, proceed if it was 0 earlier */ enter_region: TSL Reg, Lock /* set Lock to 1, copy old value of Lock into Reg*/ If (Reg != 0) then /* not the first to set to zero */ { thread_yield() ; /* let somebody else run */ Jump enter_region /* try again */ } return leave_region: Lock = 0

In this lecture sleep() and wakeup() Producer-consumer problem Semaphores

Avoiding Busy Waiting If a process cannot enter critical region, the process calls sleep() to give up CPU. A process wakes up another process using wakeup()

Review: Mutual Exclusion using TSL /* set lock to non-zero, proceed if it was 0 earlier */ enter_region: TSL Reg, Lock /* set Lock to 1, copy old value of Lock into Reg*/ If (Reg != 0) then /* not the first to set to zero */ Jump enter_region /* try again */ return leave_region: Lock = 0

TSL without busy waiting /* set lock to non-zero, proceed if it was 0 earlier */ enter_region: TSL Reg, Lock /* set Lock to 1, copy old value of Lock into Reg*/ If (Reg != 0) then /* not the first to set to zero */ { sleep() Jump enter_region /* try again */ return leave_region: Lock = 0 wakeup(process_id)

Producer – Consumer Problem Mutual exclusion Buffer Full Buffer Empty

How is this solution? No mutual exclusion #define N 100 int count = 0 PRODUCER While (TRUE) { if (count < N){ item = produce_item(); insert_item(item); count = count + 1; } CONSUMER While (TRUE) { if (count > 0){ item = remove_item(); count = count - 1; consume_item(item); }

Using sleep() and wakeup() #define N 100 int count = 0 PRODUCER While (TRUE) { item = produce_item(); if (count == N) sleep(); insert_item(item); count = count + 1; if (count == 1) wakeup(CONSUMER); } CONSUMER While (TRUE) { if (count == 0) sleep(); item = remove_item(); count = count - 1; if (count == N-1) wakeup(PRODUCER); consume_item(item); }

Producer – Consumer Problem Need a way to block till some condition is satisfied Semaphores Condition variables

Semaphore: Interface S: Integer variable down(&S): when (S > 0) S = S – 1; up(&S): S = S + 1; Atomic actions down(&S) block when S =0 up(S) never blocks

Semaphore: Implementation down(&S): If (S=0) then Suspend thread, put into a waiting queue Schedule another thread to run Else decrement S and return up(&S): Increment S If any threads in waiting queue, then release one of them (make it ‘ready’) Both the above are done atomically by disabling interrupts by TSL/XCHG

Producer Consumer using Semaphores #define N 100 int mutex =1; int empty = N; int full = 0 PRODUCER While (TRUE) { item = produce_item(); down(&empty); down(&mutex); insert_item(item); up(&mutex); up(&full); } CONSUMER While (TRUE) { down(&full); down(&mutex); item = remove_item(); up(&mutex); up(&empty); consume_item(item); }

Mutual Exclusion Solutions Software solution Disabling interrupts Strict alternation Peterson’s solution Hardware solution TSL/XCHG

Mutual Exclusion via Semaphore Initialize Semaphore mutex = 1 lock(&mutex)= down(&mutex) unlock(&mutex)= up(&mutex)

Mutual Exclusion via Semaphore lock(&mutex); critical_region(); unlock(&mutex);

Example Web Server can handle only 10 threads at a time Multiple points where threads are being created How to ensure no more than 10 active threads? Semaphore with initial value = 10 down() before Thread creation up() once Thread finishes