Cooperating Processes in Operating System:
Definition and Explanation:
There are two kinds of processes: cooperating and independent processes. A process is independent if it cannot affect or be affected by other processes. A process is said to be a cooperating process if it can affect or be affected by other processes in the system. A process that shares data with other processes is cooperating and a process that does not share data is independent.
Advantages of Cooperating Processes:
There are some advantages of cooperating processes:
information Sharing: Several users may which to share the same information e.g. a shared file. The O/S needs to provide a way of allowing concurrent access.
Computation Speedup: Some problems can be solved quicker by sub-dividing it into smaller tasks that can be executed in parallel on several processors.
Modularity: The solution of a problem is structured into parts with well-defined interfaces, and where the parts run in parallel.
Convenience: A user may be running multiple processes to achieve a single goal, or where a utility may invoke multiple components, which interconnect via a pipe structure that attaches the stdout of one stage to stdin of the next etc.
If we allow processes to execute concurrently and share data, then we must either provide some mechanisms to handle conflicts e.g. writing and reading the same piece of data. We must also be prepared to handle inconsistent or corrupted data.
Example:
Below is a very simple example of two cooperating processes. The problem is called the Producer Consumer problem and it uses two processes, the producer and the consumer.
Producer Process: It produces information that will be consumed by the consumer.
Consumer Process: It consumes information produced by the producer.
Both processes run concurrently. If the consumer has nothing to consume, it waits.
There are two versions of the producer. In version one, the producer can produce an infinite amount of items. This is called the Unbounded Buffer Producer Consumer Problem. In the other version, there is a fixed limit to the buffer size. When the buffer is full, the producer must wait until there is some space in the buffer before it can produce a new item.
Bounded Buffer - Shared Memory Solution:
Here is a shared memory solution for Bounded Buffer Producer Consumer problem. Both processes have some shared data that can be accessed and updated. The shared data is as follows:
// Shared data:
int n = 5, item, in, out;
// number of items in buffer is at most 5
int buffer[n];
// both the consumer and producer are currently looking at buffer element 0.
in = 0;
out = 0;
// Producer Process .
while (true)
{
while (in+1 % n == out)
{
// Do nothing
}
// produce a random item buffer[in] = nextProduction; in = in + 1 % n;
}
/ / Consumer process
while (true)
{
while (in = = out)
{
//Do nothing
. }
nextConsumed = buffer [out];
out = out + 1 % n;
}
The producer basically just checks to see if there is any space in which to put a newly produced item (outer while loop). If there is no space, then it waits, until there is some space. The consumer waits while the buffer is empty. If there is something/ it grabs the next item and consumes it. One drawback with this solution is that there is one element of the buffer that is wasted.