Process Synchronization-Operating Systems-Lecture Notes, Study notes of Operating Systems

Operating Systems is necessary course in Computer Science. Its about threading, process scheduling, deadlocks, memory management etc. This lecture includes: Process, Synchronization, Operating, System, Basic, Solution, Variable, Capacity, Buffer, Share, Consumer

Typology: Study notes

2011/2012

Uploaded on 08/06/2012

sarang
sarang ๐Ÿ‡ฎ๐Ÿ‡ณ

4.5

(47)

86 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
95
Operating Systems [CS-604] Lecture No. 18 and 19
Operating Systems
Lecture No. 18 and 19
Reading Material
๎˜‚๎˜ƒChapter 7 of the textbook
๎˜‚๎˜ƒLectures 18 and 19 on Virtual TV
Summary
๎˜‚๎˜ƒProcess Synchronization: the basic concept
๎˜‚๎˜ƒThe Critical Section Problem
๎˜‚๎˜ƒSolutions for the Critical Section Problem
๎˜‚๎˜ƒ2-Process Critical Section Problem solutions
Process Synchronization
Concurrent processes or threads often need access to shared data and shared resources. If
there is no controlled access to shared data, it is often possible to obtain an inconsistent
state of this data. Maintaining data consistency requires mechanisms to ensure the orderly
execution of cooperating processes, and hence various process synchronization methods
are used. In the producer-consumer problem that was discussed earlier, the version only
allows one item less than the buffer size to be stored, to provide a solution for the buffer
to use its entire capacity of N items is not simple. The producer and consumer share data
structure โ€˜bufferโ€™ and use other variables shown below:
#define BUFFER_SIZE 10
typedef struct
{
...
} item;
item buffer[BUFFER_SIZE];
int in=0;
int out=0;
The code for the producer process is:
while(1)
{
/*Produce an item in nextProduced*/
while(counter == BUFFER_SIZE); /*do nothing*/
buffer[in]=nextProduced;
in=(in+1)%BUFFER_SIZE;
counter++;
}
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Process Synchronization-Operating Systems-Lecture Notes and more Study notes Operating Systems in PDF only on Docsity!

Operating Systems [CS-604] Lecture No. 18 and 19

Operating Systems

Lecture No. 18 and 19

Reading Material

Chapter 7 of the textbook Lectures 18 and 19 on Virtual TV

Summary

Process Synchronization: the basic concept The Critical Section Problem Solutions for the Critical Section Problem 2-Process Critical Section Problem solutions

Process Synchronization

Concurrent processes or threads often need access to shared data and shared resources. If there is no controlled access to shared data, it is often possible to obtain an inconsistent state of this data. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes, and hence various process synchronization methods are used. In the producer-consumer problem that was discussed earlier, the version only allows one item less than the buffer size to be stored, to provide a solution for the buffer to use its entire capacity of N items is not simple. The producer and consumer share data structure โ€˜bufferโ€™ and use other variables shown below:

#define BUFFER_SIZE 10 typedef struct { ... } item; item buffer[BUFFER_SIZE]; int in=0; int out=0;

The code for the producer process is:

while(1) { /Produce an item in nextProduced/ while(counter == BUFFER_SIZE); /do nothing/ buffer[in]=nextProduced; in=(in+1)%BUFFER_SIZE; counter++; }

The code for the consumer process is:

while(1) { while(counter==0); //do nothing nextConsumed=buffer[out]; out=(out+1)%BUFFER_SIZE; counter--; /Consume the item in nextConsumed/ }

Both producer and consumer routines may not execute properly if executed concurrently. Suppose that the value of the counter is 5, and that both the producer and the consumer execute the statement counter++ and counter- - concurrently. Following the execution of these statements the value of the counter may be 4,5,or 6! The only correct result of these statements should be counter= =5, which is generated if the consumer and the producer execute separately. Suppose counter++ is implemented in machine code as the following instructions:

MOV R1, counter INC R MOV counter, R

whereas counter- - maybe implemented as:

MOV R2, counter DEC R MOV counter, R

If both the producer and consumer attempt to update the buffer concurrently, the machine language statements may get interleaved. Interleaving depends upon how the producer and consumer processes are scheduled. Assume counter is initially 5. One interleaving of statements is:

producer: MOV R1, counter (R1 = 5) INC R1 (R1 = 6) consumer: MOV R2, counter (R2 = 5) DEC R2 (R2 = 4) producer: MOV counter, R1 (counter = 6) consumer: MOV counter, R2 (counter = 4)

The value of count will be 4, where the correct result should be 5. The value of count could also be 6 if producer executes MOV counter, R1 at the end. The reason for this state is that we allowed both processes to manipulate the variable counter concurrently. A situation like this, where several processes access and manipulate the same data concurrently and the outcome of the manipulation depends on the particular order in which the access takes place, is called a race condition. To guard against such race conditions, we require synchronization of processes. Concurrent transactions in a bank or in an airline reservation (or travel agent) office are a couple of other examples that illustrates the critical section problem. We show

called the entry section. The remaining code is the remainder section. The critical section problem is to design a protocol that the processes can use so that their action will not depend on the order in which their execution is interleaved (possibly on many processors). There can be three kinds of solution to the critical section problem: Software based solutions Hardware based solutions Operating system based solution

We discuss the software solutions first. Regardless of the type of solution, the structure of the solution should be as follows. The Entry and Exist sections comprise solution for the problem.

do { Entry section

critical section

Exit section

remainder section

} while(1)

Solution to the Critical Section Problem

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 process can be executing in their critical section. 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 remainder section can participate in the decision on which will enter its critical section next, and this selection cannot be postponed indefinitely. 3. Bounded Waiting There exists a bound 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.

Assumptions While formulating a solution, we must keep the following assumptions in mind: Assume that each process executes at a nonzero speed No assumption can be made regarding the relative speeds of the N processes.

2-Process Solutions to the Critical Section Problem

In this section algorithms that are applicable to two processes will be discussed. The processes are P 0 and P 1. When presenting Pi , we use Pj to denote the other process. An assumption is that the basic machine language instructions such as load and store are executed atomically, that is an operation that completes in its entirety without interruption.

Algorithm 1

The first approach is to let the processes share a common integer variable turn initialized to 0 or 1. If turn = = i, then process P (^) i is allowed to execute in its critical section. The structure of the process Pi is as follows:

do { while(turn!=j);

critical section

turn=j;

remainder section } while(1)

This solution ensures mutual exclusion, that is only one process at a time can be in its critical section. However it does not satisfy the progress requirement, since it requires strict alternation of processes in the execution of the critical section. For example, if turn= =0 and P 1 is ready to enter its critical section, P 1 cannot do so even though P 0 may be in its remainder section. The bounded wait condition is satisfied though, because there is an alternation between the turns of the two processes.

Algorithm 2

In algorithm two, the variable turn is replaced with an array boolean flag[2]whose elements are initialized to false. If flag is true for a process that indicates that the process is ready to enter its critical section. The structure of process Pi is shown: