IPC Using Message Passing
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.
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.
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
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.
#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