Synchronization Mutual Exclusion Solutions using shared variables + problems such as deadlocks, starvation, progressiveness, busy wait Prof. R K Joshi.

Slides:



Advertisements
Similar presentations
Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black.
Advertisements

Synchronization. How to synchronize processes? – Need to protect access to shared data to avoid problems like race conditions – Typical example: Updating.
 Read about Therac-25 at  [  [
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CSC321 Concurrent Programming: §3 The Mutual Exclusion Problem 1 Section 3 The Mutual Exclusion Problem.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Concurrent Programming Problems OS Spring Concurrency pros and cons Concurrency is good for users –One of the reasons for multiprogramming Working.
Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2)
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
China’s Software Industry August 2006 Instructor: Hengming Zou, Ph.D.
Critical Section chapter3.
Chapter 3 The Critical Section Problem
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
© 2004, D. J. Foreman 1 The Dining Philosophers Problem.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Dining-Philosophers Problem Shared data fork[5]: semaphore; initialized to 1.
Concurrency.
1 Operating Systems, 112 Practical Session 5, Synchronization 1.
Shared Memory Coordination We will be looking at process coordination using shared memory and busy waiting. –So we don't send messages but read and write.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
6: Process Synchronization 1 1 PROCESS SYNCHRONIZATION I This is about getting processes to coordinate with each other. How do processes work with resources.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
University of Pennsylvania 9/19/00CSE 3801 Concurrent Processes CSE 380 Lecture Note 4 Insup Lee.
Process Synchronization
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
CS 603 Dining Philosopher’s Problem February 15, 2002.
Intro to OS CUCS Mossé Processes and Threads What is a process? What is a thread? What types? A program has one or more locus of execution. Each execution.
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
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.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
The Critical Section Problem
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Designing Parallel Algorithms (Synchronization)
The Critical-Section Problem
Critical Sections User Software Solutions Dekker’s Algorithm
Last Week Introduced operating systems Discussed the Kernel
Lecture 2 Part 2 Process Synchronization
Practical Session 5, Synchronization
CSE 153 Design of Operating Systems Winter 19
Don Porter Portions courtesy Emmett Witchel
Operating Systems {week 10}
CS444/544 Operating Systems II Scheduler
Presentation transcript:

Synchronization Mutual Exclusion Solutions using shared variables + problems such as deadlocks, starvation, progressiveness, busy wait Prof. R K Joshi CSE, IIT Bombay

We know about deadlocks already R2 R1 P1 P2 wait hold

The problem of mutual exclusion (exclusiveness) V p1 p2 Read Write Read Write

Attempt 1 use v as simple flag 0:free, 1:available P1 If (v==0) v=1 Use the resource V=0 Else repeat P2 If (v==0) v=1 Use the resource V=0 Else repeat Problem: both see v=0 Both go ahead, no mutual exclusion: correctness problem Due to context switch immediately after if (v==0) condition check Due to round robin scheduling or some other scheduling concern

Fix 1 use v as turn indicator 0:p1's turn, 1:p2's turn initial value: turn=0 or 1 P0 If (turn==0) Use the resource turn=1 Else repeat P1 If (turn==1) Use the resource turn=0 Else repeat Problem: if it's someone else's turn, and the process is not picking it up, then there is no progress for the interested process...

Attempt 2 v,w = 0 P1 V=1 while (true) { If (w=0) Use the resource else v=0 } P2 W=1 While (true) If (v==0) Use resource Else w=0 } Problem: P1 goes in, P2 cannot, P1 runs and then sets v=1 when it repeats P2 checks for v again, but fails, while P1 goes ahead and this can continue Starvation problem also correctness problem!

Attempt 3 v,w = 0 P1 while (true) { v=1 If (w=0) Use the resource else v=0 } P2 While (true) w=1 If (v==0) Use resource Else w=0 } Problem: P1 shows interest, P2 shows it too P1 withdraws, P2 also does the same They repeat the show interest-withdraw cycle for ever Livelock! Starvation problem: find it. correctness problem solved!

Attempt 4 claim: no starvation willing[0]=willing[1] = 0 P0 while (true) { willing[0]=1 If (!willing[1]) Use the resource willing[0]=0 } P1 While (true) willing[1]=1 If (!willing[0]) Use resource willing[1]=0 } P0 is willing, p1 is yet to set it's willingness, p0 goes in P1 comes in, sees p0 willing, it withdraws on the last line Now p0 quickly completes and and takes the resource once more This can repeat forever

Attempt 5 claim: no starvation, use a delay willing[0]=willing[1] = 0 P0 while (true) { willing[0]=1 If (!willing[1]) Use the resource Willing [0]=0 delay else Willing[0]=0 } P1 While (true) willing[1]=1 If (!willing[0]) Use resource Willing[1]=0 delay else willing[1]=0 } Doesnot solve the problem, however we have tried a new idea: that of delays

Attempt 56 claim: no starvation, use a delay willing[0]=willing[1] = 0 P0 while (true) { willing[0]=1 If (!willing[1]) Use the resource Willing [0]=0 while(willing[1]); else Willing[0]=0 } P1 While (true) willing[1]=1 If (!willing[0]) Use resource Willing[1]=0 While willing[0]; else willing[1]=0 } Doesnot solve the problem, however we have tried a new idea: that of delays

Attempt 6 claim: no starvation, use a condition willing[0]=willing[1] = 0 P0 while (true) { willing[0]=1 If (!willing[1]) Use the resource Willing [0]=0 while(willing[1]); else Willing[0]=0 } P1 While (true) willing[1]=1 If (!willing[0]) Use resource Willing[1]=0 While (willing[0]) ; else willing[1]=0 } Next idea: instead of delay, use a condition variable----> does not solve the problem

A suggestion: let's start all over again.. Let's put up the 'taking turns' solution after restructruring it a bit.. observe the separation of critical section entry and exit code..

Fix 1 use a turn indicator 0:p1's turn, 1:p2's turn initial value: turn=0 or 1 P0 while (turn==1); Use the resource turn=1 P1 while (turn==0); Use the resource turn=0 Problem: if it's someone else's turn, and the process is not picking it up, then there is no progress for the interested process...

Fix 7 use a turn indicator 0:p1's turn, 1:p2's turn initial value: turn=0 or 1 willing[0], willing[1]=0 P0 willing[0]=1 while (willing[1]); Use the resource willing[0]=0 P1 willing[1]=1 while (willing[0]); Use the resource willing[1]=0 Problem: if it's someone else's turn, and the process is not picking it up, then there is no progress for the interested process...

Fix 8: willing[0], willing[1]=0 P0 while (true) { willing[0]=1 while (willing[1]) { Willing[0]=0; Delay Willing[0]=1; } Use the resource willing[0]=0; } P1 while (true) { willing[1]=1 while (willing[0]) { Willing[1]=0; delay Willing[1]=1; } Use the resource willing[1]=0 }

Time for a solution?

Dekker's solution turn=willing[0]=willing[1]=0 While (true) { willing [0] = 1; while (willing[1]) { if (turn==1) willing[0]=0; while(turn==1); willing[0]=1; } CS willing[0]=0; turn=1; } While (true) { willing [1] = 1; while (willing[0]) { if (turn==0) willing[1]=0; while(turn==0); willing[1]=1; } CS willing[1]=0; turn=0;

Peterson's solution turn=willing[0]=willing[1]=0 While (true) { willing [0] = 1; turn=1; while (willing[1]&&turn==1); CS willing[0]=0; } While (true) { willing [1] = 1; turn=0; while (willing[0]&&turn==0); CS willing[1]=0; }