Home | | Operating Systems | Process Synchronization

Process Synchronization - | Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Chapter: Operating Systems - Process Scheduling and Synchronization

Process Synchronization

The only problem with the above code is that the maximum number of items which can be placed into the buffer is BUFFER_SIZE - 1.

PROCESS SYNCHRONIZATION

 

1. Background

 

Producer code

 

item nextProduced;

 

while( true ) {

/* Produce an item and store it in nextProduced */ nextProduced = makeNewItem( . . . );

 

/* Wait for space to become available */ while( ( ( in + 1 ) % BUFFER_SIZE ) == out )

; /* Do nothing */

 

/* And then store the item and repeat the loop. */ buffer[ in ] = nextProduced;

 

in = ( in + 1 ) % BUFFER_SIZE;

 

}

 

Consumer code

 

item nextConsumed;

 

while( true ) {

 

/* Wait for an item to become available */ while( in == out )

 

; /* Do nothing */

 

/* Get the next available item */ nextConsumed = buffer[ out ];

 

out = ( out + 1 ) % BUFFER_SIZE;

 

/* Consume the item in nextConsumed ( Do something with it ) */

 

}

 

 

 

 

 

 

 

 

ü The only problem with the above code is that the maximum number of items which can be placed into the buffer is BUFFER_SIZE - 1. One slot is unavailable because there always has to be a gap between the producer and the consumer.

ü We could try to overcome this deficiency by introducing a counter variable, as shown in the following code segments:


ü Unfortunately we have now introduced a new problem, because both the producer and the consumer are adjusting the value of the variable counter, which can lead to a condition known as a race condition. In this condition a piece of code may or may not work correctly, depending on which of two simultaneous processes executes first, and more importantly if one of the processes gets interrupted such that the other process runs between important steps of the first process. ( Bank balance example discussed in class. )

 

ü The particular problem above comes from the producer executing "counter++" at the same time the consumer is executing "counter--". If one process gets part way through making the update and then the other process butts in, the value of counter can get left in an incorrect state.

 

ü But, you might say, "Each of those are single instructions - How can they get interrupted halfway through?" The answer is that although they are single instructions in C++, they are actually three steps each at the hardware level:

 

(1)Fetch counter from memory into a register,

 

(2)increment or decrement the register, and

 

(3)Store the new value of counter back to memory. If the instructions from the two processes get interleaved, there could be serious problems, such as illustrated by the following:



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2021 BrainKart.com; All Rights Reserved. (BS) Developed by Therithal info, Chennai.