Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }

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

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems Part III: Process Management (Process Synchronization)
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
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.
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.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
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.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Introduction to Synchronization CS-3013 C-term Introduction to Synchronization CS-3013 Operating Systems (Slides include materials from Operating.
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
CS 3013 & CS 502 Summer 2006 Week 1 Review and Class Discussion 1 Review (last week) Three important terms Abstraction Concurrency Resource Critical Sections.
Introduction to Synchronization CS-3013 A-term Introduction to Synchronization CS-3013 Operating Systems (Slides include materials from Modern Operating.
Introduction to Synchronization CS-3013 A-term Introduction to Synchronization CS-3013, Operating Systems A-term 2009 (Slides include materials from.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Concurrency & Synchronization CS-502 Fall Concurrency (continued) and Synchronization CS-502 Operating Systems Fall 2007 (Slides include materials.
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.
ConcurrencyCS-502 Fall Concurrency CS-502 Operating Systems Fall 2007 (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz,
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.
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.
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 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
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.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
CS 3013 & CS 502 Summer 2006 Concurrency & Processes1 CS3013 & CS502 Operating Systems.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Background on the need for Synchronization
Chapter 5: Process Synchronization
Peculiarity of Peterson’s Solution to Process Synchronization
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Introduction to Synchronization
Introduction to Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Concurrency & Processes
Process/Thread Synchronization (Part 2)
Presentation transcript:

Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }

Concurrency 2 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; } Upon completion of main, y == 1

Concurrency 3 CS502 Spring 2006 Program 1 int main(int argc, char **argv) { extern int y; y = y + 1; return 0; } Program 2 int main2(int argc, char **argv) { extern int y; y = y - 1; return 0; } Assuming both programs run “in parallel,” what are possible values of y on completion? Thought experiment (continued) static int y = 0;

Concurrency 4 CS502 Spring 2006 Fundamental Assumption On (nearly) all computers, reading and writing of machine words are each atomic Non-interruptible Appearing to take zero time Not in conflict with any other operation No other guarantees (except for extraordinary measures)

Concurrency 5 CS502 Spring 2006 Definitions Definition: race condition When two or more concurrent activities are trying to change the same variable inconsistently Random outcome Critical Region (aka critical section) One or more fragments of code that modify the same data, such that at most one activity at a time can be executing anywhere in that set of fragments.

Concurrency 6 CS502 Spring 2006 Synchronization – Critical Regions Tannenbaum, page 103

Concurrency 7 CS502 Spring 2006 Class Discussion How do we keep multiple computations from being in a critical region at the same time? Especially when number is > 2

Concurrency 8 CS502 Spring 2006 Possible ways to protect a critical section Without OS assistance — Locking variables & busy waiting Peterson’s solution (p. 105) Atomic read-modify-write – e.g. Test & Set With OS assistance on single processor Disable interrupts Mutex (Mutual Exclusion) Semphores Monitors Barrier synchronization What about multiple processors? Shared memory Non-shared memory

Concurrency 9 CS502 Spring 2006 Requirements for Controlling Access to a Critical Section –Symmetrical among n computations –No assumption about relative speeds –A stoppage outside critical section does not lead to potential blocking of others –No starvation — i.e. no combination of timings that would cause a computation to wait forever to enter its critical section

Concurrency 10 CS502 Spring 2006 Non-solution static int turn = 0; Computation 1 while (TRUE) { while (turn !=0) /*loop*/; critical_region(); turn = 1; noncritical_region(); }; Computation 2 while (TRUE) { while (turn !=1) /*loop*/; critical_region(); turn = 0; noncritical_region(); };

Concurrency 11 CS502 Spring 2006 Non-solution static int turn = 0; Computation 1 while (TRUE) { while (turn !=0) /*loop*/; critical_region(); turn = 1; noncritical_region(); }; Computation 2 while (TRUE) { while (turn !=1) /*loop*/; critical_region(); turn = 0; noncritical_region(); }; What is wrong with this approach?

Concurrency 12 CS502 Spring 2006 Peterson’s solution static int turn = 0; static int interested[2]; void enter_region(int process) { int other = 1 - process; interested[process] = TRUE; turn = process; while (turn == process && interested[other] == TRUE) /*loop*/; }; void leave_region(int process) { interested[process] = FALSE; };

Concurrency 13 CS502 Spring 2006 Exercise Can Peterson’s solution be generalized to more than 2 concurrent computations? This will be a brief class discussion at the beginning of class next week.

Concurrency 14 CS502 Spring 2006 Test & Set Instruction static int lock = 0; extern int TestAndSet(int &i); /* sets the value of i to 1 and returns the previous value of i. */ void enter_region(int process) { while (TestAndSet(lock) == 1) /* loop */ ; }; void leave_region(int process) { lock = 0; };

Concurrency 15 CS502 Spring 2006 Test & Set Instruction static int lock = 0; extern int TestAndSet(int &i); /* sets the value of i to 1 and returns the previous value of i. */ void enter_region(int process) { while (TestAndSet(lock) == 1) /* loop */ ; }; void leave_region(int process) { lock = 0; }; What is wrong with this solution?

Concurrency 16 CS502 Spring 2006 Semaphores In the OS, implement A data type called semaphore with integer values. An operation DecrOrBlock(semaphore &s) such that –if s > 0, atomically decrement s and proceed. –if s <= 0, block the computation until some other computation executes IncrAndUnblock(s). An operation IncrAndUnblock(semaphore &s):– –Atomically increment s; if one or more computations are blocked on s, allow precisely one of them to unblock and proceed.

Concurrency 17 CS502 Spring 2006 Critical Section Control with a Semaphore static semaphore mutex = 1; Computation 1 while (TRUE) { DecrAndBlock(mutex); critical_region(); IncrAndUnblock(mutex); noncritical_region1(); }; Computation 2 while (TRUE) { DecrAndBlock(mutex); critical_region(); IncrAndUnblock(mutex); noncritical_region2(); };

Concurrency 18 CS502 Spring 2006 Practical example – n-way buffering #define N 3 /* number of items in buffer*/ buffer B[N]; semaphore full = 0; semaphore empty = N; Producer void addItem(buffer &b) { static int i = 0; DecrOrBlock(empty); B[i] = b; i++; IncrAndUnblock(full); }; Consumer void removeItem(buffer &c){ static int j = 0; DecrOrBlock(full); c = B[j]; j++; IncrAndUnblock(empty); };

Concurrency 19 CS502 Spring 2006 Semaphores – History Introduced by E. Dijkstra in DecrOrBlock() was called P() Initial letter of a Dutch word meaning “test” IncrAndUnblock() was called V() Initial letter of a Dutch word meaning “increase”

Concurrency 20 CS502 Spring 2006 Abstractions The semaphore is an example of a new and powerful abstraction defined by the OS I.e., a data type and some operations that add a capability that was not in the underlying hardware or system. Once available, any program can use this abstraction to control critical sections and more powerful forms of synchronization among computations.

Concurrency 21 CS502 Spring 2006 Summary – Week 1 What is an OS? Concurrency Abstractions