Chapter 6 – Process Synchronisation (Pgs 225 – 267)

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
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.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Synchronization Principles Gordon College Stephen Brinton.
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.
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.
What we will cover… Process Synchronization Basic Concepts
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
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,
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
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.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 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.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
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.
CE Operating Systems Lecture 8 Process Scheduling continued and an introduction to process synchronisation.
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.
Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization – Part 3
Background on the need for Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Chapter 6 – Process Synchronisation (Pgs 225 – 267)

Overview  Consider a block of shared memory  Process P wants to write data to the shared memory while Process R wants to read the data  Up until now, we have needed to use a synchronous system call to cause the reader to wait until the write is complete  The approach works because the synchronous system calls provide mutual exclusion

Mutual Exclusion  When a resource (i.e., memory) can be accessed by only one process at a time, we say that the resource is mutually exclusive  The code in each process to access the mutually exclusive resource is known as a critical section  Only one processes critical section may run at a time (and hence only one process can access the resource at a time)

Race Conditions  Pre-emption means that, essentially, the statements of any two processes can be interleaved to create any ordering of their parts  If the ordering of the parts affects the outcome of computation, we say that a race condition exists  To prevent race conditions, we use mutual exclusion and critical sections

Example  x++ increments x  R1  mem[x]; R1  R1 + 1; mem[x]  R1;  What if P1 does x++ and P2 does x--? End result is that x should never change regardless of the ordering  Assume x starts at 4.. R1  mem[x]; R1  R1 + 1; *Pre-empt* R2  mem[x]; R2  R2 - 1 ; *Pre-empt* mem[x]  R1; *Pre-empt* mem[x]  R2;.... x is now 3, not Idea: x++ must fully execute before x– (or reverse)

Critical Section  Must provide the following 1. Mutual Exclusion: Only one process at a time can be in their critical section 2. Progress: Processes must be allowed to make progress and be allowed (eventually) to enter their critical section 3. Bounded Waiting: There is a limit on how many times other processes can perform their critical sections and thus block any other process from entering theirs

Peterson's Solution  Basic software solution for processes i and j do { flag[i] = TRUE; turn = j; while (flag[j] && turn==j); /* Critical Section */ flag[i] = FALSE; /* Remainder */ } while (TRUE); Highly concurrent (threaded) CPUs can invalidate this solution

Locks  Use a lock to protect critical section  Process must acquire the lock before entering and release it after leaving  Both hardware and software solutions are possible  Simple with hardware (disable interrupts when shared variable – lock – is being modified), but inefficient and potentially dangerous

Lock Example boolean TestAndSet (bool *t) { // If False: return false, set to true // If True: return true, set to true bool rv = *t; *t = TRUE; return rv; } Then... do { // Loop while lock is set while (TestAndSet(&lock)); /* Critical section */ lock = FALSE; /* remainder */ } Works if TestAndSet is Atomic and cannot be interrupted

Problems  Hardware solutions are difficult to use since they only provide mutual exclusion  Bounded waiting requires considerable and complex "extra" code (see Fig. 6.8)  More complex for n processes than for 2  Difficult for engineers to implement as they require memory access

Semaphores  Integer variable accessed using on the operations wait and signal wait(s) { while (s <= 0) ; s--; } signal(s) { s++; } wait and signal must be atomic in their manipulation of s!

Using a Semaphore do { wait(mutex) ; /* critical section */ signal(mutex) } while (TRUE); Main problem is "busy waiting" – constant checking of mutex in wait requires CPU instructions (being busy) to do nothing except wait

Semaphores++  Semaphores with busy waiting are often called "spinlocks"  Can be generalised for bounded resources usable by more than one process  Simple to program with and provide bounded waiting  Can remove busy waiting if we tell the kernel to use a queue for the semaphore and block all processes that are waiting  Signal would wake up a waiting process

Deadlock + Starvation  When two processes are each waiting for a resource held by the other process "Deadlock" can occur  E.g., 4 cars approach an interesection, each blocking one of the 4 roads out, each waits for the others to move and clear a road forward  Starvation occurs from infinite waiting  E.g., process that is supposed to do the wakeup fails and the wakeup is never sent

Priority Inversion  Requires more than 2 priorities  Higher priority process blocked from critical section by lower priority processes  Solution: Priority Inheritance  Process inherits the priority of the highest process that is blocked

Classic Problems  Bounded Buffer (Producer/Consumer)  Readers-Writers: Simultaneous reads, writes block everything  Dining Philosophers

Monitors  Semaphores only work when used properly, by all processes  Solution is to pre-code the critical sections  A Monitor is an ADT  Only one thread at a time can be in a monitor  Usually provides a way for a thread to yield the monitor (or block) so another thread can run  Many different models of monitors (See Buhr et al. for details)

Posix Semaphores Provided by semaphore.h:  sem_init() – creates and initialises it  sem_open() – opens it  sem_close() – closes it after we are done  sem_post() – releases the semaphore  sem_wait() – obtains the semaphore  sem_trywait() – asynchronous wait

To Do:  Work on Lab 4  Read Chapter 6 (pgs ; this lecture)  Read Chapter 7 (pgs ; next lecture)