Lecture-14-Message Passing
Lecture-14-Message Passing
Lecture – 15
(Inter Process Communication - 2)
2
Message Passing RAM
• SM is created in Kernel
SM
• System calls are used Kernel/OS
• send(): Write to SM send ()
• receive(): Read from SM
Process - 1 receive()
Process - 2
3
IPC using Message Queues
• linked list of messages
• stored within the kernel
• identified by a message queue identifier.
• Functions
• msgget() To create a message queue/Open Existing
• msgsnd() To write message to message queue by Sender/New messages
are added to the end of the queue
• msgrcv() To retrieve message from message queue by Receiver
• msgctl() The control function
4
IPC using Message Queues
• Sender
• Create the MQ
• Add data to the MQ
• Receiver
• Retrieve the data from MQ
• Delete the MQ
5
IPC using Message Queues
• int msgget(key_t key, int msgflg);
6
IPC using Message Queues
• The *msgp parameter points to a user-defined buffer that must contain the
following:
• A field of type long int that specifies the type of the message.
• An array that contains the actual content of the message.
7
IPC using Message Queues
• int msgsnd(int msqid, void *msgp, size_t msgsz, int msgflg);
• The following structure is an example of what the user-defined buffer might
look like for a message that has 5 bytes of data.
struct mymsg
{
long int mtype; /* message type */
char mtext[5]; /* message text */
}
• The value of mtype must be greater than zero. When messages are received
with msgrcv(), the message type can be used to select the messages.
• The message data can be any length up to the system limit.
8
IPC using Message Queues
• int msgsnd(int msqid, void *msgp, size_t msgsz, int msgflg);
•
• msgsz: Length of the data part of the message to be sent.
• msgflg: If the message queue is full, the msgflg parameter specifies the
action to be taken. The actions are as follows:
• 0: Suspended
• IPC_NOWAIT: do not wait for space to become available on the message queue and
return immediately.
9
IPC using Message Queues
• int msgrcv(int msqid, void *msgp, size_t msgsz, long int msgtyp, int
msgflg);
The msgrcv() function reads a message from the message queue
specified by the msqid parameter and places it in the user-defined
buffer pointed to by the *msgp parameter.
The *msgp parameter points to a user-defined buffer that must
contain the following:
• A field of type long int that specifies the type of the message.
• A data part that contains the data bytes of the message.
10
IPC using Message Queues
• int msgrcv(int msqid, void *msgp, size_t msgsz, long int msgtyp, int
msgflg);
struct mymsg
{
long int mtype; /* message type */
char mtext[5]; /* message text */
}
• The value of mtype is the type of the received message, as specified
by the sender of the message.
• The msgsz parameter specifies the size in bytes of the data part of
the message.
• The received message is truncated to msgsz bytes if it is larger than msgsz.
11
IPC using Message Queues
• int msgrcv(int msqid, void *msgp, size_t msgsz, long int msgtyp, int
msgflg);
12
IPC using Message Queues
• int msgrcv(int msqid, void *msgp, size_t msgsz, long int msgtyp, int
msgflg);
• If a message of the desired type is not available on the message queue,
the msgflg parameter specifies the action to be taken. The actions are as
follows:
13
IPC using Message Queues
• int msgctl(int msqid, int cmd, struct msqid_ds *buf);
• The msgctl() function allows the caller to control the message queue
specified by the msqid parameter.
• msqid Message queue identifier, a positive integer. It is returned by
the msgget() function and used to identify the message queue on which to
perform the control operation.
• cmd Command, the control operation to perform on the message queue.
• buf Pointer to the message queue data structure to be used to get or set
message queue information.
• msqid_ds
14
MQ_Send1.c
15
MQ_Send1.c
16
Thank You
Any Questions?
17