1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
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.
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.
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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
China’s Software Industry August 2006 Instructor: Hengming Zou, Ph.D.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne  Operating System Concepts Background Shared-memory solution to bounded-buffer problem allows at most n – 1 items.
The Critical-Section Problem
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
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.
1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
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.
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
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,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
The Critical Section Problem
Concurrency, Mutual Exclusion and Synchronization.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
Principles of Operating Systems Lecture 6 and 7 - Process Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
3.1. Concurrency, Critical Sections, Semaphores
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.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
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.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
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.
Chapter 5: Process Synchronization
G.Anuradha Reference: William Stallings
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
The Critical-Section Problem
Introduction to Cooperating Processes
Lecture 20 Syed Mansoor Sarwar
Critical section problem
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Lecture 21 Syed Mansoor Sarwar
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
Chapter 5 Mutual Exclusion(互斥) and Synchronization(同步)
Presentation transcript:

1 Concurrent Processes

2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency can ease program complexity  concurrency can increase efficiency  How can the processes work together?  Flags  Files  Messages  Shared memory Concurrency x P0P1 1. Wait until x has a value 2. Use the value 3. Change the value to indicate used 1. Wait until x is able to be set 2. Produce a value 3. Set x to the value

3 Cooperating Processes  Concurrent processes (or threads) often need to share data (maintained either in shared memory or files) and resources  If there is no controlled access to shared data, some processes will obtain an inconsistent view of this data  The actions performed by concurrent processes may depend on the order in which their execution is interleaved  With no synchronization, results are typically not deterministic nor reproducible. Concurrency

4 Operating Systems must …  The OS must keep track of active processes.  The OS must allocate and deallocate resources.  Processor time  Memory  Files  I/O devices  The OS must protect the data and physical resources.  The results of a process must be independent of the speed of execution relative to the speed of other concurrent processes. Concurrency

5 An Example - Data Coherence static int a = 1, b = 1; void P1()void P2() {a = a + 1;{b = 2 * b; b = b + 1;a = 2 * a;} Process P1Process P2ab a = a b = 2 * b2 b = b a = 2 * a 4 Concurrency

6 Process Interaction Deadlock (consumable resource) Starvation Results of one process may depend on information obtained from others Timing of process may be affected Cooperation by communication Processes directly aware of each other (communication primitives available to them) Mutual exclusion Deadlock Starvation Data coherence Results of one process may depend on information obtained from others Timing of process may be affected Cooperation by sharing Processes indirectly aware of each other (e.g., shared object) Mutual exclusion Deadlock Starvation Results of one process independent of the action of others Timing of process may be affected CompetitionProcesses unaware of each other Potential Control ProblemsProcess InteractionRelationship Degree of Awareness Concurrency

7 Resource Competition  Mutual Exclusion  Critical resource – a single non-sharable resource.  Critical section – portion of the program that accesses a critical resource.  Deadlock  Each process owns a resource that the other is waiting for.  Two processes are waiting for communication from the other.  Starvation  A process is denied access to a resource, even though there is no deadlock situation. Concurrency

8 Solution to Critical-Section Problem 1. Mutual Exclusion. If process P i is executing in its critical section, then no other processes can be executing in their critical sections. 2.Progress. If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely.(no starvation) 3.Bounded Waiting. A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Assume that each process executes at a nonzero speed No assumption concerning relative speed of the n processes.

9 Initial Attempts to Solve Problem  Only 2 processes, P 0 and P 1  General structure of process P i (other process P j ) do { entry section critical section exit section reminder section } while (1);  Processes may share some common variables to synchronize their actions.

10 Algorithm 1  Shared variables:  int turn; initially turn = 0  turn == i  P i can enter its critical section  Process P i do { while (turn != i) ; critical section turn = j; reminder section } while (1);  Satisfies mutual exclusion, but not progress

11  First attempt:  int turn=0;  process 0 process 1 ..  do{ do{  while(turn!=0); while(turn!=1);  critical section critical section  turn=1; turn=0;  remainder section remainder section  }while(1) ; }while(1); 

12 Algorithm 2  Shared variables  boolean flag[2]; initially flag [0] = flag [1] = false.  flag [i] == true  P i ready to enter its critical section  Process P i do { flag[i] = true; while (flag[j]) ; critical section flag [i] = false; remainder section } while (1);  Satisfies mutual exclusion, but not progress requirement.

13  Second attempt:  int flag[2]={0,0};  process 0 process 1 .. do{ do{  while(flag[1]); while(flag[0]);  flag[0] = 1; flag[1] = 1;  critical section critical section  flag[0] = 0; flag[1] = 0;  remainder section remainder section  } while(1); } while(1);

14  int flag[2]={0,0};  process 0 process 1   do{ do{  flag[0] = 1; flag[1] = 1;  while(flag[1]); while(flag[0]);  critical section critical section  flag[0] = 0; flag[1] = 0;  remainder section remainder section  }while(1); }while(1); 

15 Algorithm 3  Combined shared variables of algorithms 1 and 2.  Process P i do { flag [i] = true; turn = j; while (flag [j] and turn == j) ; critical section flag [i] = false; remainder section } while (1);  Meets all three requirements; solves the critical- section problem for two processes.

16  Peteson algorithm:  int flag[2]={0,0}, turn;  process 0 process 1 ..  while(1) while(1)  { flag[0] = 1; { flag[1] = 1;  turn = 1; turn = 0;  while(flag[1]&& turn==1) ; while(flag[0]&& turn==0) ;  /*do nothing*/ /*do nothing*/  critical section critical section  flag[0] = 0; flag[1] = 0;  remainder section remainder section  } }

17 Bakery Algorithm  Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section.  If processes P i and P j receive the same number, if i < j, then P i is served first; else P j is served first.  The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5... Critical section for n processes

18 Bakery Algorithm  Notation <  lexicographical order (ticket #, process id #)  (a,b) <(c,d) if a < c or if a == c and b < d  max (a 0,…, a n-1 ) is a number, k, such that k  a i for i = 0,…, n – 1  Shared data boolean choosing[n]; int number[n]; Data structures are initialized to false and 0 respectively

19 Bakery Algorithm do { choosing[i] = true; number[i] = max(number[0], number[1], …, number[n – 1])+1; choosing[i] = false; for (j = 0; j < n; j++) {while (choosing[j]) ; while ((number[j] != 0) && (number[j],j]) < (number[i],i])) ; } critical section number[i] = 0; remainder section } while (1); Wait while someone else is choosing Could have overflow if we don’t reset all numbers periodically!

20 Software Solutions …  Turns  Bakery algorithm works  Proof is left as an exercise for the reader :-)  Proof by exhaustive cases  Bakery algorithm is a pain, complex, difficult to follow  Other software solutions  Dekker ’ s algorithm  Peterson ’ s algorithm  Can hardware help?  An atomic instruction support for exclusion Mutual Exclusion: Software

21 Hardware Solutions  Interrupt disabling  not all interrupts need to be disabled  might not work in multiprocessor environment  Special machine instructions  test and set instruction ( TSET )  exchange instruction ( XCHG )  advantages  works on any number of processors sharing memory  simple and supports multiple critical sections  disadvantages  busy waiting is employed  starvation and deadlock are possible Mutual Exclusion

22 Test-and-Set  Shared variable b (initialized to 0)  Only the first P i who sets b enters CS bool testset(int &i) { if (i == 0) { i = 1; return false; } else return true; } Mutual Exclusion

23 Mutual Exclusion with Test-and-Set  Shared data: boolean lock = false;  Process P i do { while (TestAndSet(lock)) ; critical section lock = false; remainder section }

24 Bounded-waiting mutual exclusion with TestAndSet do { waiting[i] = true; key =true; while(waiting[i]&&key) key = TestAndSet(lock); waiting[i] = false; critical section j=(i+1)%n; while((j!=i)&&!waiting[j]) j=(j+1)%n; If(j==i) lock=false; else waiting[j]=false; remainder section } while (1);

25 Synchronization Hardware  Atomically swap two variables. void Swap(boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; }

26 Mutual Exclusion with Swap  Shared data (initialized to false): boolean lock; boolean waiting[n];  Process P i do { key = true; while (key == true) Swap(lock,key); critical section lock = false; remainder section }