Os Unit-Iii
Os Unit-Iii
Interprocess Communication
Communications Models
Direct Communication
Processes must name each other explicitly:
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process Q
Properties of communication link
Links are established automatically
A link is associated with exactly one pair of communicating processes
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bi-directional
Indirect Communication
Messages are directed and received from mailboxes (also referred to as ports)
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
Properties of communication link
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
Mailbox sharing
P1, P2, and P3 share mailbox A
P1, sends; P2 and P3 receive
Synchronization
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
Blocking send has the sender block until the message is received
Blocking receive has the receiver block until a message is available
Non-blocking is considered asynchronous
Non-blocking send has the sender send the message and continue
Non-blocking receive has the receiver receive a valid message or null
Buffering
1. Shared Memory
2. Message Passing
Shared Memory is one of the fastest and most efficient mechanisms for Inter-Process
Communication (IPC) in operating systems. It allows multiple processes to access a common region
of memory, enabling them to share data directly without the need for kernel intervention after the
initial setup. This makes it ideal for high-performance applications where speed is critical.
1. Creation: A shared memory segment is created in the system's memory. This segment is
accessible by multiple processes.
2. Attachment: Processes that need to communicate attach the shared memory segment to
their address space.
3. Communication: Processes read from and write to the shared memory segment directly.
4. Detachment: When a process no longer needs the shared memory, it detaches from the
segment.
2. Process Address Space: Each process has its own virtual address space. Shared memory is
mapped into the address space of each participating process.
3. Synchronization: Since multiple processes can access the shared memory simultaneously,
synchronization mechanisms (e.g., semaphores or mutexes) are required to avoid race
conditions.
o The OS returns a unique identifier (shmid) for the shared memory segment.
o Processes can directly read from and write to the shared memory segment.
o The shared memory segment is destroyed using the shmctl() system call when it is
no longer needed.
1. shmget():
o Creates a new shared memory segment or obtains the identifier of an existing one.
o Example:
2. shmat():
o Example:
3. shmdt():
o Detaches the shared memory segment from the process's address space.
o Example:
shmdt(data);
4. shmctl():
o Example:
1. High Speed: Since processes access memory directly, shared memory is faster than other IPC
mechanisms like pipes or message queues.
2. Efficient for Large Data: Ideal for sharing large amounts of data between processes.
3. Flexibility: Processes can read and write to the shared memory at any time.
2. Complexity: Managing shared memory is more complex than other IPC mechanisms.
3. Security Risks: Improper handling can lead to security vulnerabilities, such as unauthorized
access.
4.
o Synchronous: The sender blocks until the receiver acknowledges the message.
o Asynchronous: The sender continues execution after sending the message without
waiting for acknowledgment.
3. Direct vs. Indirect Communication:
1. Pipes
2. Message Queues
1. Pipes
Types:
o Anonymous Pipes: Used for communication between related processes (e.g., parent
and child).
Example:
o In a shell, the command ls | grep .txt uses an anonymous pipe to send the output
of ls to grep.
2. Message Queues
Description: A linked list of messages stored in the kernel. Processes can send and receive
messages in a structured manner.
Advantages:
o Communication is asynchronous.
Example:
o A producer process sends messages to a queue, and a consumer process reads them.
1. pipe():
o Example:
int fd[2];
pipe(fd);
2. msgget():
o Example:
3. msgsnd():
o Syntax: int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
o Example:
struct message {
long mtype;
char mtext[100];
};
msg.mtype = 1;
4. msgrcv():
o Syntax: int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
o Example:
5. msgctl():
o Example:
1. Slower Than Shared Memory: Requires kernel intervention for message transfer.