0% found this document useful (0 votes)
11 views17 pages

Lecture-14-Message Passing

Uploaded by

shashank.skr1
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)
11 views17 pages

Lecture-14-Message Passing

Uploaded by

shashank.skr1
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/ 17

Operating Systems (CS3000)

Lecture – 15
(Inter Process Communication - 2)

Dr. Jaishree Mayank


Assistant Professor
Department of Computer Sc. and Engg.
Problem with SM
• Synchronization needed between the processes

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);

• creates a new message queue

• On success, msgget()returns the message queue identifier (a positive


integer).
• On failure, it returns -1and sets errnoto indicate the error.

6
IPC using Message Queues

• int msgsnd(int msqid, void *msgp, size_t msgsz, int msgflg);


• send a message to the message queue specified by the msqid parameter.
• It is returned by the msgget() function and used to identify the message
queue to send the message to.

• 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);

• The msgtyp parameter specifies the type of message to receive from


the message queue as follows:
• If msgtyp = 0, read the first message in the queue.
• the messages will the retrieved in the same order in which they were written into the
message queue
• If msgtyp > 0, the first message of type “msgtyp” is only received.
• If msgtyp < 0, the first message of the lowest type that is less than or equal
to the absolute value of msgtyp is received.

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:

• If the IPC_NOWAIT flag is set in the msgflg parameter, msgrcv() returns


immediately with a return value of -1.

• If 0 : suspend the process

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

You might also like