Operating System - Process Synchronization

<<Previous

Next >>





In operating system, one of the co-operating processes can be affected by other processes during the execution because they can directly share the common resources. The concurrent access to the common data may lead to data inconsistency. We will explain this situation with producer-consumer problem.

Let us consider the bounded buffer, and the common variable counter initialized to 0. Every time the counter is incremented by adding a new item to the buffer and is decremented by removing item from the buffer.

Following is the code for producer process

while (true)
{

    while (counter == Buffer-Size)
     ; /* do nothing */
    buffer[in] = itemProduced;
    in = (in + 1) % Buffer-Size;
    counter++;
}
Following is the code for consumer process:

while (true)
{
	while (counter == 0)
	; /* do nothing */
	itemConsumed = buffer [out] ;
	out = (out + 1) % Buffer_Size;
	counter--;
}
 

Separately both the code are correct but if we run the code concurrently, they may give the incorrect result. Let us explain that. For example, the current value of the counter is 5 and that both processes are executed concurrently. They may give wrong result such that 6(counter++) and 4 (counter--) but the correct value of the counter is 5. This happens because both code can access and manipulate the common variable counter.

There is a situation where two or more processes access and manipulate the shared data and the final outcome depends on some order in which access takes place. This is called race condition. To avoid this race condition, we must ensure that only one process at a time can access and manipulate the common data. This requirement is achieved by the process synchronization.


<<Previous

Next >>









Operating System - Process Synchronization

<<Previous

Next >>





In operating system, one of the co-operating processes can be affected by other processes during the execution because they can directly share the common resources. The concurrent access to the common data may lead to data inconsistency. We will explain this situation with producer-consumer problem.

Let us consider the bounded buffer, and the common variable counter initialized to 0. Every time the counter is incremented by adding a new item to the buffer and is decremented by removing item from the buffer.

Following is the code for producer process

while (true)
{

    while (counter == Buffer-Size)
     ; /* do nothing */
    buffer[in] = itemProduced;
    in = (in + 1) % Buffer-Size;
    counter++;
}
Following is the code for consumer process:

while (true)
{
	while (counter == 0)
	; /* do nothing */
	itemConsumed = buffer [out] ;
	out = (out + 1) % Buffer_Size;
	counter--;
}
 

Separately both the code are correct but if we run the code concurrently, they may give the incorrect result. Let us explain that. For example, the current value of the counter is 5 and that both processes are executed concurrently. They may give wrong result such that 6(counter++) and 4 (counter--) but the correct value of the counter is 5. This happens because both code can access and manipulate the common variable counter.

There is a situation where two or more processes access and manipulate the shared data and the final outcome depends on some order in which access takes place. This is called race condition. To avoid this race condition, we must ensure that only one process at a time can access and manipulate the common data. This requirement is achieved by the process synchronization.


<<Previous

Next >>















Learn about Stack Data Structure

Learn about Heap Data Structure

Learn about Operating System

Learn AVL Tree

Learn Djikstra's Algorithm