Inter Process Communication (IPC)
What is Inter Process Communication in OS?
A system can have two types of processes i.e. independent or cooperating. Cooperating processes affect each other and
may share data and information among themselves.
Interprocess Communication or IPC provides a mechanism to exachange data and information across multiple processes,
which might be on single or multiple computers connected by a network. This is essential for many tasks, such as:
• Sharing data
• Coordinating activities
• Managing resources
• Achieving modularity
Synchronization in Inter Process Communication
Synchronization in Inter Process Communication (IPC) is the process of ensuring that multiple processes are coordinated
and do not interfere with each other. This is important because processes can share data and resources, and if they are
not synchronized, they can overwrite each other's data or cause other problems.
There are a number of different mechanisms that can be used to synchronize processes, including:
• Mutual exclusion: This is a mechanism that ensures that only one process can access a shared resource at a
time. This is typically implemented using a lock, which is a data structure that indicates whether a resource is
available.
• Condition variables: These are variables that can be used to wait for a certain condition to be met. For example,
a process could wait for a shared resource to become available before using it.
• Barriers: These are synchronization points that all processes must reach before they can proceed. This can be
used to ensure that all processes have completed a certain task before moving on to the next task.
• Semaphores: These are variables that can be used to count the number of times a shared resource is being
used. This can be used to prevent a resource from being used more than a certain number of times at the same
time.
Here are some examples of how synchronization is used in IPC:
• In a database, multiple processes may need to access the same data. Synchronization is used to ensure that only
one process can write to the data at a time, and that other processes do not read the data while it is being
written.
• In a web server, multiple processes may need to handle requests from clients. Synchronization is used to ensure
that only one process handles a request at a time, and that other processes do not interfere with the request.
• In a distributed system, multiple processes may need to communicate with each other. Synchronization is used
to ensure that messages are sent and received in the correct order, and that processes do not take actions based
on outdated information.
The two fundamental models of Inter Process Communication are:
• Shared Memory
• Message Passing
Figure 1 below shows a basic structure of communication between processes via the shared memory method and via
the message passing method.
An operating system can implement both methods of communication. First, we will discuss the shared memory methods
of communication and then message passing. Communication between processes using shared memory requires
processes to share some variable, and it completely depends on how the programmer will implement it. One way of
communication using shared memory can be imagined like this: Suppose process1 and process2 are executing
simultaneously, and they share some resources or use some information from another process. Process1 generates
information about certain computations or resources being used and keeps it as a record in shared memory. When
process2 needs to use the shared
information, it will check in the record stored
in shared memory and take note of the
information generated by process1 and act
accordingly. Processes can use shared
memory for extracting information as a
record from another process as well as for
delivering any specific information to other
processes.
Figure 1 below shows a basic structure of
communication between processes via the
shared memory method and via the message
passing method.
Shared Memory
• Shared memory is a region of memory that is accessible to multiple processes. This allows processes to
communicate with each other by reading and writing data from the shared memory region.
• Shared memory is a fast and efficient way for processes to communicate, but it can be difficult to use if the
processes are not carefully synchronized.
• There are two main types of shared memory:
o Anonymous shared memory: Anonymous shared memory is not associated with any file or other
system object. It is created by the operating system and is only accessible to the processes that created
it.
o Mapped shared memory: Mapped shared memory is associated with a file or other system object. It is
created by mapping a file into the address space of one or more processes.
Multiple processes can access a common shared memory. Multiple processes communicate by shared memory, where
one process makes changes at a time and then others view the change. Shared memory does not use kernel.
Message Passing
• Message passing is a method of Inter Process Communication in OS. It involves the exchange of messages
between processes, where each process sends and receives messages to coordinate its activities and exchange
data with other processes.
• Processes can communicate without any shared variables, therefore it can be used in a distributed environment
on a network.
• In message passing, each process has a unique identifier, known as a process ID, and messages are sent from
one process to another using this identifier. When a process sends a message, it specifies the recipient process
ID and the contents of the message, and the operating system is responsible for delivering the message to the
recipient process. The recipient process can then retrieve the contents of the message and respond, if necessary.
• Message passing in shared memory has a number of advantages over other IPC mechanisms. First, it is very fast,
as messages are simply copied from one process's address space to another. Second, it is very flexible, as any
type of data can be shared between processes. Third, it is relatively easy to implement, as it does not require
any special support from the operating system.
• However, message passing in shared memory also has some disadvantages. First, it can be difficult to ensure that
messages are delivered in the correct order. Second, it can be difficult to manage the size of the message queue.
Third, it can be difficult to port to other platforms, as the implementation of shared memory can vary from one
operating system to another.
Q. What is Inter-Process Communication (IPC)?
A. IPC is a mechanism that allows processes in an operating system to communicate, share data, and synchronize their
activities.
Q. Why is IPC necessary in operating systems?
A. IPC is necessary to enable processes to cooperate, coordinate, and exchange information, which is vital for modern
computing systems and multitasking environments.
Q. What is the difference between IPC and threads?
A. IPC involves communication between separate processes, while threads are lightweight sub-processes within the
same process that share the same memory space. IPC is used to communicate between separate processes, and threads
are used to parallelize within a process.
Q. What are some common scenarios where IPC is used?
A. IPC is used in scenarios such as inter-thread communication, client-server applications, distributed systems, parallel
processing, and multi-core processors.
Q. What are the advantages of using synchronization methods in IPC?
A. Synchronization methods in IPC ensure data consistency, prevent race conditions, and improve performance.
Q. What are the common IPC mechanisms in operating systems?
A. The common IPC mechanisms in operating systems are pipes, message queues, shared memory, direct and indirect
communication.
Q. What are some common synchronization methods used in IPC?
A. Common synchronization methods include mutexes, semaphores, condition variables, monitors, and message
passing, each offering specific benefits for coordinating processes.
Q. What are the challenges in IPC, and how can they be addressed?
A. Challenges in IPC include synchronization, race conditions, and deadlock. These challenges can be addressed through
proper design, synchronization mechanisms, and error handling.