University of Pennsylvania 9/19/00CSE 3801 Concurrent Processes CSE 380 Lecture Note 4 Insup Lee.

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

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.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Chapter 6: Process Synchronization
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.
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.
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
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 2.3 : Interprocess Communication
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems 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,
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Concurrency, Mutual Exclusion and Synchronization.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
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.
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.
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
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.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Agenda  Quick Review  Finish Introduction  Java Threads.
Homework-6 Questions : 2,10,15,22.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
CSE 120 Principles of Operating
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
G.Anuradha Reference: William Stallings
Chapter 5: Process Synchronization
143a discussion session week 3
Topic 6 (Textbook - Chapter 5) Process Synchronization
Lecture 19 Syed Mansoor Sarwar
Introduction to Cooperating Processes
Module 7a: Classic Synchronization
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
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
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

University of Pennsylvania 9/19/00CSE 3801 Concurrent Processes CSE 380 Lecture Note 4 Insup Lee

University of Pennsylvania 9/19/00CSE 3802 Concurrent Processes Implementing a multiprogramming OS requires programming to accommodate a number of simultaneously executing processes Multiple-process paradigm also useful for applications (e.g., parallel processing, background processing) Two kinds of parallelism in today's computer systems: –Pseudo-parallelism - one CPU supports multiple processes –True parallelism - processes run on multiple CPUs Two kinds of communication paradigms: –Shared-variable model –Message-passing model Most systems incorporate a mixture of the two.

University of Pennsylvania 9/19/00CSE 3803 Basic Issues in Concurrent Programming Programming concurrent processes is difficult and error-prone  bugs are often not reproducible since they are timing dependent (known as race condition) Cooperating concurrent processes need to be synchronized and/or coordinated to accomplish their task. Basic actions: they are the indivisible (or atomic) actions of a process Interleaving: other processes may execute an arbitrary number of actions between any two indivisible actions of one process

University of Pennsylvania 9/19/00CSE 3804 Example: Shared variable problem –Two processes are each reading characters typed at their respective terminals –Want to keep a running count of total number of characters typed on both terminals –A Shared variable V is introduced; each time a character is typed, a process uses the code: V := V + 1; to update the count. During testing it is observed that the count recorded in V is less than the actual number of characters typed. What happened?

University of Pennsylvania 9/19/00CSE 3805 Example (cont’d) The programmer failed to realize that the assignment was not executed as a single indivisible action, but rather as the following sequence of instructions: MOVE V, r0 INCR r0 MOVE r0, V

University of Pennsylvania 9/19/00CSE 3806 The Producer/Consumer Problem  from time to time, the producer places an item in the buffer  the consumer removes an item from the buffer  careful synchronization required  the consumer must wait if the buffer empty  the producer must wait if the buffer full  typical solution would involve a shared variable count (recall previous example)  also known as the Bounded Buffer problem  Example: in UNIX shell myfile.t | eqn | troff producer process consumer process P buffer C

University of Pennsylvania 9/19/00CSE 3807 Push and Pop example struct stacknode { int data; struct stacknode *nextptr; }; typedef struct stacknode STACKNODE; typedef STACKNODE *STACKNODEPTR; void push (STACKNODEPTR *topptr, int info) { STACKNODEPTR newptr; newptr = malloc (sizeof (STACKNODE)); newptr->date = info; /* Push 1 */ newptr->nextptr = *topptr; /* Push 2 */ *topptr = newptr; /* Push 3 */ }

University of Pennsylvania 9/19/00CSE 3808 Pop int pop (STACKNODEPTR *topptr) { STACKNODEPTR tempptr; int popvalue; tempptr = *topptr; /* Pop 1 */ popvalue = (*topptr)->data; /* Pop 2 */ *topptr = (*topptr)->nextptr; /* Pop 3 */ free(tempptr); return popvalue; }

University of Pennsylvania 9/19/00CSE 3809 The Mutual Exclusion Problem The previous examples are typical of kind of problem that arises in operating system programming. Occurs when more than one process has simultaneous access to shared data, whose values are supposed to obey some integrity constraint. Other examples: airline reservation system, bank transaction system Problem generally solved by making access to shared variables mutually exclusive: at most one process can access shared variables at a time The period of time when one process has exclusive access to the data is called a critical section. A process may assume integrity constraint (or data invariant) holds at beginning of critical section and must guarantee that it holds at end.

University of Pennsylvania 9/19/00CSE Definitions Deadlock. A situation in which each process in a cycle is waiting for resources held by the next process in the cycle. Livelock. A situation in which the algorithm that decides whether to block an activity fails to reach a decision and continues to use computational resources. Starvation. A situation in which a process continue to be denied a resource that it needs, even though the resource is being granted to other processes. Safety Property: bad things will not happen. (e.g., no deadlock) Liveness Property: good things will eventually happen. (e.g., no livelock, no starvation)

University of Pennsylvania 9/19/00CSE The Critical Section Problem Definition. A critical section is a sequence of activities (or statements) in a process during which a mutually excluded resource(s) (either hardware or software) must be accessed. The critical section problem is to ensure that two concurrent activities do not access shared data at the same time. A solution to the mutual exclusion problem must satisfy the following three requirements: 1Mutual Exclusion 2Progress 3Bounded waiting (no starvation)

University of Pennsylvania 9/19/00CSE Methods for Mutual Exclusion 1. disable interrupts (hardware solution) 2. switch variables (assume atomic read and write) 3. locks (hardware solution) 4. semaphores (software solution) 5. critical regions and conditional critical sections (language solution) 6. Hoare's monitor 7. Ada rendezvous

University of Pennsylvania 9/19/00CSE Disable Interrupts process A process B disable interrupts disable interrupts CS CS enable interrupts enable interrupts prevents scheduling during CS may hinder real-time response (use different priority levels) All CS's exclude each other even if they do not access the same variables This is sometimes necessary (to prevent further interrupts during interrupt handling)

University of Pennsylvania 9/19/00CSE Switch Variables switch := A process A process B repeat repeat while switch <> A do while switch <> B do skip; skip; /* CS */ /* CS */ switch := B switch := A 1. busy waiting 2. danger of long blockage since A and B strictly alternates 3. different CS's can be implemented using different switch variables

University of Pennsylvania 9/19/00CSE Shared Variable Solutions Two processes with shared variables /* initialization section */ Process P[i: 1..2] do forever /* entry code */ /* critical section */ /* exit code */ /* non-critical section */ end

University of Pennsylvania 9/19/00CSE st Attempt 1. turn := 1; 2. Process P[1] 3. do forever 4. while turn != 1 do no-op end 5. /* critical section */ 6. turn := 2; 7. /* non-critical section * 8. end

University of Pennsylvania 9/19/00CSE nd Attempt 1. flag[i: 1..2] := {false, false} 2. Process P[1] 3. do forever 4. while flag[2] do no-op end 5. flag[1] := true; 6. /* critical section */ 7. flag[1] := false; 8. /* non-critical section */ 9. end

University of Pennsylvania 9/19/00CSE rd Attempt 1. flag[i:1..2] := {false, false} 2. Process P[1] 3. do forever 4. flag[1] := true; 5. while flag[2] do no-op end 6. /* critical section */ 7. flag[1] := false; 8. /* non-critical section */ 9. end

University of Pennsylvania 9/19/00CSE th Attempt 1. flag[i:1..2] := {false, false} 2. Process P[1] 3. do forever 4. flag[1] := true; 5. while flag[2] do 6. flag[1] := false; 7. while flag[2] do no-op end 8. flag[1] := true; 9. end 10. /* critical section */ 11. flag[1] := false; 12. /* non-critical section */ 13. end

University of Pennsylvania 9/19/00CSE Dekker’s Algorithm 1. Flag[i:1..2] := {false, false} 2. turn := 1; 3. Process P[1] 4. do forever 5. flag[1] := true; 6. while flag[2] do 7. if turn = 2 then 8. flag[1] := false 9. while turn = 2 do no-op end 10. flag[1] := true; 11. end 12. end 13. /* critical section */ 14. turn := 2; 15. flag[1] := false; 16. /* non-critical section */ 17. end

University of Pennsylvania 9/19/00CSE Correctness of Dekker's Algorithm Case 1. mutual exclusion is preserved. Process 1 decides to enter CS only if flag[1] = true. Only process 1 can change flag[1] Process 1 inspects flag[2] only while flag[1] = true Thus, process 1 enters CS only if flag[1] = true and flag[2] = false. Similarly for process 2. Therefore,... Case 2. mutual blocking cannot occur. 1Only process 1 wants to enter CS i.e., flag[1]=true and flag[2]=false Then, process 1 enters CS regardless of turn

University of Pennsylvania 9/19/00CSE Correctness (cont.) 2Both processes 1 and 2 want to enter CS and turn=1 i.e., flag[1]=true and flag[2]=true and turn=1 Process 1 loops for flag[2] to set to false Process 2 changes flag[2] to false since turn=1 Process 2 then loops So, process 1 eventually enters CS 3Only process 2 wants to enter CS 4Both processes 1 and 2 want to enter CS and turn=2 Properties:  Complex and unclear  Mutual exclusion is preserved  Mutual blocking cannot occur  Can be extended for n processes  Starvation impossible  Busy waiting

University of Pennsylvania 9/19/00CSE Shared Variable Solutions - Discussion Code depicted is for process P1;symmetric for P2. Attempt 1:mutex O.K. (Why ?) but not liveness (What if P2 decides to no longer enter its critical section ?!) Attempt 2: mutex not guaranteed (P1 and P2 can both find flags false if they happen to run at same speed) Attempt 3: mutex, but both P1, P2 may find flags true Attempt 4: again, no progress possible Dekker's alg:mutex, liveness and bounded waiting! Note: unlike in attempt 1, "turn" is used only to break ties.