0% found this document useful (0 votes)
10 views10 pages

IPC New

Inter Process Communication (IPC) enables communication between processes within a system, facilitating data sharing, synchronization, and resource management. IPC methods include pipes, FIFOs, shared memory, message queues, and semaphores, each with distinct advantages and disadvantages. Applications of IPC span various domains, including shell pipelines, GUIs, server-client communication, and distributed systems.

Uploaded by

manognabingi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

IPC New

Inter Process Communication (IPC) enables communication between processes within a system, facilitating data sharing, synchronization, and resource management. IPC methods include pipes, FIFOs, shared memory, message queues, and semaphores, each with distinct advantages and disadvantages. Applications of IPC span various domains, including shell pipelines, GUIs, server-client communication, and distributed systems.

Uploaded by

manognabingi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Inter Process Communication - Overview

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


one process with another process. This usually occurs only in one system.

Communication can be of two types −

 Between related processes initiating from only one process, such as parent and child
processes.
 Between unrelated processes, or two or more different processes.

The Need for IPC

1. Data Sharing: Processes often need to share data. For example, a text editor
may need to pass data to a printer process to generate a hard copy.
2. Synchronization: Processes may need to synchronize their activities. For
instance, in a multi-threaded environment, threads must coordinate to ensure
data consistency.
3. Communication: Processes might need to communicate for a variety of
purposes, such as exchanging information, signaling, and error handling.
4. Resource Sharing: IPC helps manage and share resources, like file access,
memory, or hardware devices among processes.

Applications of IPC

IPC is a fundamental component of modern operating systems and finds applications


in various scenarios:
1. Shell Pipelines: In Unix-like systems, the shell uses pipes to connect the output
of one command to the input of another.
2. Graphical User Interfaces (GUIs): GUI applications use IPC for event
handling, such as sending messages between windows or processes.
3. Server-Client Communication: IPC is essential in client-server applications.
Clients and servers communicate over sockets, pipes, or other IPC
mechanisms.
4. Multi-threading: In multi-threaded programs, threads within a process must
communicate and synchronize through IPC mechanisms like semaphores and
mutexes.
5. Distributed Systems: IPC is crucial in distributed computing, where processes
may run on different machines. Message passing is commonly used in such
scenarios.

IPC Methods

There are several methods of IPC used in modern operating systems. Each method
has its strengths and weaknesses, and the choice of method depends on the specific
requirements of the processes involved:
Pipes − Communication between two related processes. The mechanism is half
duplex meaning the first process communicates with the second process. To achieve
a full duplex i.e., for the second process to communicate with the first process
another pipe is required.
FIFO − Communication between two unrelated processes. FIFO is a full duplex,
meaning the first process can communicate with the second process and vice versa
at the same time.
Message Queues − Communication between two or more processes with full duplex
capacity. The processes will communicate with each other by posting a message
and retrieving it out of the queue. Once retrieved, the message is no longer available
in the queue.
Shared Memory − Communication between two or more processes is achieved
through a shared piece of memory among all processes. The shared memory needs
to be protected from each other by synchronizing access to all the processes.
Semaphores − Semaphores are meant for synchronizing access to multiple
processes. When one process wants to access the memory (for reading or writing), it
needs to be locked (or protected) and released when the access is removed. This
needs to be repeated by all the processes to secure data.
Signals − Signal is a mechanism to communication between multiple processes by
way of signaling. This means a source process will send a signal (recognized by
number) and the destination process will handle it accordingly.
Inter Process Communication – Pipes

 A pipe is a unidirectional communication channel used between related


processes.
 It allows one process to write data and the other to read it.
 It is created using the pipe() system call and only works between parent-child
processes.

Creating a Pipe: pipe():


int pipe(int fd[2]);

- fd[0]: read end

- fd[1]: write end

Returns 0 on success, -1 on failure.

How Pipes Work


- After creating a pipe, the process forks into parent and child.

- One process closes the read end and writes data.

- The other closes the write end and reads data.

- Pipes operate in a FIFO (First In First Out) manner.

Advantages of Pipes
- Simple to use and setup

- Efficient for parent-child communication

- Automatic synchronization
Disadvantages of Pipes
- Only for related processes

- Unidirectional: one-way communication

- Limited buffer size

- Data is lost if not read

Inter Process Communication – FIFO (Named Pipe)

 A FIFO is a named pipe that appears as a file in the file system.


 It allows communication between unrelated processes.
 Unlike unnamed pipes, FIFOs have a name and remains in the file system.

Creating a FIFO: mkfifo()


int mkfifo(const char *pathname, mode_t mode);

- pathname: name of the FIFO file

- mode: file permission bits

Returns 0 on success, -1 on failure.

How FIFOs Work


- A FIFO is created using mkfifo().

- Processes open the file using open() and use read() and write().

- It works like a regular file but enables IPC.

Advantages of FIFOs
- Supports unrelated process communication

- Persist as file system objects

- Simple to use with open(), read(), write()

Disadvantages of FIFOs
- Still unidirectional

- Slower than unnamed pipes


- Need manual removal using unlink()

Inter Process Communication - Shared Memory

 Shared memory is a memory segment that can be simultaneously


accessed by multiple processes.
 It is the fastest form of IPC (Inter Process Communication).
 Once shared memory is established, no more kernel involvement is
required for data exchange between processes.

🔹 Key Features:

 Efficient and Fast: Since memory is shared directly, it avoids overhead.


 Needs synchronization: Processes must coordinate access (e.g., using
semaphores) to avoid conflicts.
 Data remains until explicitly removed or the system is shut down.

System Calls for Shared Memory:

1. shmget(): Allocate a shared memory segment

int shmget(key_t key, size_t size, int shmflg);


o key: Unique identifier (can be generated using ftok()).
o size: Size of memory in bytes.
o shmflg: Permissions and flags (e.g., IPC_CREAT).
o Returns: Shared memory ID on success, -1 on failure.
2. shmat(): Attach shared memory to process address space

void *shmat(int shmid, const void *shmaddr, int shmflg);

o shmid: ID returned by shmget.


o shmaddr: Address where memory should attach (usually NULL).
o shmflg: 0 (default) or SHM_RDONLY.
o Returns: Pointer to shared memory segment, (void *) -1 on failure.
3. shmdt(): Detach shared memory

int shmdt(const void *shmaddr);

o shmaddr: Pointer returned by shmat().


o Returns: 0 on success, -1 on failure.
4. shmctl(): Control operations on shared memory

int shmctl(int shmid, int cmd, struct shmid_ds *buf);

o cmd: Can be IPC_STAT, IPC_RMID, or IPC_SET.


o Used to: remove shared memory, get/set attributes.

Advantages:

 High speed communication.


 No need for kernel intervention after setup.

Disadvantages:

 Requires synchronization mechanisms.


 Not suitable for small data or simple needs.
 Security risks if not properly managed.
Inter Process Communication – Message Queues

 A message queue is a linked list of messages maintained by the kernel.


 It allows processes to send and receive data asynchronously through
messages.
 It supports communication between related and unrelated processes.
 Each queue is identified by a message queue ID obtained using msgget().

Key Features

 Asynchronous communication: Sender and receiver do not need to interact


at the same time.
 Message types allow filtering and prioritizing.
 Multiple senders and receivers can use the same queue.
 The queue remains in the system until explicitly removed with msgctl().

 Writing into the shared memory by one process and reading from the
shared memory by another process.
 Writing into the shared memory by one process with different data
packets and reading from it by multiple processes, i.e., as per message
type.

System Calls for Message Queues


1. msgget() – Create or access a message queue
int msgget(key_t key, int msgflg);

 Parameters:
o key: A unique key for the queue (often generated using ftok()).
o msgflg: Flags like IPC_CREAT, IPC_EXCL, and permission bits.
 Returns:
o On success: Message Queue Identifier (int)
o On failure: -1

2. msgsnd() – Send a message to the queue


int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

 Parameters:
o msqid: ID of the message queue
o msgp: Pointer to the message structure
o msgsz: Size of message text (not including mtype)
o msgflg: 0 or IPC_NOWAIT
 Returns:
o 0 on success
o -1 on failure

3. msgrcv() – Receive a message from the queue


int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

 Parameters:
o msqid: ID of the queue
o msgp: Pointer to receive message structure
o msgsz: Maximum size of message text
o msgtyp: Message type to receive (0 = first message)
o msgflg: Flags like IPC_NOWAIT
 Returns:
o Number of bytes received
o -1 on failure

4. msgctl() – Perform control operations


int msgctl(int msqid, int cmd, struct msqid_ds *buf);

 Parameters:
o msqid: Queue ID
o cmd: One of the following:
 IPC_STAT – Get information
 IPC_SET – Set attributes
 IPC_RMID – Delete the queue
o buf: Pointer to data structure for command
 Returns:
o 0 on success
o -1 on error
Structure of a Message

Each message consists of a message type (a long integer) and the message data
(character array or other format).

struct msgbuf {
long mtype; // Message type (> 0)
char mtext[100]; // Message data
};

💡 The message type helps categorize messages so a receiver can choose to read
only messages of a certain type.

Advantages

 Supports asynchronous communication – sender and receiver don’t need


to run at the same time.
 Multiple readers and writers – ideal for producer-consumer models.
 Message types enable priority handling and message filtering.
 Data is transferred in structured form.

Disadvantages

 System call overhead for sending/receiving messages.


 Limited size of messages and queue.
 Requires synchronization mechanisms for concurrent access.
 Security concerns if permissions are not handled properly.

You might also like