OPERATING SYSTEM
CSET209
INTER PROCESS
COMMUNICATION (IPC)
INTER PROCESS COMMUNICATION (IPC)
Processes executing concurrently in the operating system may be independent or cooperative.
Independent means doesn’t share data with any other process.
It cannot affect or be affected by the execution of another process
Cooperating process is a process that shares data with other process(es). Thus, can affect or be affected
by the execution of another process
Inter-process communication (IPC) is a mechanism that allows processes to communicate with each
other and synchronize their actions.
The communication between these processes can be seen as a method of co-operation between them.
INTER PROCESS COMMUNICATION (IPC) : NEED
A program may involve more than one flow of control in its execution.
Example: A program may execute in the form of multiple threads cooperating to achieve a
common goal.
To cooperate, processes somehow must communicate.
But, Operating system does not allow a process to read memory locations of another process.
During execution, each process can only view its virtual addresses,
It cannot
View another processes virtual address space
Determine the physical address mapping
Inter process communication (IPC)
Characteristics
Their states are shared by other processes;
This implies that the program has shared resources or variables that are accessed or
modified by multiple concurrent processes or threads. Shared states introduce the
need for proper synchronization mechanisms to ensure data consistency and prevent
race conditions.
Its execution is not deterministic, i.e., the results of execution depend on relative
execution sequence and cannot be predicted in advance;
Its execution is irreproducible, i.e., the results of execution are not always the same for
the same input.
•Nondeterministic Behavior:
• Co-operating processes exhibit behavior that is not predictable beforehand.
• The outcome depends on the relative execution sequence of the processes.
• The exact order in which processes execute and complete their tasks is not predetermined.
•Irreproducible Results:
• Co-operating processes can produce different outputs even with the same input data.
• For instance, if one process writes "ABC" and another writes "CBA," the final result can vary.
• It is challenging to determine which process contributed a specific part of the output.
•Example: "ABCCBA" Output:
• Illustrative scenario: One process writes "ABC," another writes "CBA."
• The output "ABCCBA" can result from the collaboration, but determining the order of each contribution is
challenging.
• The subtle state sharing, possibly through a shared terminal, adds complexity to predicting the exact output.
•Limitations on Possible Outputs:
• Although outcomes are unpredictable, not every combination is possible.
• Certain sequences, such as "AABBCC," are not valid and cannot occur as a result of co-operating processes.
• There are constraints on what can transpire, despite the inherent nondeterminism.
IPC : 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:
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.
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.
SHARED MEMORY
One process will create a shared memory segment in RAM which the other process can also access.
Advantages:
Access to this shared memory region is like a regular array like access.
Communication is extremely fast as no system calls are involved.
Limitation:
Needs synchronization between the processes. Absence of synchronization
leads to errors.
MESSAGE PASSING
Shared memory segment is created in the Kernel space.
Hence, system calls such as send and receive are required to communicate.
Advantages:
Less error prone as the synchronization is taken care of by the kernel.
Limitation:
Slower in comparison to shared memory due to the use of system calls.
MESSAGE PASSING
Message Queues (POSIX message queues, System V message queues)
Pipes (Anonymous (Ordinary) pipes, Named pipes)
Signals
Remote Procedure Calls (RPC)
Sockets
MESSAGE QUEUES
Message Queues are a form of Inter-Process Communication (IPC) where processes communicate by sending
and receiving messages through a queue.
The messages are stored in the queue and are processed asynchronously.
Message Queue:
A data structure that holds messages sent from one process to another.
Messages are stored in the queue until they are received by the intended recipient.
Sender:
A process that sends messages to the queue.
Receiver:
A process that retrieves messages from the queue.
Queue Management:
The operating system or a middleware system manages the creation, deletion, and maintenance of the
message queue.
MESSAGE PASSING
The communication of a message between two processes implies some level of synchronization between the two
processes.
Sender and receiver can be blocking or nonblocking.
Three combinations are possible using blocking and nonblocking.
Blocking Sender and Blocking Receiver
The sender process is halted until the receiver acknowledges the message.
The receiver is also blocked until it receives the message.
Synchronization occurs as both processes are paused, ensuring a coordinated exchange.
This is called rendezvous.
This combination allows for tight synchronization between processes.
Nonblocking Sender and Blocking Receiver:
The sender doesn't wait for an acknowledgment from the receiver and continues its execution.
The receiver, however, is blocked until it receives the message, ensuring synchronization from the receiver's
perspective.
An example is a server process that exists to provide a service or resource to other processes.
Nonblocking Sender and Nonblocking Receiver
Sending process sends the message and resumes the operation.
Receiver retrieves either a valid message or a null i.e. neither party is required to wait.
SYNCHRONIZATION IN MESSAGE PASSING
Methods for message passing operations:
Direct and Indirect communication
In Direct communication, Each task explicitly pass the message along with passing name of process to which it
is passing.
In Indirect communication, Message passing is through mailboxes. The tasks communicating should have a
shared mailbox.
Message Format:
In operating systems that support variable-length messages, the message format typically consists of two
main parts: the header and the body. This approach allows flexibility in handling messages of different sizes
while minimizing processing and storage overhead.
Header:
Source Address: Identifies the sender of the message.
Destination Address: Specifies the intended recipient of the message.
Message Length: Indicates the size of the entire message (header + body).
Message Type: Defines the nature or purpose of the message.
Control Information: Includes flags or control bits for handling special
conditions.
Body:
Message Content: The actual data or information being transmitted.
Variable Length: Allows for flexibility in accommodating messages of different sizes.
MESSAGE QUEUES
MESSAGE QUEUES
pipes
Pipes act as a channel between 2 processes to communicate. These were one of the first IPC
mechanisms in early UNIX. Pipes allow a mechanism in which output of one process is input of
another process. Pipes function as FIFO.
There are 2 types of pipes:
Ordinary pipes: It allows communication in producer-consumer fashion. The producer writes to
one end, and consumer reads from the other. It allows only undirectional flow. For 2-way, we will
have to use 2 ordinary pipes. On Windows systems, ordinary pipes are termed as Anonymous
pipes and the communicating processes have a parent-child relationship. These can be used only
for communication between processes on same machine. Once processes finish, ordinary pipes
don't exist.
Named pipes: These can be bidirectional, require no parent-child relationship and is used for
communication for several processes. These pipes continue to exist after communicating processes
have finished. Named pipes are referred to as FIFOs in UNIX.
Ordinary pipes
NAMED PIPES
Sender Code
NAMED PIPES
Receiver Code
Signals
Signals in inter-process communication (IPC) are a mechanism for asynchronous communication
between processes in a Unix-like operating system.
A signal is a software interrupt that notifies a process about a specific event or requests some action.
Signals are often used for IPC to convey information or trigger behavior in another process.
Types Signals
Signals Working
Sender Process:
The sender process (sender.c) sends a signal (SIGUSR1) to the receiver process.
The kill function is used to send a signal to a process identified by its process ID (pid_t). In this
example, replace <RECEIVER_PID> with the actual PID of the receiver process.
If the signal is sent successfully, a message is printed.
Receiver Process:
The receiver process (receiver.c) registers a signal handler function using the signal function. In this
example, the handler is set up for the SIGUSR1 signal.
The process then enters a loop where it continuously waits for signals.
When the SIGUSR1 signal is received, the registered signal handler (signalHandler) is called, and a
message is printed.
Signal Handler:
The signal handler function (signalHandler) is a function that gets executed when the process receives
the specified signal (in this case, SIGUSR1).
The handler function can perform custom actions in response to the received signal.
Signals
Receiver
Sender
REMOTE PROCEDURE CALL
HOW TO MAKE A REMOTE PROCEDURE CALL
MESSAGE PASSING
MESSAGE PASSING
Remote Procedure Call (RPC) is a protocol facilitating communication between processes on different
computers or networked systems.
Client-Server Model:
RPC follows the client-server model where the client initiates a request, and the server processes the
request, returning the result.
Stubs:
Stubs abstract the complexity of network communication, representing local procedure calls on the
client and server sides.
Interface Definition Language (IDL):
IDL is used to define the interface between client and server, specifying procedures, data types, and
parameters.
Marshalling and Unmarshalling:
Marshalling packages procedure call parameters into a network-transmissible format. Unmarshalling is
the reverse process on the receiving side.
Binding:
Binding associates a client request with the corresponding server procedure, either at compile time or
runtime.
Thank you !