1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
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.
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.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
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.
Synchronization. Shared Memory Thread Synchronization Threads cooperate in multithreaded environments – User threads and kernel threads – Share resources.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
CS510 Concurrent Systems Class 2 A Lock-Free Multiprocessor OS Kernel.
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 (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
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.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CE Operating Systems Lecture 5 Processes. Overview of lecture In this lecture we will be looking at What is a process? Structure of a process Process.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
Concurrency, Mutual Exclusion and Synchronization.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
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 Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Synchronicity Introduction to Operating Systems: Module 5.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
CSE 153 Design of Operating Systems Winter 2015 Lecture 5: Synchronization.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
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?
Processes Chapter 3. Processes in Distributed Systems Processes and threads –Introduction to threads –Distinction between threads and processes Threads.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Tutorial 2: Homework 1 and Project 1
Processes and threads.
CSE 120 Principles of Operating
Chapter 3: Process Concept
Background on the need for Synchronization
Chapter 5: Process Synchronization
Designing Parallel Algorithms (Synchronization)
Topic 6 (Textbook - Chapter 5) Process Synchronization
Grades.
Chapter 6: Process Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Foundations and Definitions
Process/Thread Synchronization (Part 2)
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo

2 Thread Control Block OS’s representation of thread –Registers, PC, SP, scheduling info, etc Several different lists of TCBs in OS –Ready list –Blocked lists Waiting for disks Waiting for input Waiting for events

3 Context Switching Switching between 2 threads –Change PC to current instruction of new thread might need to run old thread in the future –Must save exact state of first thread –State saved into the TCB What must be saved? –Registers (including PC and SP) –What about stack itself?

4 Implementing Threads in User Space A user-level threads package

5 Implementing Threads in the Kernel A threads package managed by the kernel

6 Hybrid Implementations Multiplexing user-level threads onto kernel- level threads

7 Independent vs. Cooperating Threads Independent threads –No state shared – separate address space Cooperating threads –Same address space –Can affect one another –Must be careful!

8 Example of Concurrent Program int balance = 100; void deposit (int amount) { balance += amount; exit(0); } int main () { threadCreate(deposit, 10); threadCreate(deposit, 20); waitForAllDone();/*make sure all children finish*/ printf (“The balance is %d”, balance); } What are the possible output?

9 More Concurrent Programming: Linked lists (head is shared) insert (head, elem) { Elem->next := head; head := elem; } void *removed (head) { void *tmp; tmp := head; head := head->next; return t; } Assume one thread calls insert and one calls remove.

10 Race Condition This kind of bug, which only occurs under certain timing conditions, is called a race condition. Output depends on ordering of thread execution More concretely: (1)two or more threads access a shared variable with no synchronization, and (2)at least one of the threads writes to the variable

11 Critical Section Section of code that: –Must be executed by one thread at a time –If more than one thread executes at a time, have a race condition –Ex: linked list from before Insert/Delete code forms a critical section What about just the Insert or Delete code?

12 Critical Section (CS) Problem Provide entry and exit routines –All threads must call entry before executing CS. –All threads must call exit after executing CS –Thread must not leave entry rounte until it’s safe.

13 Structure of threads for Critical Section Problem Threads do the following While (1) { call entry(); critical section call exit(); do other stuff; }

14 Properties of Critical Section Solution Mutual Exclusion: at most one thread is executing CS. Absence of Deadlock: two or more threads trying to get into CS => at least one succeeds. Absence of Unnecessary Delay: if only one thread trying to get into CS, it succeeds Bounded Waiting: thread eventually gets into CS.

15 Critical Section Solution Attempt #1 (2 thread solution) Intially, turn == 0 entry (id) { while (turn !=id); /* if not my turn, spin */ } exit(id) { turn = 1 - id; /* other thread’s turn*/ }

16 Critical Section Solution Attempt #2 (2 thread solution) Intially, flag[0] == flags[1] == false entry (id) { flag [id] = true;/* I want to go in */ while (flag[1-id]); /* proceed if other not trying */ } exit(id) { flag[id] = false; /* I am out*/ }

17 Critical Section Solution Attempt #3 (2 thread solution) Intially, flag[0] == flags[1] == false, turn entry (id) { flag [id] = true;/* I want to go in */ turn = 1 – id; while (flag[1-id] && turn == 1-id); /* proceed if other not trying */ } exit(id) { flag[id] = false; /* I am out*/ }

18 Satisfying the 4 properties Mutual exclusion –turn must be 0 or 1 => only one thread can be in CS Absence of deadlock –turn must be 0 or 1 => one thread will be allowed in Absence of unnecessary delay –only one thread trying to get into CS => flag[other] is false => will get in Bounded Waiting –spinning thread will not modify turn –thread trying to go back in will set turn equal to spinning thread

19 Critical Section Problem multiple threads solutions Bakery algorithm It’s a mess –Trying to prove it correct is a headache

20 Hardware Support Provide instruction that is: –Atomic –Fairly easy for hardware designer to implement Read/Modify/Write –Atomically read value from memory, modify it in some way, write it back to memory Use to develop simpler critical section solution for any number of threads.

21 Atomic Operation An operation that, once started, runs to completion Indivisible Ex: loads and stores –Meaning: if thread A stores “1” into variable x and thread B stores “2” into variable x about the same time, result is either “1” or “2”.

22 Test-and-Set Many machines have it bool TestAndSet(bool &target) { bool b = target; target = true; return b; } Executes atomically

23 CS solution with Test-and-Set Initially, s == false; entry () { bool spin; spin = TestAndSet(s); while (spin) spin = TS(s); } exit() { S = false; }

24 Basic Idea With Atomic Instructions Each thread has a local flag One variable shared by all threads Use the atomic instruction with flag, shared variable –on a change, all thread to go in –Other threads will not see this change When done with CS, set shared varible back to initial state.

25 Other Atomic Instructions The definition of the Swap instruction void Swap(bool &a, bool &b) { bool temp = a; a = b; b = temp; }

26 Problems with busy-waiting CS solution Complicated Inefficient –Consumes CPU cycles while spinning Priority inversion problem –Low priority thread in CS, high priority thread spinning can end up causing deadlock Solution: block when waiting for CS