1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.) Advanced Operating System Fall 2009.

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.
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles Seventh Edition By 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 
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Concurrency: Mutual Exclusion and Synchronization Why we need Mutual Exclusion? Classical examples: Bank Transactions:Read Account (A); Compute A = A +
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
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 2.3 : Interprocess Communication
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Lecture : Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2008.
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 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
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.
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
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.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
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)
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
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.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
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.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
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.
Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Chapter 5: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization Hank Levy 1.
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 2019
Lecture 6: Concurrency: Mutual Exclusion and Synchronization
Presentation transcript:

1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.) Advanced Operating System Fall 2009

2 Three Environments 1. There is no central program to coordinate the processes. The processes communicate with each other through global variable. 2. Special hardware instructions 3. There is a central program to coordinate the processes.

3 Hardware Support Disable interrupt CS Enable interrupt Won’t work if we have multiprocessors

4 Special Machine Instructions Modern machines provide special atomic hardware instructions  Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words

5 Exchange(int register, int memory) Exchange the contents of a register with that of memory. Shared Variable: lock, initially 0 local variable: key Process P i … Prefix i Key i =1 While(Key i  0) do {exchange(Key i,lock)} CS i Lock=0; … suffix i

6 Three Environments 1. There is no central program to coordinate the processes. The processes communicate with each other through global variable. 2. Special hardware instructions 3. There is a central program to coordinate the processes.

7 Semaphores A variable that has an integer value upon which 3 operations are defined. Three operations: 1. A semaphore may be initialized to a nonnegative value 2. The wait operation decrements the semaphore value. If the value becomes negative, then the process executing the wait is blocked 3. The signal operation increments the semaphore value. If the value is not positive, then a process blocked by a wait operation is unblocked. Other than these 3 operations, there is no way to inspect or manipulate semaphores.

8 Wait(s) and Signal(s) Wait(s) – is also called P(s) { s=s-1; if (s<0) {place this process in a waiting queue} } Signal(s) – is also called V(s) { s=s+1; if(s  0) {remove a process from the waiting queue} }

9 Semaphore as General Synchronization Tool Counting semaphore – integer value can range over an unrestricted domain Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement Also known as mutex locks Wait B(s) s is a binary semaphore { if s=1 then s=0 else block this process } Signal B(s) { if there is a blocked process then unblock a process else s=1 } Can implement a counting semaphore S as a binary semaphore

10 Note The wait and signal primitives are assumed to be atomic; they cannot be interrupted and each routine can be treated as an indivisible step.

11 Mutual Exclusion provided by Semaphores Semaphore S; // initialized to 1 P i prefix i wait (S); CS i signal (S); suffix i Signal B(s) { if there is a blocked process then unblock a process else s=1 } Wait B(s) s is a binary semaphore { if s=1 then s=0 else block this process }

12 Message Passing (not covered) Direct Addressing Specific identifier of source and destination processes Send(destination, message) Receive(source, message) Indirect Addressing Messages are not sent directly from sender to receiver but rather are sent to a shared data structure consisting of queues that can temporarily hold messages. Such queues are generally referred to as mailboxes. Thus, for 2 processes to communicate, one process sends a message to the appropriate mailbox and the other process picks up the message from the mailbox. Mailbox P1 Pn … Q1 Qn … Sending processes Receiving processes

13 Message Passing (cont.) (not covered) When a send primitive is executed in a process, there are 2 possibilities: Either the sending process is blocked until the message is received Or it is not When a receive primitive is executed in a process, there are 2 possibilities: If a message has previously been sent, the message is received and execution continues If there is no waiting message, then either a) The process is blocked until a message arrives, or b) The process continues to execute, abandoning the attempt to receive. Blocking send, blocking receive Nonblocking send, blocking receive Nonblocking send, nonblocking receive

14 Mutual Exclusion (not covered) Create_mailbox(mutex) Send(mutex, null) P i Prefix i Receive(mutex,msg); CS i Send(mutex,msg); Suffix i Main process Mailbox name is mutex

15 Two classical examples Producer and Consumer Problem Readers/Writers Problem

16 Two classical examples Producer and Consumer Problem Readers/Writers Problem

17 Producer and Consumer Problem Producer can only put something in when there is an empty buffer Consumer can only take something out when there is a full buffer Producer and consumer are concurrent processes … 0 N-1 N buffers producer consumer

18 Producer and Consumer Problem(cont.) Global Variable 1. B[0..N-1] – an array of size N (Buffer) 2. P – a semaphore, initialized to N 3. C – a semaphore, initialized to 0 Local Variable 1. In – a ptr(integer) used by the producer, in=0 initially 2. Out – a ptr(integer) used by the consumer, out=0 initially Producer Process producer: produce(w) wait(p) B[in]=w in=(in+1)mod N signal(c) goto producer Consumer Process consumer: wait(c) w=B[out] out=(out+1)mod N signal(p) consume(w) goto consumer W is a local buffer used by the producer to produce W is a local buffer used by the consumer to store the item to be consumed

19 Two classical examples Producer and Consumer Problem Readers/Writers Problem

20 Readers/Writers Problem Suppose a data object is to be shared among several concurrent processes. Some of these processes want only to read the data object, while others want to update (both read and write) Readers – Processes that read only Writers – processes that read and write If a reader process is using the data object, then other reader processes are allowed to use it at the same time. If a writer process is using the data object, then no other process (reader or writer) is allowed to use it simultaneously.

21 Solve Readers/Writers Problem using wait and signal primitives(cont.) Global Variable: Wrt is a binary semaphore, initialized to 1; Wrt is used by both readers and writers For Reader Processes: Mutex is a binary semaphore, initialized to 1;Readcount is an integer variable, initialized to 0 Mutex and readcount used by readers only Reader Processes Wait(mutex) Readcount=readcount+1 If readcount=1 then wait(wrt) Signal(mutex); … Reading is performed … Wait(mutex) Readcount=readcount-1 If readcount=0 then signal(wrt) Signal(mutex) Writer Processes Wait(wrt) … Writing is performed … Signal(wrt)

22 End of lecture 6 Thank you!