Inter Process Communication
Inter Process Communication
Communication
IPC between processes on a single computer system, IPC between
processes on different systems, using pipes, FIFOs, message queues,
shared memory.
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Interprocess Communication
Operating System Concepts – 10th Edition 3.2 Silberschatz, Galvin and Gagne ©2018
Interprocess Communication
Operating System Concepts – 10th Edition 3.3 Silberschatz, Galvin and Gagne ©2018
Interprocess Communication
Operating System Concepts – 10th Edition 3.4 Silberschatz, Galvin and Gagne ©2018
Inter-Process Communication Types
Inter-process communication can be:-
Unicast: When communication is from one process to a single other process.
e.g. Socket communication.
Multicast: When communication is from one process to a group of processes.
e.g. Publish/Subscribe Message model.
processes to exchange data and synchronize their execution. IPC can occur:
are essential.
Operating System Concepts – 10th Edition 3.5 Silberschatz, Galvin and Gagne ©2018
IPC b/w Process in Single System
Operating System Concepts – 10th Edition 3.6 Silberschatz, Galvin and Gagne ©2018
IPC b/w Process in Single System
Operating System Concepts – 10th Edition 3.8 Silberschatz, Galvin and Gagne ©2018
Shared Memory
Message Passing
Message queue
Pipe
Named Pipe(FIFO)
Operating System Concepts – 10th Edition 3.9 Silberschatz, Galvin and Gagne ©2018
Shared memory
Operating System Concepts – 10th Edition 3.10 Silberschatz, Galvin and Gagne ©2018
Message passing
Operating System Concepts – 10th Edition 3.11 Silberschatz, Galvin and Gagne ©2018
Message Passing
The message size can be of fixed size or of variable size.
if it is of fixed size, it is easy for OS designer but
complicated for programmer and if it is of variable size
then it is easy for programmer but complicated for the OS
designer.
•A standard message can have two parts:
header and body.
•The header part is used for storing
Message type,
destination id,
source id,
message length and
control information.
The control information contains information like what to
do if runs out of buffer space, sequence number, priority.
Operating System Concepts – 10th Edition 3.12 Silberschatz, Galvin and Gagne ©2018
Message queue
Message Queue is a method of Inter Process Communication in OS. It involves the use of a
shared queue, where processes can add messages and retrieve messages for
communication.
The queue acts as a buffer for the messages and provides a way for processes to exchange
data and coordinate their activities.
In Message Queue IPC, each message has a priority associated with it, and messages are
retrieved from the queue in order of their priority. This allows processes to prioritize the
delivery of important messages and ensures that critical messages are not blocked by less
important messages in the queue.
Message Queue IPC provides a flexible and scalable method of communication between
processes, as messages can be sent and received asynchronously, allowing processes to
continue executing while they wait for messages to arrive. Additionally, Message Queue IPC
supports the communication between processes running on different hosts, as the message
queue can be implemented as a network service.
Operating System Concepts – 10th Edition 3.13 Silberschatz, Galvin and Gagne ©2018
Message queue
Disadvantage of Message Queue IPC: It can introduce additional overhead,
as messages must be copied between address spaces, and the queue must be
managed by the operating system to ensure that it remains synchronized and
consistent across all processes. Despite these limitations, Message Queue IPC
remains a popular and widely used method of IPC in operating systems.
Features:
1. OS provides for inserting and deleting the message-pointers or messages.
2. Each queue for a message need initialization (creation) before using the functions in the
scheduler for the message queue
3. There may be a provision for multiple queues for the multiple types or destinations of messages. Each
queue may have an ID.
4. Each queue either has a user definable size (upper limit for number of bytes) ora fixed predefined size
assigned by the scheduler
5. When the queue becomes full, there may be a need for error handling and user codes for blocking the tasks
Operating System Concepts – 10th Edition 3.14 Silberschatz, Galvin and Gagne ©2018
Message queue
implementation
Operating System Concepts – 10th Edition 3.15 Silberschatz, Galvin and Gagne ©2018
Pipe
• Pipe is a device used for the inter process communication.
• A message-pipe is a device for inserting (writing) and deleting (reading) from that
between two given inter-connected tasks or two sets of tasks
• Conceptually, a pipe is a connection between two processes, such that the standard
output from one process becomes the standard input of the other process.
• Writing and reading from a pipe is like using a C command fwrite with a file name to
write into a named file, and C command fread with a file name to read into a named
file.
• Pipe is one-way communication only i.e we can use a pipe such that One process write
to the pipe, and the other process reads from the pipe. It opens a pipe, which is an area
of main memory that is treated as a“virtual file”.
• The pipe can be used by the creating process, as well as all its child processes, for
reading and writing. One process can write to this “virtual file” or pipe and another
related process can read from it.
• If a process tries to read before something is written to the pipe, the process is
suspended until something is written.
• The pipe system call finds the first two available positions in the process’s open file
table and allocates them for the read and write ends of the pipe.
Operating System Concepts – 10th Edition 3.16 Silberschatz, Galvin and Gagne ©2018
Pipes issues
Acts as a conduit allowing two processes to communicate
Issues:
• Is communication unidirectional or bidirectional?
• In the case of two-way communication, is it half or full-duplex?
• Must there exist a relationship (i.e., parent-child) between the communicating
processes?
• Can the pipes be used over a network?
• The communication between pipes are meant to be unidirectional.
• Pipes were restricted to one-way communication in general and need at least two pipes
for two-way communication.
• Pipes are meant for inter-related processes only.
• Pipes can’t be used for unrelated processes communication, say, if we want to execute
one process from one terminal and another process from another terminal, it is not
possible with pipes.
Operating System Concepts – 10th Edition 3.17 Silberschatz, Galvin and Gagne ©2018
Types of Pipes: Ordinary Pipes
Ordinary pipes – cannot be accessed from outside the process that
created it. Typically, a parent process creates a pipe and uses it to
communicate with a child process that it created.
Named pipes – can be accessed without a parent-child relationship.
Ordinary Pipes allow communication in standard producer-consumer style
Producer writes to one end (the write-end of the pipe)
Consumer reads from the other end (the read-end of the pipe)
Ordinary pipes are therefore unidirectional
Require parent-child relationship between communicating processes
Operating System Concepts – 10th Edition 3.18 Silberschatz, Galvin and Gagne ©2018
Named Pipes
Named Pipes are more powerful than ordinary pipes
Communication is bidirectional
No parent-child relationship is necessary between the communicating
processes
Several processes can use the named pipe for communication
Provided on both UNIX and Windows systems
Operating System Concepts – 10th Edition 3.19 Silberschatz, Galvin and Gagne ©2018
Pipe implementation
#include<unistd.h>
Operating System Concepts – 10th Edition 3.20 Silberschatz, Galvin and Gagne ©2018
FIFO(named pipe)
FIFO (First-In-First-Out) is a method of Inter Process Communication in OS. It involves the
use of a FIFO buffer, which acts as a queue for exchanging data between processes.
In the FIFO method, one process writes data to the FIFO buffer, and another process reads
the data from the buffer in the order in which it was written. The FIFO buffer acts as a
queue, with the oldest data being read first, and the newest data being added to the end
of the queue.
The main advantage of the FIFO method is that it provides a simple and straightforward
way for processes to communicate, as data is exchanged in a sequential manner, and
there is no need for processes to coordinate their access to the FIFO buffer. This makes
FIFO particularly useful for applications that need to exchange data in a sequential
manner, such as printing applications or pipeline-based processing systems.
However, the FIFO method can also introduce limitations, as it may result in slow
performance if the buffer becomes full and data must be written to the disk, or if the
buffer becomes empty and data must be read from the disk. Additionally, the FIFO method
is limited in terms of the amount of data that can be exchanged, as the buffer has a finite
size. Despite these limitations, the FIFO method remains a popular and widely used
method of IPC in operating systems.
Operating System Concepts – 10th Edition 3.21 Silberschatz, Galvin and Gagne ©2018
FIFO(named pipe)
It can use single named pipe that can be used for two-way communication
(communication between the server and the client, plus the client and the server at
the same time) as Named Pipe supports bi-directional communication.
Another name for named pipe is FIFO (First-In-First-Out). Let us see the system call
(mknod()) to create a named pipe, which is a kind of a special file.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
Operating System Concepts – 10th Edition 3.22 Silberschatz, Galvin and Gagne ©2018
Direct Communication
Operating System Concepts – 10th Edition 3.23 Silberschatz, Galvin and Gagne ©2018
Direct Communication
Operating System Concepts – 10th Edition 3.24 Silberschatz, Galvin and Gagne ©2018
Indirect Communication
Operating System Concepts – 10th Edition 3.25 Silberschatz, Galvin and Gagne ©2018
Indirect Communication
Operating System Concepts – 10th Edition 3.26 Silberschatz, Galvin and Gagne ©2018