0% found this document useful (0 votes)
20 views8 pages

Os Unit-Iii

The document discusses Interprocess Communication (IPC), highlighting the differences between cooperating and independent processes, and the necessity of IPC for data sharing and synchronization. It details two main IPC models: Shared Memory and Message Passing, explaining their mechanisms, advantages, and disadvantages. Key concepts such as direct and indirect communication, synchronization methods, and system calls for implementation in Unix/Linux are also covered.

Uploaded by

Kolli Bavitha
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)
20 views8 pages

Os Unit-Iii

The document discusses Interprocess Communication (IPC), highlighting the differences between cooperating and independent processes, and the necessity of IPC for data sharing and synchronization. It details two main IPC models: Shared Memory and Message Passing, explaining their mechanisms, advantages, and disadvantages. Key concepts such as direct and indirect communication, synchronization methods, and system calls for implementation in Unix/Linux are also covered.

Uploaded by

Kolli Bavitha
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/ 8

UNIT-III

Interprocess Communication

 Processes within a system may be independent or cooperating


 Cooperating process can affect or be affected by other processes, including sharing
data
 Reasons for cooperating processes:
 Information sharing
 Computation speedup
 Modularity
 Convenience
 Cooperating processes need interprocess communication (IPC)
 Two models of IPC
 Shared memory
 Message passing

Communications Models

Interprocess Communication – Message Passing


 Mechanism for processes to communicate and to synchronize their actions
 Message system – processes communicate with each other without resorting to shared
variables
 IPC facility provides two operations:
 send(message) – message size fixed or variable
 receive(message)
 If P and Q wish to communicate, they need to:
 establish a communication link between them
 exchange messages via send/receive
 Implementation of communication link
 physical (e.g., shared memory, hardware bus)
 logical (e.g., logical properties

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

Queue of messages attached to the link; implemented in one of three ways


 Zero capacity – 0 messages Sender must wait for receiver (rendezvous)
 Bounded capacity – finite length of n messages Sender must wait if link full
 Unbounded capacity – infinite length Sender never waits

Types of IPC Mechanisms

IPC mechanisms can be broadly categorized into:

1. Shared Memory

2. Message Passing

Shared Memory in IPC (Inter-Process Communication)

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.

How Shared Memory Works

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.

5. Destruction: The shared memory segment is destroyed when it is no longer needed.

Key Concepts in Shared Memory

1. Shared Memory Segment: A block of memory that is shared between processes.

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.

Steps to Implement Shared Memory

1. Create the Shared Memory Segment:

o A process requests the operating system to create a shared memory segment.

o The OS returns a unique identifier (shmid) for the shared memory segment.

2. Attach the Shared Memory Segment:


o Processes attach the shared memory segment to their address space using
the shmat() system call.
o After attachment, the shared memory appears as part of the process's address
space.

3. Read/Write to Shared Memory:

o Processes can directly read from and write to the shared memory segment.

4. Detach the Shared Memory Segment:

o When a process no longer needs the shared memory, it detaches using


the shmdt() system call.

5. Destroy the Shared Memory Segment:

o The shared memory segment is destroyed using the shmctl() system call when it is
no longer needed.

System Calls for Shared Memory in Unix/Linux

1. shmget():

o Creates a new shared memory segment or obtains the identifier of an existing one.

o Syntax: int shmget(key_t key, size_t size, int shmflg);

o Example:

int shmid = shmget(IPC_PRIVATE, 1024, 0666 | IPC_CREAT);

2. shmat():

o Attaches the shared memory segment to the process's address space.

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

o Example:

char *data = (char *)shmat(shmid, NULL, 0);

3. shmdt():

o Detaches the shared memory segment from the process's address space.

o Syntax: int shmdt(const void *shmaddr);

o Example:

shmdt(data);

4. shmctl():

o Controls the shared memory segment (e.g., destroys it).

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

o Example:

shmctl(shmid, IPC_RMID, NULL);


Advantages of Shared Memory

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.

Disadvantages of Shared Memory

1. Synchronization Required: Processes must use synchronization mechanisms (e.g.,


semaphores) to avoid race conditions.

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.

Message Passing in IPC (Inter-Process Communication)

Message Passing is a fundamental mechanism for Inter-Process Communication (IPC) in operating


systems. It allows processes to communicate by sending and receiving messages. Unlike shared
memory, message passing does not require processes to share a common memory region, making it
easier to implement and less prone to synchronization issues. It is widely used in both single-
machine and distributed systems.

How Message Passing Works

1. Sender Process: Creates a message and sends it to the receiver process.

2. Receiver Process: Receives the message and processes it.

3. Communication Channel: The operating system provides a communication channel (e.g.,


message queue, pipe, or socket) for transferring messages.

Key Concepts in Message Passing

1. Message: A unit of data exchanged between processes. It typically consists of:

o Header: Contains metadata (e.g., message type, sender ID).

o Body: Contains the actual data.

2. Synchronous vs. Asynchronous Communication:

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:

o Direct Communication: Processes communicate directly by naming each other (e.g.,


sender specifies the receiver's ID).

o Indirect Communication: Processes communicate through a shared mailbox or


message queue.

Types of Message Passing Mechanisms

1. Pipes

2. Message Queues

1. Pipes

 Description: A unidirectional communication channel between two processes. Data written


to one end is read from the other.

 Types:

o Anonymous Pipes: Used for communication between related processes (e.g., parent
and child).

o Named Pipes (FIFOs): Used for communication between unrelated processes.

 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 Messages can be prioritized.

o Communication is asynchronous.

 Example:

o A producer process sends messages to a queue, and a consumer process reads them.

System Calls for Message Passing in Unix/Linux

1. pipe():

o Creates an anonymous pipe.

o Syntax: int pipe(int fd[2]);

o Example:

int fd[2];
pipe(fd);

2. msgget():

o Creates or accesses a message queue.

o Syntax: int msgget(key_t key, int msgflg);

o Example:

int msgid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);

3. msgsnd():

o Sends a message to a message queue.

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

o Example:

struct message {

long mtype;

char mtext[100];

};

struct message msg;

msg.mtype = 1;

strcpy(msg.mtext, "Hello, Message Queue!");

msgsnd(msgid, &msg, sizeof(msg.mtext), 0);

4. msgrcv():

o Receives a message from a message queue.

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

o Example:

struct message msg;

msgrcv(msgid, &msg, sizeof(msg.mtext), 1, 0);

printf("Received: %s\n", msg.mtext);

5. msgctl():

o Controls a message queue (e.g., deletes it).

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

o Example:

msgctl(msgid, IPC_RMID, NULL);

Advantages of Message Passing


1. No Shared Memory: Avoids synchronization issues like race conditions.

2. Portability: Works well in both single-machine and distributed systems.

3. Modularity: Encourages modular design by decoupling processes.

Disadvantages of Message Passing

1. Slower Than Shared Memory: Requires kernel intervention for message transfer.

2. Overhead: Additional overhead due to message creation and copying.

3. Complexity: Managing message queues and synchronization can be complex.

You might also like