IPC New
IPC New
Between related processes initiating from only one process, such as parent and child
processes.
Between unrelated processes, or two or more different processes.
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 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
Advantages of Pipes
- Simple to use and setup
- Automatic synchronization
Disadvantages of Pipes
- Only for related processes
- Processes open the file using open() and use read() and write().
Advantages of FIFOs
- Supports unrelated process communication
Disadvantages of FIFOs
- Still unidirectional
🔹 Key Features:
Advantages:
Disadvantages:
Key Features
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.
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
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
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
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
Disadvantages