Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.

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

Operating Systems Part III: Process Management (Process Synchronization)
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.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: 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.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
The Critical-Section Problem
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
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.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
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.
Concurrency, Mutual Exclusion and Synchronization.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
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.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
3.1. Concurrency, Critical Sections, 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.
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
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.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
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
G.Anuradha Reference: William Stallings
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
The Critical-Section Problem
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Lecture 21 Syed Mansoor Sarwar
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
Presentation transcript:

Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software Engineering University of Wisconsin-Platteville

Outlines  Critical Section Problem  Mutual Exclusion  Mutual Exclusion Algorithms  Synchronization Hardware  Semaphores

Critical Section Problem  n processes all competing to use some shared data  Each process has a code segment, called critical section, in which the shared data is accessed.  Mutual Exclusion (MX) – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section  MX – basic synchronization problem

MX: Hardware Support 1  A process runs until it invokes an operating-system service or until it is interrupted  Interrupt Problem: MX violation  Hardware Solution:  Single Processor: Interrupt Disabling –Processor is limited in its ability to interleave programs Disabling interrupts guarantees mutual exclusion  Multiprocessor: disabling interrupts will not guarantee mutual exclusion

MX: General Solution  There is a set of threads/processes that switch between executing the critical section (CS) of code and non-critical section  Before entering the CS, a thread/process requests it  MX Solution requirement:  safety – at most one thread at a time execute the CS  liveness – a thread requesting the CS is given a chance to enter it Liveness violation: starvation – a requesting thread is not allowed to enter the CS –deadlock – all threads are blocked and cannot proceed –livelock – threads continue to operate but none could enter the CS

MX: Process Structure  General structure of process P i

MX: Algorithm 1 P1 ( ) { while (true) { while (turn != 1) ; /* do nothing */ CS turn = 2; non-CS } } P2 ( ) { while (true) { while (turn != 2) ; /* do nothing */ CS turn = 1; non-CS }  Pre condition: only two processes/threads in the system  Shared variables:  int turn;  OS sets turn to an initial value (1 or 2)  Advantages:  Safety  Problems:  Violates liveness

MX: Algorithm 2a  Pre condition: only two processes/threads in the system  Shared variables:  Boolean variables P1_in_CS P2_in_CS Boolean variables initially false  Advantages:  liveness  Problems:  Violates Safety P1 ( ) { while (true) { while (P2_in_CS == true) ; /* do nothing */ P1_in_CS = true; CS P1_in_CS = false; non-CS } P2 ( ) { while (true) { while (P1_in_CS == true) ; /* do nothing */ P2_in_CS = true; CS P2_in_CS = false; non-CS }

MX: Algorithm 2b  Pre condition: only two processes/threads in the system  Shared variables:  Boolean variables P1_in_CS P2_in_CS Boolean variables initially false  Advantages:  Safety  Problems:  Violates liveness P1 ( ) { while (true) { P1_in_CS = true; while (P2_in_CS == true) ; /* do nothing */ CS P1_in_CS = false; non-CS } P2 ( ) { while (true) { P2_in_CS = true; while (P1_in_CS == true) ; /* do nothing */ CS P2_in_CS = false; non-CS }

MX: Algorithm 3 (Peterson’s)  Pre condition: only two processes/threads in the system  Shared variables:  int turn; initially turn = 0  Boolean variables P1_in_CS P2_in_CS Boolean variables initially false  Advantages:  Safety  liveness P1 ( ) { while (true) { P1_in_CS = true; turn=2; while (P2_in_CS == true && turn==2) ; /* do nothing */ CS P1_in_CS = false; non-CS } P2 ( ) { while (true) { P2_in_CS = true; turn=1; while (P1_in_CS == true && turn==1) ; /* do nothing */ CS P2_in_CS = false; non-CS }}

MX: Multiple Processes  Bakery Algorithm  MX solution for n processes  Algorithm:  Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section.  If processes P i and P j receive the same number, if i < j, then P i is served first; else P j is served first.  The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...

MX: Multiple Processes  Shared data  boolean choosing[n]  int number[n]  Data structures are initialized to false and 0 respectively int i; /* unique process id */ while (true) { choosing[i]=true; number[i]=max(number[0],…, number[n-1])+1; choosing[i]=false; for (j=0; j<n; j++) { while (choosing[j]) ; /* do nothing */ while (number[j]>0 && (number[j]<number[i] || number[j]==number[i] && j<i)) ; /do nothing */ } CS number[i] = 0; non-CS }

MX: Hardware Support 2  Special Machine Instructions  Test and Set Instruction  Swap (Exchange) Instruction

MX: Test and Set Instruction boolean TestAndSet (boolean &target) { boolean rv = target; tqrget = true; return rv; }  Shared data: boolean lock = false;  Process P i while (true) { while (TestAndSet(lock)) /* do nothing */ CS lock = false; non-CS }

MX: Swap Instruction void Swap (boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; }  Shared data: boolean lock=false;  Process P i while (true) { boolean key=true; while (key == true) Swap(lock,key); CS lock = false; non-CS }

MX: Special Machine Instructions  Advantages  Applicable to any number of processes on either a single processor or multiple processors sharing main memory  It is simple and therefore easy to verify  It can be used to support multiple critical sections  Disadvantages  Busy-waiting consumes processor time  Starvation is possible when a process leaves a critical section and more than one process is waiting.  Deadlock If a low priority process has the critical region and a higher priority process needs, the higher priority process will obtain the processor to wait for the critical region

Semaphores  Semaphore is a special integer variable that is used for synchronization  Semaphore supports two atomic operations : wait and signal  The atomicity means that the two operations on the same semaphore can not overlap  How semaphore works ?  The semaphore initialized to 1 or a positive value  Before entering the critical section, a process/thread calls wait (semaphore)  After leaving the critical section, a process /thread calls signal (semaphore)

Semaphores  Assume our semaphore is called s wait (s):signal (s): s = s – 1s = s + 1 if (s < 0)if (s <= 0) block the thread wake up & run one of that called wait(s) the waiting threads otherwise continue into CS  Note :  Negative semaphore = number of blocked threads, where only one right now in the critical section

Using Semaphores for MX P1 () { while (true) { wait(s); /* CS */ signal(s); /* non-CS */ } P2 () { while (true) { wait(s); /* CS */ signal(s); /* non-CS */ }

Readers /Writers Problem int readcount=0; semaphore wrt(1),mx(1); writer() { wait(wrt); /* perform write */ signal(wrt); } reader() { wait(mx); readcount++; if(readcount==1) wait(wrt); signal(mx); /* perform read */ wait(mx); readconut--; if(readcount==0) signal(wrt); signal(mx); }  Readers and writers can access concurrently same item  writers cannot concurrently writing same item, readers can  If a writer is writing the item, no reader may read it  Any writer must wait for all readers  First reader races with writers  Last reader signals writers  Readers can starve writers  readcount - number of readers wishing to read the item  mx - protects manipulation with readcount  wrt- writer can get to item if open