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

IPC Using Message Passing

Message passing provides a mechanism for processes to communicate without sharing memory. It uses primitives like send and receive to allow processes to exchange fixed or variable sized messages. Messages can be sent directly between processes or indirectly through shared mailboxes. Communication can be synchronous using blocking primitives, or asynchronous using non-blocking primitives. Message queues are commonly used to implement message passing between processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

IPC Using Message Passing

Message passing provides a mechanism for processes to communicate without sharing memory. It uses primitives like send and receive to allow processes to exchange fixed or variable sized messages. Messages can be sent directly between processes or indirectly through shared mailboxes. Communication can be synchronous using blocking primitives, or asynchronous using non-blocking primitives. Message queues are commonly used to implement message passing between processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

IPC using Message Passing

Message-Passing Systems
• Message passing provides a mechanism to allow
processes to communicate and to synchronize their
actions without sharing the same address space.
• It is particularly useful in a distributed environment,
where the communicating processes may reside on
different computers connected by a network.
• A message-passing facility provides at least two
operations: send(message) and receive(message).
• Messages sent by a process can be of either fixed or
variable size.

Basic Message-passing Primitives

A. Frank - P. Weisberg
Message format
• Consists of header and body
of message.
• In Unix: no ID, only message
type.
• Control info:
– what to do if run out of buffer
space.
– sequence numbers.
– priority.
• Queuing discipline: usually
FIFO but can also include
priorities.

Message Queue Vs Pipes


Message Passing
• Message passing is a general method used
for IPC:
– for processes inside the same computer.
– for processes in a networked/distributed
system.
• In both cases, the process may or may not
be blocked while sending a message or
attempting to receive a message.

Synchronization in message passing

• Message passing may be 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
• For the sender: it is more natural not to be
blocked after issuing send:
– can send several messages to multiple
destinations.
– but sender usually expect acknowledgment of
message receipt (in case receiver fails).
• For the receiver: it is more natural to be
blocked after issuing receive:
– the receiver usually needs the information before
proceeding.
– but could be blocked indefinitely if sender process
fails before send.

• Hence other possibilities are sometimes


offered.
• Example: blocking send, blocking receive:
– both are blocked until the message is
received.
– occurs when the communication link is
unbuffered (no message queue).
– provides tight synchronization (rendezvous).
Combinations
• There are really 3 combinations here that make
sense:
1. Blocking send, Blocking receive
2. Non-blocking send, Non-blocking receive
3. Non-blocking send, Blocking receive – most
popular – example:
• Client-Server proceses

IPC Requirements
• If P and Q wish to communicate, they need
to:
– establish 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)
Implementation Questions

• How are links established?


• Can a link be associated with more than two
processes?
• How many links can there be between every
pair of communicating processes?
• What is the capacity of a link?
• Is the size of a message that the link can
accommodate fixed or variable?
• Is a link unidirectional or bi-directional?

Link Capacity – Buffering


• Queue of messages attached to the link;
implemented in one of three ways:
1. Zero capacity – 0 messages
Sender must wait for receiver (rendezvous).
2. Bounded capacity – finite length of n
messages
Sender must wait if link full.
3. Unbounded capacity – infinite length
Sender never waits.
Direct/Indirect Communication
• Direct communication:
– when a specific process identifier is used for
source/destination.
– but it might be impossible to specify the source
ahead of time (e.g., a print server).
• Indirect communication (more convenient):
– messages are sent to a shared mailbox which
consists of a queue of messages.
– senders place messages in the mailbox, receivers
pick them up.

Direct Communication
• Processes must name each other explicitly:
– send(P, message) – send a message to process P
– receive(Q, message) – receive a message from 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.
– Who gets the message?
• Possible solutions:
– Allow a link to be associated with at most two
processes.
– Allow only one process at a time to execute a receive
operation.
– Allow the system to select arbitrarily the receiver.
Sender is notified who the receiver was.

Mailboxes and Ports


• A mailbox can be private to one
sender/receiver pair.
• The same mailbox can be shared
among several senders and
receivers:
– the OS may then allow the
use of message types (for
selection).
• Port: is a mailbox associated
with one receiver and multiple
senders
– used for client/server
applications: the receiver is
the server.
Message Queue Implementation

• To perform communication using message


queues, following are the steps −
• Step 1 − Create a message queue or connect to
an already existing message queue (msgget())
• Step 2 − Write into message queue (msgsnd())
• Step 3 − Read from the message queue (msgrcv())
• Step 4 − Perform control opera ons on the
message queue (msgctl())
• Initializing the Message Queue
– The msgget() function initializes a new message
queue:
• Sending and Receiving Messages
– The msgsnd() and msgrcv() functions send and receive
messages, respectively
– msgtype, indicates the type of message
• Controlling message queues
– The msgctl() function alters the permissions and other
characteristics of a message queue. The owner or
creator of a queue can change its ownership or
permissions using msgctl().

#include <sys/types.h>
struct queue
{
long type;
char text[200];
}buf;
int main()
{ int msgqid;
msgqid = msgget(42L, 0666 | IPC_CREAT);
printf("Message qid: %d",msgqid);
printf(“Enter the Message”);
scanf("%s", buf.text)
do { printf(“Enter the Message Type”);
scanf("%ld",&buf.type);
int len = strlen(buf.text);
msgsnd(msgqid, &buf, len +1,0);
printf(“Enter the Message”);
scanf("%s", buf.text)
}while(strcmp(buf.text,”end”) !=0);
msgctl(msgqid, IPC_RMID,NULL);
}
//Sample message and type: Nokia 1, Redmi 2, Motorola 3, Asus 4
struct queue
{ long type;
char text[100];
}buf;
long int x;
int i=1;
int main()
{ int msgqid;
msgqid = msgget(42L,0666|IPC_CREAT);
printf("%d",msgqid);
while (i<=5)
{ printf("Enter the type:");
scanf("%ld",&buf.type);
printf("%ld",buf.type);
msgrcv(msgqid,&buf,sizeof(buf.text),buf.type,0);
printf("Message associated with type is %s",buf.text);
i++;
} } // Enter the type: 3 -> Message associated with type is Motorola

Summary
• Message passing is a IPC technique for
communication among local processes as well as
remote processes.
• Send and Receive primitives are provided by OS.
• Direct or indirect communication
• Process Ids or port numbers used for direct
communication
• Mailboxes used for indirect communication
• Fixed or variable sized messages
• Blocked or non-blocked primitives
References
1. SilberSchatz, J. Peterson, and P. Galvin, “Operating System
concepts”, John Wiley
2. William Stallings, “Operating Systems Internals and Design
Principles”, Pearson Education
3. Tanenbaum, Andrew S, and Herbert Bos “Modern Operating
Systems”, Pearson

You might also like