Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

UNIT 3 - Inter-Process Communication

IPC –
Inter-Process Communication (IPC) in distributed systems refers to the mechanism through
which processes running on different computers or within the same computer communicate
and synchronize their actions. IPC allows processes to exchange data, share resources, and
coordinate activities to achieve a common goal.
In the context of distributed systems, IPC is crucial for enabling collaboration and interaction
between distributed components. This communication can occur over a network, enabling
processes to communicate even if they are running on different physical machines.
Characteristics of IPC -
1. Communication Channels:
 IPC provides various communication channels for processes to exchange data
and messages, such as shared memory, message passing, sockets, and remote
procedure calls (RPC).
 These channels can be uni-directional or bi-directional, synchronous or
asynchronous, depending on the requirements of the application.
2. Abstraction:
 IPC abstracts the underlying complexities of communication mechanisms,
providing a standardized interface for processes to interact.
 This abstraction allows developers to focus on application logic rather than
low-level communication details.
3. Concurrency and Parallelism:
 IPC supports concurrent and parallel execution of processes, allowing multiple
processes to communicate simultaneously.
 Processes can execute independently or cooperate with each other to
accomplish tasks efficiently.
4. Synchronization:
 IPC mechanisms include synchronization primitives like locks, semaphores,
and barriers to coordinate the execution of concurrent processes.
 Synchronization ensures that processes access shared resources in a mutually
exclusive and orderly manner, preventing data corruption and race conditions.
5. Data Transfer:
 IPC facilitates the transfer of data between processes, allowing them to share
information and work collaboratively.
 Data can be transferred in the form of messages, files, or shared memory
segments, depending on the communication channel used.
6. Error Handling:
 IPC mechanisms include error detection and handling capabilities to deal with
communication failures, network errors, or data corruption.
 Error handling ensures the reliability of communication between processes
and helps maintain data integrity.
7. Scalability:
 IPC supports scalable communication between processes, allowing systems to
handle increasing loads and growing numbers of users.
 Scalability is achieved through efficient resource management, optimized
communication protocols, and distributed architectures.
8. Security:
 IPC mechanisms include security features to protect communication channels
from unauthorized access, data tampering, and eavesdropping.
 Encryption, authentication, and access control mechanisms are employed to
secure IPC in distributed systems, ensuring confidentiality, integrity, and
availability of data.
9. Platform Independence:
 IPC mechanisms are often designed to be platform-independent, allowing
processes running on different operating systems or hardware architectures to
communicate seamlessly.
 This ensures interoperability across diverse computing environments and
facilitates the development of distributed applications.

Approaches of IPC –
1.Pipes –
 Pipes are a simple form of shared memory that allows two processes to communicate
with each other.
 It is a half duplex method (or one way communication) used for IPC between two
related processes .
 One process writes data to the pipe, and the other process reads data from the pipe.
 Pipes can be either named or anonymous, depending on whether they have a unique
name or not.
 Named pipes are a type of pipe that has a unique name, and can be accessed by
multiple processes. Named pipes can be used for communication between processes
running on the same host or between processes running on different hosts over a
network.
 Anonymous pipes, on the other hand, are pipes that are created for communication
between a parent process and its child process. Anonymous pipes are typically used
for one-way communication between processes, as they do not have a unique name
and can only be accessed by the processes that created them.

2.Shared Memory –
Shared memory allows multiple processes to access the same portion of memory. Each
process can read from and write to the shared memory region.
 Provides fast communication as processes directly access shared memory.
 Requires synchronization mechanisms (e.g., semaphores, mutexes) to manage access
to shared memory and prevent data corruption.
 Well-suited for scenarios where processes need to exchange large amounts of data
frequently.
 Can be more efficient than other IPC methods due to its low overhead.

3.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.
 Messages can be sent synchronously or asynchronously.
 Synchronous message passing involves blocking until the message is sent or received.
 Asynchronous message passing allows processes to continue execution while
messages are being sent or received.
 Requires serialization and deserialization of data.
 Can be implemented using sockets, pipes, or message queues.
 Message passing is used in IPC for a variety of purposes, such as exchanging data
between processes, coordinating the activities of multiple processes, and
implementing complex communication protocols between processes.

4. 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.

5.Direct Communication
 Direct communication is a method of Inter Process Communication in OS. It involves
the direct exchange of data between processes, without the use of intermediate
communication mechanisms such as message passing, message queues, or shared
memory.
 In direct communication, the sender process must know the identifier of the receiver
process in order to send a message to it.
 However, direct communication also has some limitations, as it can lead to tight
coupling between processes, and it can make it more difficult to change the
communication mechanism in the future, as direct communication is hardcoded into
the processes themselves.
6. Indirect Communication
 Indirect communication in IPC is a method of communication in which processes do
not explicitly name the sender or receiver of the communication. Instead, processes
communicate through a shared medium such as a message queue or mailbox.
 Pairs of communicating processes have shared mailbox.
 Link (uni-directional or bi-directional) is established between pairs of processes.
 Sender process puts message in the port or mailbox of reciever process and reciever
process takes out (or deletes) the data from mailbox.
 The sender and receiver processes do not need to know each other's identifiers in
order to communicate with each other.
 The main advantage of indirect communication is that it provides a more flexible and
scalable way for processes to communicate, as processes do not need to have direct
access to each other’s data.
 However, indirect communication can also introduce additional overhead, as data
must be copied between address spaces, and the communication mechanism must be
managed by the operating system to ensure that it remains synchronized and
consistent across all processes.

API for Internet Protocol –


Mainly there are JAVA API’s for UDP Datagram Communication and TCP Stream
Communication.
UDP (User Datagram Protocol):
Connectionless Protocol: UDP is connectionless, meaning there is no handshaking between
the sender and receiver before sending data. Each UDP packet, also known as a datagram, is
independent of the others.
Unreliable: UDP does not guarantee delivery of packets. There is no acknowledgment
mechanism, and if a packet is lost during transmission, it won't be retransmitted. This makes
UDP faster but less reliable compared to TCP.
No Flow Control: There is no flow control mechanism in UDP. This means that if the
receiver is slower than the sender, packets may be dropped.
Low Overhead: UDP has lower overhead compared to TCP because it lacks features like
handshaking, acknowledgment, and retransmission.
Suitable for Real-time Applications: UDP is often used in real-time applications like video
streaming, online gaming, DNS (Domain Name System), where low latency and speed are
more important than reliability.
Datagram Communication: UDP sends data in the form of datagrams, which are self-
contained units of data with a maximum size of 65,507 bytes (including header).
Example: Voice over IP (VoIP), streaming media, online gaming.

Java API for UDP datagrams –


The Java API provides datagram communication by means of two classes: DatagramPacket
and DatagramSocket.
DatagramPacket: This class provides a constructor that makes an instance out of an array of
bytes comprising a message, the length of the message and the Internet address and local port
number of the destination socket, as follows

An instance of DatagramPacket may be transmitted between processes when one process


sends it and another receives it. This class provides another constructor for use when
receiving a message. Its arguments specify an array of bytes in which to receive the message
and the length of the array. A received message is put in the DatagramPacket together with its
length and the Internet address and port of the sending socket. The message can be retrieved
from the DatagramPacket by means of the method getData. The methods getPort and
getAddress access the port and Internet address.
DatagramSocket: This class supports sockets for sending and receiving UDP datagrams. It
provides a constructor that takes a port number as its argument, for use by processes that need
to use a particular port. It also provides a no-argument constructor that allows the system to
choose a free local port. These constructors can throw a SocketException if the chosen port is
already in use or if a reserved port (a number below 1024) is specified when running over
UNIX. The class DatagramSocket provides methods that include the following:
send and receive: These methods are for transmitting datagrams between a pair of sockets.
The argument of send is an instance of DatagramPacket containing a message and its
destination. The argument of receive is an empty DatagramPacket in which to put the
message, its length and its origin. The methods send and receive can throw IOExceptions.
setSoTimeout: This method allows a timeout to be set. With a timeout set, the receive
method will block for the time specified and then throw an InterruptedIOException. connect:
This method is used for connecting to a particular remote port and Internet address, in which
case the socket is only able to send messages to and receive messages from that address.
Program -
UPD Client –
UDP Server –

TCP (Transmission Control Protocol):


 Connection-Oriented Protocol: TCP establishes a connection between the sender
and receiver before data transmission through a process called the "three-way
handshake". This ensures reliable data delivery.
 Reliable: TCP ensures reliable and ordered delivery of packets. It uses
acknowledgment, retransmission, and flow control mechanisms to guarantee that all
data is received without errors and in the correct order.
Stream Abstraction:
Stream of Bytes: TCP abstracts the communication between processes as a stream of
bytes. Processes can write data to this stream or read data from it.
Message Sizes: Applications can choose the size of data to write or read, regardless of
how much data is actually sent or received at a time. TCP manages the transmission and
reception of data in packets.
Lost Messages: TCP handles lost messages using an acknowledgment scheme. It
retransmits data if acknowledgments are not received within a timeout.
Flow Control: TCP matches the speed of data transmission between sender and receiver.
If the sender is too fast, TCP blocks it until the receiver consumes sufficient data.
Message Duplication and Ordering: TCP uses message identifiers to detect duplicates
and reorder messages if they arrive out of sequence.
Message Destinations: Processes establish connections before communication. After the
connection, they communicate via streams without needing to know the Internet
addresses and ports of each other.

Java API for TCP streams –


The Java interface to TCP streams is provided in the classes ServerSocket and Socket:
ServerSocket: This class is intended for use by a server to create a socket at a server port
for listening for connect requests from clients. Its accept method gets a connect request
from the queue or, if the queue is empty, blocks until one arrives. The result of executing
accept is an instance of Socket – a socket to use for communicating with the client.
Socket: This class is for use by a pair of processes with a connection. The client uses a
constructor to create a socket, specifying the DNS hostname and port of a server. This
constructor not only creates a socket associated with a local port but also connects it to
the specified remote computer and port number. It can throw an UnknownHostException
if the hostname is wrong or an IOException if an IO error occurs. The Socket class
provides the methods getInputStream and getOutputStream for accessing the two streams
associated with a socket. The return types of these methods are InputStream and
OutputStream, respectively – abstract classes that define methods for reading and writing
bytes. The return values can be used as the arguments of constructors for suitable input
and output streams. Our example uses DataInputStream and DataOutputStream, which
allow binary representations of primitive data types to be read and written in a machine-
independent manner.

You might also like