Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.

Slides:



Advertisements
Similar presentations
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Advertisements

Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Lecture 5: Concurrency: Mutual Exclusion and Synchronization.
Stuff  Exam timetable now available  Class back in CS auditorium as of Wed, June 4 th  Assignment 2, Question 4 clarification.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Classical Problems of Concurrency
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
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.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Process Synchronization
Chapter 2.3 : Interprocess Communication
Semaphores CSCI 444/544 Operating Systems Fall 2008.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Synchronization Solutions
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
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.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
1 Previous Lecture Review n Concurrently executing threads often share data structures. n If multiple threads are allowed to access shared data structures.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Synchronizing Threads with Semaphores
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Synchronicity II Introduction to Operating Systems: Module 6.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Web Server Architecture Client Main Thread for(j=0;j
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
PARALLEL PROGRAM CHALLENGES
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Topic 6 (Textbook - Chapter 5) Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization Hank Levy 1.
Lecture 2 Part 2 Process Synchronization
Chapter 6: Synchronization Tools
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 19
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
Presentation transcript:

Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process Scheduling 4.3 Operations on Processes  Chapter 6 - CPU Scheduling: 6.1 Basic Concepts 6.2 Scheduling Criteria 6.3 Scheduling Algorithms  Chapter 7 - Process Synchronization: 7.1 Background 7.2 Critical-Section Problem 7.3 Synchronization Hardware

Process Synchronization Continued 7.4 Semaphores 7.5 Classic Problems of Synchronization Producer/Consumer (unbounded buffer) Producer/Consumer (bounded buffer)

Busy Waiting Semaphores  The simplest way to implement semaphores.  Useful when critical sections last for a short time, or we have lots of CPUs.  S initialized to positive value (to allow someone in at the beginning). wait(S): while S<=0 do ; S--; signal(S): S++; S is an integer variable that, apart from initialization, can only be accessed through 2 atomic and mutually exclusive operations:

Using semaphores for solving critical section problems  For n processes  Initialize semaphore “mutex” to 1  Then only one process is allowed into CS (mutual exclusion)  To allow k processes into CS at a time, simply initialize mutex to k Process P i : repeat wait(mutex); CS signal(mutex); RS forever

Synchronizing Processes using Semaphores  Two processes: P 1 and P 2  Statement S 1 in P 1 needs to be performed before statement S 2 in P 2  Need to make P 2 wait until P 1 tells it it is OK to proceed  Define a semaphore “synch” Initialize synch to 0  Put this in P 2 : wait(synch); S 2 ;  And this in in P 1 : S 1 ; signal(synch);

Busy-Waiting Semaphores: Observations  When S>0: the number of processes that can execute wait(S) without being blocked = S  When S=0: one or more processes are waiting on S  Semaphore is never negative  When S becomes >0, the first process that tests S enters enters its CS random selection (a race) fails bounded waiting condition

Blocking Semaphores  In practice, wait and signal are system calls to the OS The OS implements the semaphore.  To avoid busy waiting: when a process has to wait on a semaphore, it will be put in a blocked queue of processes waiting for this to happen.  Queues are normally FIFO. This gives the OS control on the order processes enter CS. There is one queue per semaphore just like I/O queues.

Blocking Semaphores: Implementation  A semaphore can be seen as a record (structure): typedef struct { int count; struct PCB *queue; } semaphore; semaphore S;  When a process must wait for a semaphore S, it is blocked and put on the semaphore’s queue  Signal(S) removes one process from the queue and moves it to Ready.

Semaphore Operations in OS (atomic) void wait(semaphore S){ S.count--; if (S.count<0) { add this process to S.queue block this process } signal(S){ S.count++; if (S.count<=0) { move one process P from S.queue to ready list } Negative count indicates number of processes waiting

Semaphores: Implementation  wait() and signal() themselves contain critical sections! How to implement them?  Notice: they are very short critical sections.  Solutions: uniprocessor: disable interrupts during these operations (ie: for a very short period).  Fails on a multiprocessor machine. multiprocessor: use some busy waiting scheme, such as test-and-set.  The busy-wait will be short, so it can be tolerated.

Deadlocks and Semaphores Process P 0 : wait(S); wait(Q);. signal(S); signal(Q); Process P 1 : wait(Q); wait(S);. signal(Q); signal(S); This could function correctly sometimes, but: What if P 0 reaches dotted line, and context switch to P 1 ?

Binary Semaphores  The semaphores we have studied are called counting semaphores  We can also have binary semaphores similar to counting semaphores except that “count” can only be 0 or 1 simpler to implement on some hardware  Can still be used in “counting” situations need to add additional counting variables protected by binary semaphores. See example in section 7.4.4

Binary Semaphores waitB(S): if (S.value == 1) { S.value = 0; } else { place this process in S.queue block this process } signalB(S): if (S.queue is empty) { S.value = 1; } else { move a process P from S.queue to ready list }

Some Classic Synchronization Problems  Bounded Buffer (Producer/Consumer)  Dining Philosophers Problem  Readers-Writers Problem

The Producer/Consumer Problem  A producer process produces information that is consumed by a consumer process Example: Implementation of pipes on Unix systems  We need a buffer to hold items that are produced and eventually consumed  and a way for the producer and the consumer of the items to coordinate their access to the buffer  A common paradigm for cooperating processes

Producer/Consumer: Unbounded Buffer  We look first at an unbounded buffer consisting of a linear array of elements  in points to the next item to be produced  out points to the next item to be consumed  Number of elements = (in-out)

Unbounded Buffer: Observations  If only the producer alters the pointer in and only the consumer alters out, and only the producer writes to the buffer itself, mutual exclusion in this simple case may not be an issue if the code is written carefully  The producer write whenever it wants, but ..the consumer must check to make sure the buffer is not “empty” (in==out)?  So the consumer may have to busy-wait, waiting for the producer to provide at least one item

Pitfalls with Simple Solution  Producer basically does b[in] = item; in++;  and consumer does while (out >= in) ; /* Wait… */ item = b[out]; out++;  What could happen if the producer adjusted “in” before it put the data in? in++; b[in-1] = produced_item;

Producer/Consumer: Unbounded Buffer Semaphore Solution  Let’s make it “clean” declare the buffer and its pointers to be critical data And protect them in a critical section  Use a semaphore “mutex” to perform mutual exclusion on the buffer and pointers  Use another semaphore “number” to synchronize producer and consumer on the number (= in - out) of items in the buffer an item can be consumed only after it has been created (The semaphore value itself is the item count)

Producer/Consumer: Unbounded Buffer  The producer is free to add an item into the buffer at any time: it performs wait(mutex) before appending and signal(mutex) afterwards to prevent access by the consumer  It also performs signal(number) after each append to increment number  The consumer must first do wait(number) to see if there is an item to consume and then use wait(mutex) / signal(mutex) to access the buffer

Solution of Producer/Consumer: Unbounded Buffer Producer: repeat produce item; wait(mutex); append(item); signal(mutex); signal(number); forever Consumer: repeat wait(number); wait(mutex); item:=take(); signal(mutex); consume item; forever Initialization: mutex.count:=1; //mutual exclusion number.count:=0; //number of items in:=out:=0; //indexes to buffer critical sections append(item): b[in]:=item; in++; take(): item:=b[out]; out++; return item;

Producer/Consumer: Unbounded Buffer  Remarks: Putting signal(number) inside the CS of the producer (instead of outside) has no useful effect since the consumer must always wait for both semaphores before proceeding The consumer must perform wait(number) before wait(signal), otherwise deadlock occurs if consumer enters CS while the buffer is empty. Why?  because it would lock the producer out! Disaster if you forget to do a signal after a wait.  So using semaphores still has pitfalls...  Now let’s look at what happens if the buffer is bounded

Producer/Consumer: Circular Buffer of Size k (Bounded Buffer)  can consume only when number of (consumable) items is at least 1 (now: number != in-out)  can produce only when number of empty spaces is at least 1

Producer/Consumer: Bounded Buffer  Again: Use a semaphore “mutex” for mutual exclusion on buffer access and a semaphore “full” to synchronize producer and consumer on the number of consumable items (full spaces)  But we have to add: a semaphore “empty” to synchronize producer and consumer on the number of empty spaces

Producer/Consumer: Bounded Buffer (Solution) Initialization: mutex.count:=1; //mutual excl. full.count:=0; //full spaces empty.count:=k; //empty spaces Producer: repeat produce item; wait(empty); wait(mutex); append(item); signal(mutex); signal(full); forever Consumer: repeat wait(full); wait(mutex); item:=take(); signal(mutex); signal(empty); consume(item); forever critical sections append(item): b[in]:=item; in=(in+1)mod k; take(): item:=b[out]; out=(out+1)mod k; return item;

The Dining Philosophers Problem ( read for next class)  5 philosophers who only eat and think  each needs to use 2 forks for eating  but we have only 5 forks!  A classical synchronization problem  Illustrates the difficulty of allocating resources among process without deadlock and starvation