Process Synchronization

8
Unit IV Process Synchronization 4.1 Background The processes may execute either independently or in cooperating way. In the later case, processes share data with other processes and hence gets afftected by their execution too. The reason for providing process cooperation: Information sharing: Since several users may be interested in the same piece of information (for instance, a shared file), we must provide an environment to allow concurrent access to such information. Computation speed: If we want a particular task to run faster, we must break it into subtasks, each of which will be executing in parallel with the others. Notice that such a speedup can be achieved only if the computer has multiple processing elements (such as CPUs or I/O channels). Modularity: We may want to construct the system in a modular fashion, dividing the system functions into separate processes or threads. Convenience: Even an individual user may work on many tasks at the same time. For instance, a user may be editing, printing, and compiling in parallel. Cooperating processes require an interprocess communication (IPC) mechanism for exchanging data and information. Following is the two fundamental models of IPC:- 1. Shared memory: A region of memory that is shared by cooperating process is created. Processes can exchange information by reading and writing data to the shared region. 2. Message Passing: It require system calls for every message exchange. In simple terms standard message is passed among the processes for communication to take place. Two primitives message can be thought of under this scheme: send (destination, &message) receive (source, &message)

Transcript of Process Synchronization

Page 1: Process Synchronization

Unit IVProcess Synchronization

4.1 BackgroundThe processes may execute either independently or in cooperating way. In the later case, processesshare data with other processes and hence gets afftected by their execution too.

The reason for providing process cooperation:• Information sharing: Since several users may be interested in the same piece of

information (for instance, a shared file), we must provide an environment to allowconcurrent access to such information.

• Computation speed: If we want a particular task to run faster, we must break it intosubtasks, each of which will be executing in parallel with the others. Notice that such aspeedup can be achieved only if the computer has multiple processing elements (such asCPUs or I/O channels).

• Modularity: We may want to construct the system in a modular fashion, dividing thesystem functions into separate processes or threads.

• Convenience: Even an individual user may work on many tasks at the same time. Forinstance, a user may be editing, printing, and compiling in parallel.

Cooperating processes require an interprocess communication (IPC) mechanism for exchangingdata and information. Following is the two fundamental models of IPC:-

1. Shared memory: A region of memory that is shared by cooperating process is created.Processes can exchange information by reading and writing data to the shared region.

2. Message Passing: It require system calls for every message exchange. In simple termsstandard message is passed among the processes for communication to take place.

Two primitives message can be thought of under this scheme:• send (destination, &message)• receive (source, &message)

Page 2: Process Synchronization

Comparision of shared memory and messgae passing schemes:

• Shared memory is faster once it is set up because no system calls are required and read/writeoperation occurs at normal speed.

• As message passing requires system calls for every message transfer, it is slower.• In comparision to shared memory, message passing is easier to implement because no new

place is created for message sharing.

Example Problem: Producer-Consumer Problem

The code for the producer process can be defined as follows:

An integer variable counter, initialized to 0. counter is incremented every time we add a new item tothe buffer and is decremented every time we remove one item from the buffer.

The code for the consumer process can be defined as follows:

Although both the producer and consumer routines shown above are correct separately, they maynot function correctly when executed concurrently. For example, suppose that the value of thevariable counter is currently 5 and that the producer and consumer processes execute the statements

Page 3: Process Synchronization

"counter++" and "counter--" concurrently. Following the execution of these two statements, thevalue of the variable counter may be 4, 5, or 6! The only correct result, though, is counter == 5,which is generated correctly if the producer and consumer execute separately.

We can show that the value of counter may be incorrect as follows. Note that the statement"counter++" may be implemented in machine language (on a typical machine) as:

register1 = counterregister1 = register1 + 1counter = register1

Similarly, the statement "counter--" is implemented as follows:register2 = counterregister2 = register2 - 1counter= register2

where register1 and register2 are local CPU registers.

The concurrent execution of "counter++" and "counter--" is equivalent to a sequential execution inwhich the lower-level statements presented previously are interleaved in some arbitrary order. Onesuch interleaving is:

Notice that we have arrived at the incorrect state "counter == 4", indicating that four buffers arefull, when, in fact, five buffers are full. If we reversed the order of the statements at T4 and T5 , wewould arrive at the incorrect state "counter== 6".

Race ConditionWe would arrive at this incorrect state because we allowed both processes to manipulate thevariable counter concurrently. A situation like this, where several processes access and manipulatethe same data concurrently and the outcome of the execution depends on the particular order inwhich the access takes place, is called a Race Condition.

To guard against the race condition above, we need to ensure that only one process at a time canmanipulate the variable counter. So, we require that the processes be synchronized in some way.

4.2 critical section problemConsider a system consisting of n processes {Po, P1 , ... , Pn-1}. Each process has a segment of code,in which they are changing common variables. This piece of code is called critical section.

The important feature of the system is that, when one process is executing in its critical section, noother process is to be allowed to execute in its critical section. That is, no two processes areexecuting in their critical sections at the same time. The critical-section problem gives a protocolthat the processes can use to cooperate.

The general structure of a typical process Pi is shown in following figure. The entry section and exit section are enclosed in boxes to highlight these important segments of code.

Page 4: Process Synchronization

Each process must request permission to enter its critical section. The section of code implementingthis request is the entry section. critical section may be followed by an exit section. The remaining code is the remainder section.

A solution to the critical-section problem must satisfy the following three requirements: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 some processes wish to enter

their critical sections, then only those processes that are not executing in their remaindersections can participate in deciding which will enter its critical section next, and thisselection carmot be postponed indefinitely.

3. Bounded waiting. There exists a bound, or limit, on the number of times that otherprocesses are allowed to enter their critical sections after a process has made a request toenter its critical section and before that request is granted.

Peterson's solutionA classic software-based solution to the critical-section problem is Peterson's solution. In moderncomputer architectures, there are no guarantees that Peterson's solution will work correctly.However the solution is presented because it provides a good algorithmic description of solving thecritical-section problem and illustrates some of the complexities involved in designing software thataddresses the requirements of mutual exclusion, progress, and bounded waiting.

Peterson's solution is restricted to two processes that alternate execution between their criticalsections and remainder sections. The processes are numbered P0 and P1. For convenience, if thecurrent process is referred as Pi, we use Pj to denote the other process; that is,

j = 1 - i.Peterson's solution requires the two processes to share two data items:

• int turn; The variable turn indicates whose turn it is to enter its critical section. That is, ifturn == i, then process Pi is allowed to execute in its critical section.

• boolean flag[2]; The flag array is used to indicate if a process is ready to enter its criticalsection. For example, if flag [i] is true, this value indicates that Pi is ready to enter its criticalsection.

To enter the critical section, process Pi first sets flag [i] to be true and then sets turn to the value j,thereby asserting that if the other process (i.e. j) wishes to enter the critical section, it can do so. Ifboth processes try to enter at the same time, turn will be set to both i and j at roughly the same time.

Page 5: Process Synchronization

But only one of these assignments will last; the other will occur but will be overwrittenimmediately.

The eventual value of turn determines which of the two processes is allowed to enter its criticalsection first.

Proof of the correctness of this solutionWe need to show that:

• Mutual exclusion is preserved.• The progress requirement is satisfied.• The bounded-waiting requirement is met.

To prove property 1, we note that each Pi enters its critical section only if either flag [j] == false orturn == i [(while flag[j] && turn == j)].

Also note that, if both processes can be executing in their critical sections at the same time, thenflag [0] ==flag [1] ==true. These two observations imply that P0 and P1 could not have successfullyexecuted their while statements at about the same time, since the value of turn can be either 0 or 1but can not be both. Hence, one of the processes -say, Pj -must have successfully executed the whilestatencent, whereas Pi had to execute at least one additional statement ("turn== j"). However, at thattime, flag [j] == true and turn == j, and this condition will persist as long as Pi is in its criticalsection; as a result, mutual exclusion is preserved.

To prove property 2, we note that a process Pi can be prevented from entering the critical sectiononly if it is stuck in the while loop with the condition flag [j] ==true and turn== j. If Pj is not readyto enter the critical section, then flag [j] ==false, and Pi can enter its critical section. If Pj has setflag [j] to true and is also executing in its while statement, then either turn == i or turn == j.

If turn == i, then Pi will enter the critical section. If turn== j, then Pj will enter the critical section.However, once Pi exits its critical section, it will reset flag [j] to false, allowing Pi to enter itscritical section.

To prove property 3, If Pj resets flag [j] to true, it must also set turn to i. Thus, since Pi does notchange the value of the variable turn while executing the while statement, Pi will enter the criticalsection (progress) after at most one entry by Pj (bounded waiting).

4.3 Synchronization HardwareSoftware-based solutions such as Peterson's are not guaranteed to work on modern computerarchitectures. Instead, a process must acquire a lock before entering a critical section and it releases

Page 6: Process Synchronization

the lock when it exits the critical section as illustrated in following Figure.

In Figure 6.8, we present another algorithm using the TestAndSet () instruction that satisfies all thecritical-section requirements. TestAndSet () is hardware supported atomic instruction i.e. it can't beinterrupted once it has started running.

The common data structures areboolean waiting[n];boolean lock;

These data structures are initialized to false.

Page 7: Process Synchronization

To prove that the mutual-exclusion requirement is met, we note that process Pi can enter itscritical section only if either waiting [i] == false or key == false. The value of key can become falseonly if the TestAndSet () is executed. The first process to execute the TestAndSet () will find key==false; all others must wait. The variable waiting [i] can become false only if another process leavesits critical section; only one waiting [i] is set to false, maintaining the mutual-exclusionrequirement.

To prove that the progress requirement is met, we note that the arguments presented for mutualexclusion also apply here, since a process exiting the critical section either sets lock to false or setswaiting[j] to false. Both allow a process that is waiting to enter its critical section to proceed.

To prove that the bounded-waiting requirement is met, we note that, when a process leaves itscritical section, it scans the array waiting in the cyclic ordering (i + 1, i + 2, ... , n - 1, 0, ... , i - 1). Itdesignates the first process in this ordering that is in the entry section (waiting[j] ==true) as the nextone to enter the critical section. Any process waiting to enter its critical section will thus do sowithin n - 1 turns.

4.4 SemaphoresThe hardware-based solution to the critical-section problem (TestAndSet) are complicated forapplication programmers to use. So, we use a synchronization tool called semaphore. A semaphoreS is an integer variable that, apart from initialization, is accessed only through two standard atomicoperations: wait () and signal ().

The wait () operation was originally termed P (from the Dutch proberen, "to test"); signal() wasoriginally called V (from verhogen, "to increment"). The definition of wait () is as follows:

The definition of signal() is as follows:

All modifications to the integer value of the semaphore in the wait () and signal() operations mustbe executed indivisibly. That is, when one process modifies the semaphore value, no other processcan simultaneously modify that same semaphore value. In addition, in the case of wait (S), thetesting of the integer value of S (S <= 0), as well as its possible modification (S--), must beexecuted without interruption.

Usage

There are two types of semaphores: Binary semaphore and Counting semaphore.

Binary Semaphore can have value 0 and 1 only, so sometimes they are also called as mutex locks,as they are locks that provide mutual exclusion.

We can use binary semaphores to deal with the critical-section problem for multiple processes. The

Page 8: Process Synchronization

n processes share a semaphore, mutex, initialized to 1. Each process Pi is organized as shown inFigure 6.9.

Counting semaphores can be used to control access to a given resource consisting of a finitenumber of instances. The semaphore is initialized to the number of resources available. Eachprocess that wishes to use a resource performs a wait() operation on the semaphore (therebydecrementing the count). When a process releases a resource, it performs a signal() operation(incrementing the count). When the count for the semaphore goes to 0, all resources are being used.After that, processes that wish to use a resource will block until the count becomes greater than 0.

We can also use semaphores to solve various synchronization problems. For example, consider twoconcurrently running processes: P1 with a statement S1 and P2 with a statement S2 . Suppose werequire that S2 be executed only after S1 has completed. We can implement this scheme readily byletting P1 and P2 share a common semaphore synch, initialized to 0, and by inserting the statements

S1 ;signal(synch) ;

in process P1 and the statementswait(synch); S2 ;

in process P2.

Because synch is initialized to 0, P2 will execute S2 only after P1 has invoked signal (synch), whichis after statement S1 has been executed.