Day 14 Concurrency. Software approaches Programs must be written such that they ensure mutual exclusion. Petersons and Dekkers (Appendix B)

Slides:



Advertisements
Similar presentations
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Seventh Edition By William Stallings.
Concurrency: mutual exclusion and synchronization Slides are mainly taken from «Operating Systems: Internals and Design Principles”, 8/E William Stallings.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Lecture 5: Concurrency: Mutual Exclusion and Synchronization.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Synchronization Principles Gordon College Stephen Brinton.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and 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.
Concurrency: Mutual Exclusion and Synchronization
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Concurrency : Mutual Exclusion and Synchronization
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Synchronization Solutions
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Chapter 5 Concurrency. 2 Concurrency 3 4 Mutual Exclusion: Hardware Support Test and Set Instruction boolean testset (int *i) { if (*i == 0) { *i.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
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.
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.
1 Concurrency: Mutual Exclusion and Synchronization Module 2.2.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Synchronizing Threads with Semaphores
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
CIS Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Operating System Chapter 5. Concurrency: Mutual Exclusion and Synchronization Lynn Choi School of Electrical Engineering.
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.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Process Synchronization: Semaphores
Concurrency: Mutual Exclusion and Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Synchronization and Semaphores
Concurrency: Mutual Exclusion and Synchronization
Thread Synchronization including Mutual Exclusion
Synchronization.
Presentation transcript:

Day 14 Concurrency

Software approaches Programs must be written such that they ensure mutual exclusion. Petersons and Dekkers (Appendix B)

Operating system support – Semaphores A semaphore “s” is an integer variable that has a queue associated with it. If the queue is a FIFO queue, then the semaphore is called a strong semaphore, which prevents starvation. A process that blocks on semaphore s, is placed in its queue. A semaphore can be used to enforce mutual exclusion and to synchronize processes.

Semaphores Only three operations: A semaphore s can be initialized to a non-negative integer. The semwait(s) procedure or P(s) can be used by a process to wait for a signal from another process. The semsignal(s) or V(s) procedure can be used by a process to signal another (blocked) process. semwait() can block a process and semsignal() can unblock a process. semwait(s) and semsignal(s) are atomic operations.

General or Counting semaphore - used to synchronize processes semaphore s.count = n; semwait(s){ s.count--; if(s.count < 0){ add this process to s.queue; block process; } semsignal(s){ s.count++; if(s.count <= 0){ remove a process from s.queue; move the process to the ready queue; } }

s.count >= 0 - # of processes that will not block on semwait(s) and will be able to enter the CS. s.count < 0 - # of processes that are blocked on s.

Binary semaphore or mutex(used for mutual exclusion) Binary semaphore s = 0 or 1; semwaitB(s){ If(s.value==1){ s.value=0; } else{ place this process in s.queue; block this process; } semsignalB(s){ if(s.queue is empty) s.value=1; else{ remove a process from the s.queue; move the process to the ready queue; } }

Mutual exclusion with semaphores semaphore s = 1; int n = num_of_processes; void Q(int i){ while(true){ semwait(s); semsigna(s); } void main(){ parbegin(Q(1),Q(2),…,Q(n)); }

Implementing semaphores – using testset() s.flag = 0; semWait(s){ while(!testset(s.flag)){ } s.count--; if (s.count < 0){ place this process in the s.queue and block this process } s.flag = 0; } semsignal(s){ while(!testset(s.flag)){ } s.count++; if(s.count < 0){ remove a process P from s.queue and place the process P on ready list } s.flag = 0; } boolean testset ( i){ If(i == 0){ i = 1; return true; } else{ return false; }

Semaphores Is there busy-waiting? Hardware-software solution Hardware support needed for the OS to support a semaphore Must share a common variable (like “bolt” in testset instruction) Processes must share memory

Producer-consumer problem n – items in the buffer infinite size buffer Producer(){ while(true){ produce(); append(); n++; } Consumer(){ while(true){ if(n != 0){ take(); n--; consume(); }

Consumer-Producer One or many producers One or more consumers Infinite buffer – no overflow Finite buffer - overflow Empty buffer – consumer must be stopped Mutual exclusion and synchronization

Possible solution1 (infinite buffer) binary semaphore s = 1; int n = 0; binary semaphore delay = 0; Producer(){ while(true){ produce(); semwaitB(s); append(); n++; if(n==1) semsignalB(delay); semsignalB(s); } Consumer(){ semwaitB(delay); while(true){ semwaitB(s); take(); n--; semsignalB(s); consume(); if(n==0) semwaitB(delay); }

Using the code in the previous slide, complete the following table by determining the values of semaphores “s” and “delay” and the count “n” for each of the statements listed.

Correction on page 224, figure 5.3 Column “Consumer” If(n == 0) NOT if (n == 1)

Possible solution 2 binary semaphore s = 1; int n = 0; binary semaphore delay = 0; Producer(){ while(true){ produce(); semwaitB(s); append(); n++; if(n==1) semsignalB(delay); semsignalB(s); } Consumer(){ semwaitB(delay); while(true){ semwaitB(s); take(); n--; consume(); if(n==0) semwaitB(delay); semsignalB(s); }

Another possible solution (3) semaphore s = 1; semaphore n = 0; Producer(){ while(true){ produce(); semwait(s); append(); semsignal(s); semsignal(n); } Consumer(){ while(true){ semwait(s); semwait(n); take(); semsignal(s); consume(); }

A correct solution semaphore s = 1; semaphore n = 0; Producer(){ while(true){ produce(); semwait(s); append(); semsignal(s); semsignal(n); } Consumer(){ while(true){ semwait(n); semwait(s); take(); semsignal(s); consume(); }