A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.

Slides:



Advertisements
Similar presentations
Synchronization and Deadlocks
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
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.
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
Cooperating Processes Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: 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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication in Client-Server.
Silberschatz, Galvin and Gagne  Operating System Concepts Cooperating Processes Independent process cannot affect or be affected by the execution.
Concurrency, Mutual Exclusion and Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program.
Processes: program + execution state
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
3.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Interprocess Communication Processes within a system may be.
Processes. Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization 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.
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 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-4 Process Communication Department of Computer Science and Software.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 10 Processes II Read.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems.
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 3: Processes.
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.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Termination Process executes last statement and asks the operating.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
CSC 360, Instructor: Kui Wu IPC. CSC 360, Instructor: Kui Wu Agenda 1.The need to communicate 2.Shared memory 3.Message passing.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Background on the need for Synchronization
Chapter 5: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Process Synchronization
Lecture 19 Syed Mansoor Sarwar
Introduction to Cooperating Processes
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Lecture 18 Syed Mansoor Sarwar
Chapter 6: Synchronization Tools
Presentation transcript:

A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes

2 A. Frank - P. Weisberg Cooperating Processes Introduction to Cooperating Processes Producer/Consumer Problem The Critical-Section Problem Synchronization Hardware Semaphores

3 A. Frank - P. Weisberg Introduction to Cooperating Processes Processes within a system may be independent or cooperating. Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected by other processes, including sharing data. Reasons for cooperating processes: –Information sharing –Computation speed-up –Modularity –Convenience

4 Cooperation Models

5 A. Frank - P. Weisberg Cooperation among Processes by Sharing Processes use and update shared data such as shared variables, memory, files, and databases. Writing must be mutually exclusive to prevent a race condition leading to inconsistent data views. Critical sections are used to provide this data integrity. A process requiring the critical section must not be delayed indefinitely; no deadlock or starvation.

6 A. Frank - P. Weisberg Cooperation among Processes by Communication Communication by messages provides a way to synchronize, or coordinate, the various activities. Possible to have deadlock – –each process waiting for a message from the other process. Possible to have starvation – –two processes sending a message to each other while another process waits for a message.

7 A. Frank - P. Weisberg Producer/Consumer (P/C) Problem (1) Paradigm for cooperating processes – Producer process produces information that is consumed by a Consumer process. –Example 1: a print program produces characters that are consumed by a printer. –Example 2: an assembler produces object modules that are consumed by a loader.

8 A. Frank - P. Weisberg Producer/Consumer (P/C) Problem (2) We need a buffer to hold items that are produced and later consumed: –unbounded-buffer places no practical limit on the size of the buffer. –bounded-buffer assumes that there is a fixed buffer size.

9 A. Frank - P. Weisberg Multiple Producers and Consumers

10 A. Frank - P. Weisberg Producer/Consumer (P/C) Dynamics A producer process produces information that is consumed by a consumer process. At any time, a producer activity may create some data. At any time, a consumer activity may want to accept some data. The data should be saved in a buffer until they are needed. If the buffer is finite, we want a producer to block if its new data would overflow the buffer. We also want a consumer to block if there are no data available when it wants them.

11 A. Frank - P. Weisberg Idea for Producer/Consumer Solution The bounded buffer is implemented as a circular array with 2 logical pointers: in and out. The variable in points to the next free position in the buffer. The variable out points to the first full position in the buffer.

12 A. Frank - P. Weisberg Bounded-Buffer – Shared-memory Solution Shared data #define BUFFER_SIZE 10 typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Suggested solution is correct, but can only use BUFFER_SIZE-1 elements.

13 A. Frank - P. Weisberg Bounded-Buffer – Producer Process item nextProduced; while (TRUE) { while (((in + 1) % BUFFER_SIZE) == out); /* do nothing – - no free slots */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; }

14 A. Frank - P. Weisberg Bounded-Buffer – Consumer Process item nextConsumed; while ( TRUE ) { while (in == out); /* do nothing – nothing to consume */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; }

15 A. Frank - P. Weisberg Problems with concurrent execution 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 action performed by concurrent processes will then depend on the order in which their execution is interleaved.

16 A. Frank - P. Weisberg Example of inconsistent view 3 variables: A, B, C which are shared by thread T1 and thread T2. T1 computes C = A+B. T2 transfers amount X from A to B –T2 must do: A = A-X and B = B+X (so that A+B is unchanged). But if T1 computes A+B after T2 has done A = A-X but before B = B+X. Then T1 will not obtain the correct result for C = A+B.

17 A. Frank - P. Weisberg Inconsistent View Example Process P1 and P2 are running this same procedure and have access to the same variable “a”. Processes can be interrupted anywhere. If P1 is first interrupted after user input and P2 executes entirely. Then the character echoed by P1 will be the one read by P2 !! static char a; void echo() { cin >> a; cout << a; }

18 A. Frank - P. Weisberg Data Consistency Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffer. We can do so by having an integer count that keeps track of the number of items in the buffer. Initially, the count is set to 0. It is incremented by the producer after it produces a new item and is decremented by the consumer after it consumes a item.

19 A. Frank - P. Weisberg Bounded-Buffer – Shared Counter (1) Shared data #define BUFFER_SIZE 10 typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; int count = 0;

20 A. Frank - P. Weisberg Bounded-Buffer – Producer Process item nextProduced; while (TRUE) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE); /* do nothing – no free slots */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }

21 A. Frank - P. Weisberg Bounded-Buffer – Consumer Process item nextConsumed; while (TRUE) { while (count == 0); /* do nothing – nothing to consume */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed */ }

22 A. Frank - P. Weisberg Bounded-Buffer – Shared Counter (2) The statements count++; count--; must be performed atomically. Atomic/Indivisible operation means an operation that completes in its entirety without interruption.

23 A. Frank - P. Weisberg Bounded-Buffer – Shared Counter (3) The statement “count++” could be implemented in machine language as: register1 = count register1 = register1 + 1 count = register1 The statement “count--” could be implemented as: register2 = count register2 = register2 – 1 count = register2

24 A. Frank - P. Weisberg Bounded-Buffer – Shared Counter (4) If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved. The interleaving depends upon how the producer and consumer processes are scheduled.

25 A. Frank - P. Weisberg Bounded-Buffer – Shared Counter (5) Consider this execution interleaving with “count = 5” initially: producer: register1 = count (register1 = 5) producer: register1 = register1 + 1 (register1 = 6) consumer: register2 = count (register2 = 5) consumer: register2 = register2 – 1 (register2 = 4) producer: count = register1 (count = 6) consumer: count = register2 (count = 4) The value of count may be either 4 or 6, whereas the correct result should be 5.

26 A. Frank - P. Weisberg This is the Race Condition Race condition: The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must coordinate or be synchronized.

27 A. Frank - P. Weisberg Race condition updating a variable (1) shared double balance; Code for p1:... balance += amount;... Code for p1:... Load R1, balance Load R2, amount Add R1, R2 Store R1, balance... Code for p2:... balance += amount;... Code for p2:... Load R1, balance Load R2, amount Add R1, R2 Store R1, balance...

28 A. Frank - P. Weisberg Race condition updating a variable (2)

29 A. Frank - P. Weisberg Critical section to prevent a race condition Multiprogramming allows logical parallelism, uses devices efficiently but we lose correctness when there is a race condition. So we forbid logical parallelism inside critical section so we lose some parallelism but we regain correctness.