Cooperating Processes in Operating System
Overview
There may be several processes running in the system at the same time which can be either cooperating processes or independent processes.
An independent process cannot be impacted or affected by other processes.
Cooperating Process in OS is a process that can affect or get affected by any other process under execution.
What are Cooperating Processes in the Operating System?
Before learning about Cooperating processes in operating systems let's learn a bit about Operating Systems and Processes.
There are two types of software first is the application software and the other is the system software.
An application software performs tasks for the user while system software operates and controls the computer system and works as an interface to run the application software.
Operating system is system software that manages the resources of a computer system that is both hardware and software. It works as an interface between the user and the hardware so that the user can interact with the hardware. It provides a convenient environment in which a user can execute the programs. An operating system is a resource manager and it hides the internal working complexity of the hardware so that users can perform a specific task without any difficulty.
Some widely used operating systems are:
- Single-process operating system
- Batch-processing operating system
- Multiprogramming operating system
- Multitasking operating system
- Multi-processing operating system
- Distributed operating system
- Real-Time operating system
Now, Lets's talk about how the process is the most important part of an operating system:
A program under execution is known as the process. Every task in an operating system is converted into a process. A process has several states from its start to its termination. After a new process is generated the process gets admitted into a ready queue by job scheduler or long-term scheduler where every process is ready for execution. Then the processes inside the ready queue are admitted into the execution state by CPU scheduler or short-term scheduler. After execution, the process gets terminated.
Before termination, there may be multiple processes being executed in a system. There are two modes in which the processes can be executed. These two modes are:
- Serial mode
- Parallel mode
In serial mode, the process will be executed one after the other means the next process cannot be executed until the previous process gets terminated.
On the contrary in parallel mode, there may be several processes being executed at the same time quantum. In this way, there will be two types of processes which can be either cooperating processes or independent processes.
Cooperating Process in the operating system is a process that gets affected by other processes under execution or can affect any other process under execution. It shares data with other processes in the system by directly sharing a logical space which is both code and data or by sharing data through files or messages.
Whereas, an independent process in an operating system is one that does not affect or impact any other process of the system. It does not share any data with other processes.
Methods of Cooperating Process in OS
Cooperating processes in OS requires a communication method that will allow the processes to exchange data and information.
There are two methods by which the cooperating process in OS can communicate:
- Cooperation by Sharing
- Cooperation by Message Passing
Details about the methods are given below:
Cooperation by Sharing
The cooperation processes in OS can communicate with each other using the shared resource which includes data, memory, variables, files, etc.
Processes can then exchange the information by reading or writing data to the shared region. We can use a critical section that provides data integrity and avoids data inconsistency.
Let's see a diagram to understand more clearly the communication by shared region:
In the above diagram, We have two processes A and B which are communicating with each other through a shared region of memory. Process A will write the information in the shared region and then Process B will read the information from the shared memory and that's how the process of communication takes place between the cooperating processes by sharing.
Cooperation by Message Passing
The cooperating processes in OS can communicate with each other with the help of message passing. The production process will send the message and the consumer process will receive the same message.
There is no concept of shared memory instead the producer process will first send the message to the kernel and then the kernel sends that message to the consumer process.
A kernel is known as the heart and core of an operating system. The kernel interacts with the hardware to execute the processes given by the user space. It works as a bridge between the user space and hardware. Functions of the kernel include process management, file management, memory management, and I/O management.
If a consumer process waits for a message from another process to execute a particular task then this may cause a problem of deadlock and if the consumer process does not receive the message then this may cause a problem of process starvation.
Let's see a diagram to understand more clearly the cooperation by communication:
In the above diagram, the processes A and B are communicating with each other. Process A first sends the message to the kernel and then the kernel will interpret that this message is meant for Process B.
The kernel then sends the message to the process P2 and that's how the process of communication takes place between the cooperation processes by communication.
Need of Cooperating Processes in OS
One process will write to the file and the other process reads the file. Therefore, every process in the system could be affected by the other process.
The need for cooperating processes in OS can be divided into four types:
- Information Sharing
- Computation Speed
- Convenience
- Modularity
Let's learn about them in detail.
Information Sharing
As we know the cooperating process in OS shares data and information between other processes. There may be a possibility that different processes are accessing the same file. Processes can access the files concurrently which makes the execution of the process more efficient and faster.
Computation Speed
When a task is divided into several subtasks and starts executing them parallelly, this improves the computation speed of the execution and makes it faster. Computation speed can be achieved if a system has multiple CPUs and input/output devices.
When the tasks are assigned into several subtasks they become several different processes that need to communicate with each other. That's why we need cooperating processes in the operating system.
Convenience
A user may be performing several tasks at the same time which leads to the running of different processes concurrently. These processes need to cooperate so that every process can run smoothly without interrupting each other.
Modularity
We want to divide a system of complex tasks into several different modules and later they will be established together to achieve a goal. This will help in completing tasks with more efficiency as well as speed.
Advantages of Cooperating Process in Operating System
Let's discuss several advantages of cooperating processes in the operating system:
- With help of data and information sharing, the processes can be executed with much faster speed and efficiency as processes can access the same files concurrently.
- Modularity gives the advantage of breaking a complex task into several modules which are later put together to achieve the goal of faster execution of processes.
- Cooperating processes provide convenience as different processes running at the same time can cooperate without any interruption among them.
- The computation speed of the processes increases by dividing processes into different subprocesses and executing them parallelly at the same time.
Disadvantages of Cooperating Process in Operating System
Let's discuss the disadvantages of cooperating processes in the operating system:
- During the communication method of the cooperation processes, there may be a problem of deadlock if a consumer process waits for the message from the production process and the message does not receive at the consuming end.
- There may be a condition of process starvation where the next process will have to wait until the message is received by the previous consuming process.
- The cooperating processes in the operating system can damage the data which may occur due to modularity.
- During information sharing, it may also share any sensitive data of the user with the other process that the user might not want to share.
Example of Cooperating Process in Operating System
Let's take the example of the producer-consumer problem which is also known as a bounded buffer problemto understand cooperating processes in more detail:
Producer:
The process which generates the message that a consumer may consume is known as the producer.
Consumer:
The process which consumes the information generated by the produces.
A producer produces a piece of information and stores it in a buffer(critical section) and the consumer consumes that information.
For Example, A web server produces web pages that are consumed by the client. A compiler produces an assembly code that is consumed by the assembler.
Buffer memory can be of two types:
- Unbounded buffer: It is a kind of buffer that has no practical limit on the size buffer. The producer can produce new information but the consumer might have to wait for them.
- Bounded buffer: It is a kind of buffer that assumes a fixed size. Here, the consumer has to wait if the buffer is empty, while the producer has to wait if the buffer is full. But here in the process-consumer problem, we have used a bounded buffer.
Producer and consumer both processes execute simultaneously. The problem arises when a consumer wants to consume information when the buffer is empty or there is nothing to be consumed and a producer produces a piece of information when the buffer is full or the memory of the consumer is already full.
Producer Process:
Consumer Process:
In the above producer code and consumer code, we have the following variables:
- counter: counter is used to identify the size of the buffer which is used by the producer as well as consumer processes.
- in: in a variable is used by the producer to detect the next empty slot in the buffer region.
- out: The consumer uses the out variable to detect where the items are stored.
Shared Resources:
In the process-consumer problem we have used two types of shared resources:
- Buffer
- Counter
When both the producer process and consumer process do not execute on time then it may cause inconsistency in the process. The value of the counter variable will be incorrect if both the producer and consumer processes will be executed concurrently.
Producer and consumer processes both share the following variables:
The shared buffer region holds two logical pointers i.e, in and out, and are implemented in the form circular array. By default, the values of both the variables(in and out) are initialized to 0. As we discussed earlier, the out variable points to the first filled location in the buffer while the in variable points to the first free location in the buffer. The buffer will be empty if in=out. The buffer will be filled if in+1 mod n=out.
Conclusion
- Cooperating Process in the operating system is a process that gets affected by other processes under execution or can affect any other process under execution.
- An independent process in an operating system is one that does not affect or impact any other process of the system.
- There are two methods by which the cooperating process in OS can communicate:
- Cooperation by Sharing
- Processes can exchange information by reading or writing data to a shared region.
- Cooperation by Message Passing
- The producer process will send the message to the kernel and then the kernel sends that message to the consumer process.
- Cooperation by Sharing
- The need for cooperating processes in OS can be divided into four types:
- Information Sharing
- Computation Speed
- Convenience
- Modularity
- One of the main advantages of the cooperating process is that it helps in the sharing of data and information, which helps processes to be executed with much faster speed and efficiency.
- Deadlock may occur if a consumer process waits for the message from the production process and the message is not received at the consuming end.