Cooperating Processes in Operating System

In this tutorial, we will learn about the cooperating processes in operating system. By Prerana Jain Last updated : May 07, 2023

What are cooperating processes in operating system?

In the computer system, there are many processes which may be either independent processes or cooperating processes that run in the operating system. A process is said to be independent when it cannot affect or be affected by any other processes that are running the system. It is clear that any process which does not share any data (temporary or persistent) with any another process then the process independent. On the other hand, a cooperating process is one which can affect or affected by any another process that is running on the computer. the cooperating process is one which shares data with another process.

What are the reasons for needing cooperating processes?

There are several reasons for providing an environment that allows process cooperation:

1. Information Sharing

In the information sharing at the same time, many users may want the same piece of information(for instance, a shared file) and we try to provide that environment in which the users are allowed to concurrent access to these types of resources.

2. Computation Speedup

When we want a task that our process run faster so we break it into a subtask, and each subtask will be executing in parallel with another one. It is noticed that the speedup can be achieved only if the computer has multiple processing elements (such as CPUs or I/O channels).

3. Modularity

In the modularity, we are trying to construct the system in such a modular fashion, in which the system dividing its functions into separate processes.

4. Convenience

An individual user may have many tasks to perform at the same time and the user is able to do his work like editing, printing and compiling.

A cooperating process is one that can affect or be affected by other process executing in the system cooperating process an:

  1. Directly share a logical address data space (i.e. code & data) - This may result in data inconsistency. It is implemented on threads.
  2. Share data only through files/ messages - So we will deal with various to order....orderly execution of cooperating process so that data consistency is maintained.

Example: producer-consumer problem

There is a producer and a consumer, producer produces on the item and places it into buffer whereas consumer consumes that item. For example, a print program produces characters that are consumed by the printer driver. A compiler may produce assembly code, which is consumed by an assembler. The assembler, in turn, may produce objects modules which are consumed by the loader.

Producer process

while(true)
{  
    produce an item &
    while(counter = = buffer-size);
    buffer[int] = next produced;
    in = (in+1) % buffer- size;
    counter ++;
}

Consumer process

while(true)
{ 
    while (counter = = 0);
    next consumed = buffer[out];
    out= (out+1) % buffer size;
    counter--;
}

Here,

  • in variable is used by producer t identify the next empty slot in the buffer.
  • out variable is used by the consumer to identify where it has to the consumer the item.
  • counter is used by producer and consumer to identify the number of filled slots in the buffer.

Shared Resources

  1. buffer
  2. counter

When producer and consumer are not executed can current then inconsistency arises. Here the value of a counter that is used by both producer and consumer will be wrong if both are executed concurrently without any control. The producer and consumer processes share the following variables:

var n;
type item = .....;
var Buffer : array [0,n-1] of item;
In, out:0..n-1;

With the variables in and out initialized to the value 0. In The shared buffer there are two logical pointers; in and out that is implemented in the form of a circular array. The in variables points to the next free position in the buffer and out variable points to the first full position in the buffer. When, in = out the buffer is empty and when in+1 mod n = out the buffer is full.





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.