0% found this document useful (0 votes)
11 views

Unit-3

Inter-Process Communication (IPC) encompasses mechanisms that enable processes to communicate and synchronize within a computing system, essential for complex software systems. Various IPC methods include pipes, shared memory, message queues, sockets, signals, and semaphores, each serving specific communication needs. IPC namespaces provide isolation for these resources, allowing multiple instances to coexist without conflict, enhancing process management and resource handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit-3

Inter-Process Communication (IPC) encompasses mechanisms that enable processes to communicate and synchronize within a computing system, essential for complex software systems. Various IPC methods include pipes, shared memory, message queues, sockets, signals, and semaphores, each serving specific communication needs. IPC namespaces provide isolation for these resources, allowing multiple instances to coexist without conflict, enhancing process management and resource handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 93

Unit-3

InterProcessCommunication
IPC
IPC

► Inter-process communication (IPC) refers to the mechanisms that


allow processes to communicate and synchronize with each other
in a computing system.
► Processes are instances of executing programs on a computer, and
IPC enables them to exchange data, coordinate activities, and
share resources.
► IPC is essential for building complex software systems where
multiple processes need to work together to accomplish a task.
► Interprocess communication (IPC) refers to the methods and
mechanisms used by processes to exchange data and synchronize
their activities.
► In a multitasking operating system, multiple processes may run
concurrently, and IPC provides a means for them to communicate
and coordinate with each other.
► IPC is essential for building complex systems where different
processes need to collaborate to achieve a common goal.
IPC between processes on a single
computer system

► Inter Process Communication (IPC) is a mechanism that involves communication


of one process with another process.This usually occurs only in one system.
Between related processes initiating from only one process, such as parent and
child processes. Between unrelated processes, or two or more different
processes.

► Inter-process communication (IPC) refers to the mechanisms used by processes


to communicate with each other.
► There are several methods for IPC in Unix:
various methods of IPC:

► Pipes
► Shared memory
► Message Queues
► Sockets
► Signals
► Semaphores
Pipes:

► Pipes provide a one-way communication channel between two related processes.


► A pipe is a one-way communication channel that allows the output of one process
to be the input of another process.
► One process writes data to the pipe, and the other process reads from it.
► Pipes are commonly used for communication between a parent process and its
child process.
► There are two types of pipes: unnamed pipes, which exist between related
processes (e.g., parent and child), and named pipes (FIFOs), which are used for
communication between unrelated processes.
Shared memory

► Shared memory allows multiple processes to access the same region of memory,
allowing them to communicate by reading and writing to shared memory locations.
► This provides a fast and efficient means of communication because data can be
transferred between processes without the need for copying.
► However, shared memory requires careful synchronization to prevent race
conditions and ensure data consistency.
Message queues

► Message queues provide a message-based communication mechanism between


processes.
► Processes can send messages to a queue, and other processes can retrieve
them in a first-in-first-out (FIFO) order.
► Message queues are often used when multiple processes need to exchange
relatively small amounts of data in a structured format.
Sockets

► Sockets are communication endpoints that allow processes to communicate over


a network or within the same system.
► They provide bidirectional communication and support various communication
protocols, such as TCP/IP and Unix domain sockets.
► Sockets are widely used for building client-server applications and interprocess
communication in distributed systems.
Signals

► Signals are used for asynchronous communication between processes.


► A process can send a signal to another process to notify it of events or request
action.
► Signals are commonly used for process management (e.g., handling termination
signals) and for interprocess communication in event-driven systems.
Semaphores

1. Semaphores are synchronization primitives used to control access to shared resources


by multiple processes.
2. Semaphores are used for process synchronization and mutual exclusion. They are often
used in conjunction with shared memory to coordinate access to shared resources.
3. They allow processes to coordinate their activities and avoid race conditions by
enforcing mutual exclusion and synchronization.
4. Semaphores can be binary (mutexes) or counting semaphores, and they provide a way
to implement critical sections and process synchronization.
Asynchronous communication

► asynchronous communication refers to a communication mechanism where the


sender and receiver processes are not synchronized in time.
► That is, the sender does not need to wait for an immediate response from the
receiver after sending a message or performing an action.
► Instead, the sender continues its execution independently, and the receiver
processes the message or action whenever it receives it, possibly at a later time.
Pipes

► Pipes provide a one-way communication channel between two


related processes.
► A pipe is a one-way communication channel that allows the
output of one process to be the input of another process.
► One process writes data to the pipe, and the other process
reads from it.
► Pipes are commonly used for communication between a parent
process and its child process.
► There are two types of pipes:
unnamed pipes, which exist between related processes
(e.g., parent and child), and
named pipes (FIFOs), which are used for communication
between unrelated processes.
pipe
► Parent Process sends the data to child process and child process display
the data.
► PARENT PROCESS is the sender
► CHILD PROCESS is the receiver
fd[0] – Read fd[1] - write
► File locking and record locking are really the same thing, except that
► file locking blocks access to the whole file, while
► record locking blocks access to only a specified segment of the file.
► File and record locking mechanisms are used to coordinate access to
files and records by multiple processes to prevent conflicts and data
corruption.
► File and record locking mechanisms are essential for maintaining data
integrity and preventing race conditions where multiple processes may be
accessing the same files concurrently.
► it's important to use them correctly and judiciously to avoid deadlocks and
other synchronization issues.
Lock Types

► Lock Types: UNIX supports two types of locks: advisory locks and mandatory
locks.
• Advisory locks: Processes can request locks, but it's up to other processes to
honor these locks.
• Mandatory locks: The system enforces locks, ensuring that no process can
access the locked portion of the file without appropriate permissions.
File locking
• File locking allows processes to prevent other processes from
accessing a file while it is being used.
• The two main types of file locking in Unix are advisory locking and mandatory
locking.
example
Lock structure
Locking Commands
Locking Process
Unlocking
fl_lock1.c
fl_lock2.c
Program on Record locking
Recordlock –program 1
Recordlock Program 2
Other UNIX locking techniques
FIFOs

► FIFOs (First In, First Out) are a method of inter-process


communication (IPC) in UNIX systems.
► They provide a way for processes to communicate with each other
by reading and writing to a named pipe.
Like Teacher and students
Sender
Sender
Receiver
Non block sender file
Non block in receiver file
► 1. Pipes
explore the concept of streams in IPC in
UNIX
Streams

► UNIX streams are a flexible and powerful


framework for IPC, providing bidirectional
communication channels between processes.
► Streams in UNIX provide robust and versatile mechanisms for IPC,
suitable for various applications
► ranging from
simple communication between processes to
complex network protocol implementations.
► Stream pipes, often referred to as socket pairs, provide a
bidirectional communication channel between two processes.

► The example will involve creating a socket pair, forking a child


process, and then having the parent and child processes
communicate through the socket pair.
streams
Messages

Inter-process communication (IPC) in UNIX using messages can


be achieved through various mechanisms, such as
► message queues,
► sockets, and
► shared memory.
Message Queue
Message Queues

► Message queues allow processes to send and receive messages in a


FIFO (first in, first out) manner.

► They provide a way to exchange messages between processes,


allowing for asynchronous communication.
Key Functions for Message Queues:
• msgget(): Creates a new message queue or accesses an existing queue.

• msgsnd(): Sends a message to a message queue.

• msgrcv(): Receives a message from a message queue.

• msgctl(): Performs various operations on a message queue, such as removing


it.
Namespaces
Namespaces

► Namespaces are used to provide isolation and independence for various


system resources, including Inter-Process Communication (IPC)
mechanisms
► IPC namespaces specifically allow for isolated communication channels
between processes, ensuring that IPC resources do not interfere with each
other across different namespaces.
IPC Namespaces in UNIX

► IPC namespaces isolate certain IPC resources such as message queues,


semaphores, and shared memory segments. This isolation allows multiple
instances of these resources to coexist on the same system without conflicts.
Key Concepts
Creating an IPC Namespace:

► The ‘unshare’ command can be used to create a new IPC namespace.


For example:
► unshare --ipc
► This command creates a new shell with its own IPC namespace.
► Programmatically Creating an IPC Namespace:
► The ‘clone’ system call can be used to create a new process with a separate IPC
namespace:
► #define _GNU_SOURCE
► #include <sched.h>
► #include <unistd.h>
► #include <stdio.h>

► int main() {
► if (clone(NULL, NULL, CLONE_NEWIPC, NULL) == -1) {
► perror("clone");
► return 1;
► }
► // Now in a new IPC namespace
► // Create and manipulate IPC resources here
► return 0;
► }
Benefits of IPC Namespaces
IPC

You might also like