Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.

Slides:



Advertisements
Similar presentations
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Advertisements

Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6: Process Synchronization
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.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Day 14 Concurrency. Software approaches Programs must be written such that they ensure mutual exclusion. Petersons and Dekkers (Appendix B)
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
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.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
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.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
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.
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 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.
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.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
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
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
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
Chapter 5: Process Synchronization – Part 3
Chapter 5: Process Synchronization – Part II
Concurrency: Mutual Exclusion and Synchronization
Critical Section and Critical Resources
Chapter 5: Process Synchronization
Critical Section and Critical Resources
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization and Semaphores
Concurrency: Mutual Exclusion and Synchronization
Chapter 6 Synchronization Principles
Thread Synchronization including Mutual Exclusion
Synchronization.
Presentation transcript:

Enforcing Mutual Exclusion, Semaphores

Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches Support from the OS/compiler 2

OS support: Semaphores Two or more processes can cooperate with each other by means of signals Two (atomic) operations: semSignal(s) : send signal via semaphore s semWait(s) : receive signal via semaphore s ; process is suspended until signal is received Observation: this is an abstract data type (a set of values together with operations on those values) Any complex coordination requirement can be satisfied by appropriate signal structure 3

Mutual Exclusion Using Semaphores Shared data: semaphore mutex; /* Initial value determines … */ Process Pi: do { semWait(mutex); /* … effect of first call */ /* critical section */ semSignal(mutex); /* remainder section */ } while (1); 4

Semaphore Implementations Stallings: Semaphore initialized to nonnegative integer value semWait(s) : s = s – 1 if s < 0 then block process, otherwise continue executing semSignal(s) : s = s + 1 if s <= 0 then unblock one blocked process Example of a counting or general semaphore vs. a binary semaphore (later) If blocked processes are unblocked in FIFO order, the semaphore is strong; otherwise weak 5

Semaphore Implementations (cont.) semWait(S) : S.value--; if (S.value < 0) { add this process to S.L; block; } semSignal(S) : S.value++; if (S.value <= 0) { remove a process P from S.L; add P to ready queue } 6

Semaphore Implementations (cont.) POSIX int sem_init( sem_t *, int, unsigned ) Initializes the semaphore to a nonnegative integer value int sem_post( sem_t * ) If semaphore value is zero and at least one thread is blocked on the semaphore, Increments the semaphore Unblocks one of those threads Otherwise, just increments the semaphore int sem_wait( sem_t *, int, unsigned ) If semaphore value is zero, thread blocks If semaphore value is positive, decrements semaphore (and continues executing) Other operations for convenience and efficiency (see man semaphore.h ) 7

Classical Synchronization Problems Producer/Consumer and Bounded-Buffer Problems Readers and Writers Problem Dining-Philosophers Problem 8

Producer/Consumer Problem One or more producers are generating data and placing these in a buffer A single consumer is taking items out of the buffer one at time Only one producer or consumer may access the buffer at any one time 9

Producer/Consumer Problem 10

Producer/Consumer Problem Infinite buffer – no possibility of overflow Bounded buffer – possibility of overflow Empty buffer – consumer must be stopped Mutual exclusion and synchronization 11

Producer Process (Infinite Buffer) while (true) { /* produce item v */ b[in] = v; in++; } 12

Consumer Process (Infinite Buffer) while (true) { while (in <= out) /*do nothing */; w = b[out]; out++; /* consume item w */ } 13

14

15

Producer with Finite Circular Buffer while (true) { /* produce item v */ while ((in + 1) % n == out) /* do nothing */; b[in] = v; in = (in + 1) % n } 16

Consumer with Finite Circular Buffer while (true) { while (in == out) /* do nothing */; w = b[out]; out = (out + 1) % n; /* consume item w */ } 17

18

19