L5process synchronizationa

Page 1

Chapter 6: Process Synchronization

ef: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

Prof.Dr.Hanafy Ismail


Module 6: Process Synchronization     

Background The Critical-Section Problem Semaphores Classic Problems of Synchronization Atomic Transactions

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.2

Prof. Dr. Hanafy Ismail


Background  

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 buffers. We can do so by having an integer count that keeps track of the number of full buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.3

Prof. Dr. Hanafy Ismail


Producer while (true) { /* produce an item and put in nextProduced */

while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.4

Prof. Dr. Hanafy Ismail


Consumer while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed

}

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.5

Prof. Dr. Hanafy Ismail


Race Condition 

 

Race Condition: A situation where several processes access and manipulate the same data concurrently and outcome of the execution depends on the particular order in which access take place. We need to ensure that only one process at a time can be manipulating the shared variable (e.g. variable count). To make such guarantee, we require that processes must be synchronized in some way. In the producer/consumer example:  Process1 (producer) include count++  Process2 (consumer) include count--

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.6

Prof. Dr. Hanafy Ismail


Race Condition 

count++ could be implemented as

register1 = count register1 = register1 + 1 count = register1 count-- could be implemented as

register2 = count register2 = register2 - 1 count = register2 register1 and register2 are CPU registers may be the same

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.7

Prof. Dr. Hanafy Ismail


Race Condition 

Consider this execution interleaving with “count = 5” initially: (The concurrent execution of count++ and count– may result that the value of the variable count is 4, 5, or 6. only the correct result is count = 5)) S0: producer execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4}

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.8

Prof. Dr. Hanafy Ismail


Critical-Section Problem  

 

Consider a system consisting of n processes (P0, P1 , …, Pn-1 ). Each process has a segment of code called a critical segment, in which the process may be changing common variables, updating a table, writing a file, and so on. No two processes are executing their critical sections at the same time. The general structure of a typical process is given in the next slide: 1. Critical Section - Section of code where shared data is accessed. 2. Entry Section - Code that requests permission to enter its critical section. 3. Exit Section - Code that is run after exiting the critical section 4. The critical section problem is to design a protocol that the processes can use to cooperate.

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.9

Prof. Dr. Hanafy Ismail


Structure of a Typical Process

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.10

Prof. Dr. Hanafy Ismail


Solution to Critical-Section Problem (requirements to be satisfied)

1. Mutual Exclusion - If process Pi 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 (from that are not executing in their remainder sections) that will enter the critical section next cannot be postponed indefinitely 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

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.11

Prof. Dr. Hanafy Ismail


Semaphore  

Synchronization tool that does not require busy waiting. It is Less complicated A semaphore S is an integer variable that is accessed only through two standard operations modify S: wait() and signal()  Originally called P() and V() Can only be accessed via two indivisible (atomic) operations 

wait (S) { // the following must be executed without interruption while S <= 0 ; // no-op S--; } signal (S) { S++; }

When one process modifies the semaphore value, no other process can simultaneously modify that semaphore value.

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.12

Prof. Dr. Hanafy Ismail


Semaphore as General Synchronization Tool  

Counting semaphore – integer value can range over an unrestricted domain Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement  Also known as mutex locks provide mutual exclusion.  Used to deal with critical-section problem for multiple processes.

 

Provides mutual exclusion; each process is organized as follows:  Semaphore S; // the n processes share S are initialized to 1  do { wait (S); // Critical Section signal (S); // remainder section } while (true);

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.13

Prof. Dr. Hanafy Ismail


Semaphore as General Synchronization Tool 

Counting semaphores (S) can be used to control access to a given resource consisting of a finite number of instances (n):  Initially S = n  A process performs wait() on S to use the resource; thereby S-  A process performs signal() on S to release the resource; thereby S++  When S=0 (all resources are used), a process wishes to use a resource block until S become > 0.

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.14

Prof. Dr. Hanafy Ismail


Semaphore Implementation  

Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section. The main disadvantage of the semaphore definition that it requires busy waiting . While a process in its critical section, any other process that tries to enter its critical section must loop continuously in the entry code. Note that applications may spend lots of time in critical sections and therefore this is not a good solution.

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.15

Prof. Dr. Hanafy Ismail


Semaphore Implementation with no Busy waiting 

With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items:  value (of type integer)  pointer to next record in the list Two operations:  block – when a process executes the wait() operation and finds the semaphore value is not positive, the process block itself. It places the process into the waiting queue associated with the semaphore.  wakeup – a process that is blocked is restarted by wakeup() operation, when some other process executes a signal() operation; which removes the process from the waiting queue and places it in the ready queue.

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.16

Prof. Dr. Hanafy Ismail


Semaphore Implementation with no Busy waiting (Cont.) 

Implementation of wait:

wait (S){ value--; if (value < 0) { add this process to waiting queue block(); } } Implementation of signal: Signal (S){ value++; if (value <= 0) { remove a process P from the waiting queue wakeup(P); } }

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.17

Prof. Dr. Hanafy Ismail


Deadlock and Starvation   

Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes The implementation of a semaphore with a waiting queue may result in a deadlock situation. The event() in question is the execution of a signal() operation. Consider a system has two processes P0 and P1 each accessing two semaphores S and Q , initialized to 1: P0 P1 wait (S); wait (Q); wait (Q); wait (S); . . it must wait until P1 executes signal (Q) .

it must wait until P0 executes signal (S)

. signal (S);

. signal (Q);

// cannot be executed, so P0 and P1 are blocked 

signal (Q); signal (S); Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.18

Prof. Dr. Hanafy Ismail


Classical Problems of Synchronization   

Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.19

Prof. Dr. Hanafy Ismail


Bounded-Buffer Problem  

 

Assume that the pool consists of N buffers, each can hold one item Semaphore mutex initialized to the value 1; it provides mutual exclusion for accesses to the buffer pool. Semaphore full initialized to the value 0, it counts the number of full buffers. Semaphore empty initialized to the value N; it counts the number of empty buffers.

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.20

Prof. Dr. Hanafy Ismail


Bounded Buffer Problem (Cont.) ď °

The structure of the producer process while (true) { // produce an item wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); }

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.21

Prof. Dr. Hanafy Ismail


Bounded Buffer Problem (Cont.) ď °

The structure of the consumer process while (true) { wait (full); wait (mutex); // remove an item from buffer signal (mutex); signal (empty); // consume the removed item }

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.22

Prof. Dr. Hanafy Ismail


Readers-Writers Problem A data set ( or database) is shared among a number of concurrent processes  Readers – processes that only read the data set; they do not perform any updates  Writers – processes that can both read and write.  Readers-Writers Problem  allow multiple readers to read at the same time.  Only one single writer can access the shared data at the same time ( writers have exclusive access to the shared database).  Variations of the problem: 1. No readers should wait for other readers to finish simply because a writer is waiting 2. If a writer is waiting to access the object, no new readers may start reading.  In both cases, starvation may result. In the first case writers may starve In the second case readers Prof. Dr. Hanafy Ismail 6.23 may starve. Ref: Operating System Concepts with Java – 7 Edition, Nov 15, 2006 

th


Readers-Writers Problem: Solution to the first problem  The reader processes share the following data structures:  Data set  Semaphore mutex initialized to 1.  it is used to ensure mutual exclusion when the variable readcount is updated.  Semaphore wrt initialized to 1.  It is common to both reader and writer processes.  It functions as a mutual exclusion semaphore for the writers.  It is also used by the first or last reader that enters or exits the critical section. It is not used by readers who enter or exit while other readers are in the critical section.  Integer readcount initialized to 0. it indicates how many processes are currently reading the object. Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.24

Prof. Dr. Hanafy Ismail


Readers-Writers Problem: Solution to the first problem 

Note that if a writer is in the critical section and n readers are waiting, then one reader is queued on wrt, and n – 1 readers are queued on mutex. Note that when a writer executes signal (wrt), the scheduler can resume the execution of either the waiting readers or a single waiting writer

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.25

Prof. Dr. Hanafy Ismail


Readers-Writers Problem (Cont.) ď °

The structure of a writer process while (true) { wait (wrt) ; //

writing is performed

signal (wrt) ; }

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.26

Prof. Dr. Hanafy Ismail


Readers-Writers Problem (Cont.) ď °

The structure of a reader process while (true) { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex) // reading is performed wait (mutex) ; readcount - - ; if (readcount == 0) signal (wrt) ; signal (mutex) ; }

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.27

Prof. Dr. Hanafy Ismail


Review Questions 1. 2. 3. 4. 5.

Define the meaning of race condition? Answer the question first and use an execution sequence to illustrate your answer. What is meant by semaphore? What is used for? Illustrate the implementation of it. What is meant by busy waiting? Describe the semaphore implementation with no busy waiting. Describe the semaphore implementation as a solution to the bounded buffer problem. Describe the semaphore implementation as a solution to the readers and writers problem

Ref: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

6.28

Prof. Dr. Hanafy Ismail


End of Chapter 6

ef: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006

Prof.Dr.Hanafy Ismail


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.