Race Conditions Critical Sections Deker’s Algorithm.

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

Ken Birman 1. Refresher: Dekers Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true; turn = J; while(inside[J]
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
1 Implementations: User-level Kernel-level User-level threads package each u.process defines its own thread policies! flexible mgt, scheduling etc…kernel.
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.
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.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
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.
Chapter 3 The Critical Section Problem
Race Conditions. Isolated & Non-Isolated Processes Isolated: Do not share state with other processes –The output of process is unaffected by run of other.
1 Friday, June 16, 2006 "In order to maintain secrecy, this posting will self-destruct in five seconds. Memorize it, then eat your computer." - Anonymous.
Concurrency.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Parallel Programming and Synchronization P&H Chapter 2.11.
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.
Critical Sections with lots of Threads. Announcements CS 414 Homework due today.
Race Conditions Critical Sections Dekker’s Algorithm.
Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling.
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.
Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,
Synchronization CSCI 444/544 Operating Systems Fall 2008.
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 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
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.
Nachos Phase 1 Code -Hints and Comments
The Critical Section Problem
COSC 3407: Operating Systems Lecture 6: Synchronization.
Concurrency, Mutual Exclusion and Synchronization.
Lecture 2 Foundations and Definitions Processes/Threads.
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION OPERATING SYSTEMS.
Games Development 2 Concurrent Programming CO3301 Week 9.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
CIS 842: Specification and Verification of Reactive Systems Lecture INTRO-Examples: Simple BIR-Lite Examples Copyright 2004, Matt Dwyer, John Hatcliff,
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Critical Sections with lots of Threads. Refresher: Deker’s Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true;
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Parallelism and Concurrency
Background on the need for Synchronization
Chapter 5: Process Synchronization
Threads Ken Birman.
Race Conditions Critical Sections Dekker’s Algorithm
Grades.
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

Race Conditions Critical Sections Deker’s Algorithm

Threads share global memory When a process contains multiple threads, they have –Private registers and stack memory (the context switching mechanism needs to save and restore registers when switching from thread to thread) –Shared access to the remainder of the process “state” This can result in race conditions

some slides taken from Mendel Rosenblum's lecture at Stanford Two threads, one counter Popular web server Uses multiple threads to speed things up. Simple shared state error: –each thread increments a shared counter to track number of hits What happens when two threads execute concurrently? … hits = hits + 1; …

Shared counters Possible result: lost update! One other possible result: everything works.  Difficult to debug Called a “race condition” hits = read hits (0) hits = read hits (0) T1 T2 hits = 1 hits = 0 time

Race conditions Def: a timing dependent error involving shared state –Whether it happens depends on how threads scheduled –In effect, once thread A starts doing something, it needs to “race” to finish it because if thread B looks at the shared memory region before A is done, it may see something inconsistent Hard to detect: –All possible schedules have to be safe Number of possible schedule permutations is huge Some bad schedules? Some that will work sometimes? –they are intermittent Timing dependent = small changes can hide bug

If i is shared, and initialized to 0 –Who wins? –Is it guaranteed that someone wins? –What if both threads run on identical speed CPU executing in parallel Scheduler assumptions Process b: while(i > -10) i = i - 1; print “B won!”; Process a: while(i < 10) i = i +1; print “A won!”;

Scheduler Assumptions Normally we assume that –A scheduler always gives every executable thread opportunities to run In effect, each thread makes finite progress –But schedulers aren’t always fair Some threads may get more chances than others –To reason about worst case behavior we sometimes think of the scheduler as an adversary trying to “mess up” the algorithm

Critical Section Goals Threads do some stuff but eventually might try to access shared data CSEnter(); Critical section CSExit(); T1 T2 time CSEnter(); Critical section CSExit(); T1 T2

Critical Section Goals Perhaps they loop (perhaps not!) T1 T2 CSEnter(); Critical section CSExit(); T1 T2 CSEnter(); Critical section CSExit();

Critical Section Goals We would like –Safety: No more than one thread can be in a critical section at any time. –Liveness: A thread that is seeking to enter the critical section will eventually succeed –Fairness: If two threads are both trying to enter a critical section, they have equal chances of success … in practice, fairness is rarely guaranteed

Solving the problem CSEnter() { while(inside) continue; inside = true; } A first idea: –Have a boolean flag, inside. Initially false. CSExit() { inside = false; } Now ask: –Is this Safe? Live? Fair? Code is unsafe: thread 0 could finish the while test when inside is false, but then 1 might call CSEnter() before 0 can set inside to true!

Solving the problem: Take 2 CSEnter(int i) { inside[i] = true; while(inside[i^1]) continue; } A different idea (assumes just two threads): –Have a boolean flag, inside[i]. Initially false. CSExit(int i) { Inside[i] = false; } Now ask: –Is this Safe? Live? Fair? Code isn’t live: with bad luck, both threads could be looping, with 0 looking at 1, and 1 looking at 0

Solving the problem: Take 3 CSEnter(int i) { while(turn != i) continue; } Another broken solution, for two threads –Have a turn variable, turn, initially 1. CSExit(int i) { turn = i ^ 1; } Now ask: –Is this Safe? Live? Fair? Code isn’t live: thread 1 can’t enter unless thread 0 did first, and vice-versa. But perhaps one thread needs to enter many times and the other fewer times, or not at all

A solution that works Deker’s Algorithm (book: Section ) CSEnter(int i) { int J = i^1; inside[i] = true; turn = J; while(inside[J] && turn == J) continue; } CSExit(int i) { Inside[i] = false; }

Why does it work? Safety: Suppose thread 0 is in the CS. Then inside[0] is true. If thread 1 was simultaneously trying to enter, then turn must equal 0 and thread 1 waits If thread 1 tries to enter “now”, it sets turn to 0 and waits Liveness: Suppose thread 1 wants to enter and can’t (stuck in while loop) –Thread 0 will eventually exit the CS –When inside[0] becomes false, thread 1 can enter –If thread 0 tries to reenter immediately, it sets turn=1 and hence will wait politely for thread 1 to go first!