Hello and welcome.
In this video session we will discuss on the posix message queues.
In Linux.
IT Linux provides basically two two types of message queues.
One is the system file message queue and the other is the postfix message queue.
So these both are the standards system five and six.
We will discuss the message queue based on Posix standards as referred earlier.
Posix is Posix is nothing but the portable operating system interface, and this is a standard set by
Apple.
Message queues can be used to pass messages between the process.
In the earlier video session we had discussed the pipes and Fifo where.
Both pipes and C4 are used to communicate between the process using byte oriented, whereas a message
queue.
Is used to communicate between the process and this is basically a message oriented.
So each communication occurs as a message.
Basically the reader and writer communicate.
In terms of a unit of messages.
We will see with some examples what basically we mean by this.
In message cuz we we can set the priority of each messages that we transmit on the message queue so
that the receiver can also receive the priority based messages.
So this is the unique feature which is not present in pipelined fifos.
Also in Linux system, we can see the entry of the message queue in slash dev slash MQ folder.
So this folder.
Specifies all the message queue that is present in the file system.
Basically, the message queue is a persistent memory which finds its entry in the file system.
Now let's see the different system calls that are related to message queue.
The the system calls are MQ open.
MQ close MQ and link.
MQ send MQ receive.
MQ and score set attribute and MQ get attribute.
So basically in.
Posix message queues.
A prefix of underscore is used for all the system calls.
So let's see.
View of the system calls.
Open.
This is a system call used to open a message queue.
This system call is similar to the open system call that we have discussed to open a regular file.
So in this case, we can open a existing message queue or in case we can create a message queue as well.
NQ underscore send is used to send the data on the message queue.
MQ underscore receive is used to receive the messages on the message queue.
MQ close is used to close the message queue descriptors.
And Unlink is finally used to delete the message queue.
Now let's see the syntax of.
Open underscore open system call.
So we need to include the required header files.
And the open can be used in two different ways.
Firstly, we can either specify only the name of the message queue followed by the O flag.
This is this.
The first one is used when we are just opening the existing message queue and this is the second format
in which.
If we are creating a new message queue using a flag, then we have to specify the mode and the attribute
in which we are creating a new message queue.
Let's discuss on this in the next slide.
So if on successful it, it returns.
A the message queue descriptor.
Whereas if there is a error, it returns a minus one.
So if we recall that when we open a regular file using open system call the open system call was returning
a file descriptor.
In the same way, when we open a message queue, we we get a message queue descriptor and this is of
type message queue descriptor.
And the name here.
When we specify the name, it should always contain with the.
It should start with a slash, for example.
Slash McHugh slash.
So Slash has to be present and then followed by the name of the message queue.
Now let's see the next parameter.
That is the O flag.
So here the O flag specifies in which mode we are opening the message queue.
For example, one of the below three modes has to be specified.
Like when we specify only then we are opening the message queue in read only mode only refers to the
write only mode and refers to the read and write mode.
Now if if we have to.
Create a new message queue which is not present.
Then we have we can specify or create this.
These one of the flags can be ordered with the existing one.
So.
So we if you see this, this is similar to the open system call what we have referred to the regular
file so we can apply the concepts of the open system, call for regular files in message queue as well.
So if we are going to create a new message queue, then we we can we have to or the O flag with o create.
And by default the message queue is opened in a blocking mode and if we do not need a blocking mode
then we have to specify non block.
For more information, we can refer to the manual pages on Open.
Let's open the manual pages for this.
So.
So we have to specify.
Man three For the open system call.
You can see that we have to include the required header files.
And when we are not opening the when we are not creating a message queue, we can specify only the first
two parameters, but when we are creating a new message queue, then we we will be specifying a or create
and then followed by the mode.
So here mode refers to the permission in which the message queue is going to be created.
For example, user group and others mode.
So user can have read, write and group can have read write.
This permission is similar to the regular file permission what we had discussed.
We will see more with the example.
So as a note, if you see here, we need to link the object file with minus L, so we have to link with
the library.
Minus l option should be specified while compiling the code which contains the message queue postfix
message queue.
Now let's see the attributes of the message queue.
So the attributes these these are the attributes of the message queue.
So a few of few of the important attributes are underscore max message, underscore, message size,
MQ underscore queue or messages.
So in this MQ underscore max message refers to the maximum number of messages that is allowed in the
message queue.
MQ underscore msg size refers to the maximum size of each message in bytes and QR messages refers to
how many messages are present out of the.
Out of the maximum messages.
So Max message refers to the maximum number of messages in the queue.
Message size refers to the maximum size of.
Size of a message and it is in terms of bytes and messages is a total number of messages currently in
the queue.
So if we can see the analogous of a message queue, we can consider this box as a message queue.
This is only for analogous purpose, so we can consider this as one message queue and this is the underscore
max message.
So here we can say 1234, five, six, seven, eight, nine, ten, 11, 12, 12.
Maximum messages are present and each message.
So this horizontal represents to the each messages.
So the maximum message size in terms of bytes.
So here one, two, three, four, five is the max message size and each entry is a one complete message.
So this is the difference between the Fifo or a pipe and a message queue.
So message queue is a message oriented, whereas in the Fifo or a pipe it was a byte oriented in which
each byte of data is read.
But in this message queue a complete message is read from the message queue.
Now let's see the system call that is used to send the data onto the message queue.
MQ underscore send is used to send the data, whereas underscore receive is used to receive the data
on the message queue.
So when we send the data we have to specify the message queue descriptor and the buffer in which the
message will be received.
And what is the maximum length of the data that is received.
So in this case it is we are sending the data.
So the this contains this contains the pointer to the buffer in which the data is present and length
is the maximum, the length of the length of the data that needs to be sent.
And also, if you note here, we can specify the priority of each messages.
So each message we can set the priority zero is the least priority and the higher number has the higher
priority.
So so if message underscore send is successful, it returns a zero.
If MQ send fails, then it returns a minus one.
Similarly, to receive the data on the message queue, we have to specify.
Underscore, receive and we have to specify the message queue descriptor.
And in this case we are.
Since we are receiving the data, we need to specify the pointer to the buffer and what is the length
of the message queue that we are about to receive.
And we have to specify the priority of of which message we want to receive.
So here the number of bytes in received on success.
So whenever this message queue underscore receive is success, it returns the actual number of bytes
read.
If underscore receive fails, then it returns a minus one.
Now the same way.
What We close a regular file.
We have to close a message queue as well once the operations are performed.
So we have to use underscore close to close the message queue.
Now let's go through the examples in order to understand better.
Here we are going to refer to the send send message program here.
So let's.
Open this.
This is a program in which we can send a message on to the message queue.
So if you see here, we begin with the main function.
And we we have a variable of.
Imcd and score t Q and we have the attributes.
This is the attributes of the message.
Q And now here the message Q is open from the command line arguments.
So the command line argument from the command line argument, we have to specify the message queue name
and we have to specify the message that we are about to send When we execute the program, we can understand
more better.
So here MQ open using the MQ open we are.
Opening the message queue.
Since we are using a create, that means if that message queue is not present, we are going to create
a new message queue and the message queue name is specified in the command line argument argv of one.
And in this case, if the message queue is not present then we are opening the message queue in read
write for user read write for group and no permission for others.
So this is similar to the regular file that we open.
Right passing a null.
That means we are giving a default attributes.
We want a default attributes to be considered.
Now the open returns the queue of type underscore T, and if it returns a minus one, that means it's
a error and upon success we proceed further.
Now, since we have passed a null here, we can get the default attributes and in order to get the attributes
we use the function name called as underscore get attributes and we have to pass the message queue descriptor.
So underscore open.
Returns a message queue descriptor and we have to pass the message queue descriptor for which we need
to get the attribute and we have to pass the address of the attributes mq underscore attr.
So this fills the data structure of MQ underscore getattr fills the data structure of underscore attr
type.
Now here we.
Print the default maximum message and message size of the message queue.
And now, finally, we are going to send the data onto the message queue.
Now let's see the receiving end.
So once seeing the receiving receiving end of the program, let's run both hand in hand the sender program
and the receiver program.
So this is the receiver program.
So this in the receiver program also, we are taking the command line argument in the picture.
So now the the program in the in the program, we are opening the message queue that is specified by
of one.
That means the user specifies the message queue through command line argument.
And in this program we are just opening in read only mode and and also the null is the default attributes
and.
And we are getting the attributes.
This getting the attributes is not required in the receiving.
It is just for information purpose.
So now we have to receive the data.
So in order to receive the data we are taking msg underscore pointer which is of a character pointer.
Now we allocate the memory that is the maximum size from the attribute.
We allocate the memory so each message can be of maximum byte length is MSG size.
So we are allocating a memory and the pointer allocated is given to MSG underscore pointer.
So this is the if.
If msg pointer is null, that means garlic has failed.
Now let's use the underscore receive function call in order to receive the message onto the queue.
So queue is the message queue descriptor that is obtained after opening the message queue and we have
to pass the message queue descriptor.
And the second parameter is the pointer to the buffer and we are specifying the length in which the
message length and finally this received received is nothing but the number of the bytes that is received.
And if number of bytes is received is minus one, that means there is an error while receiving.
And finally we print.
We print the message that is received.
So now let's compile both of the programs.
So we have.
We have a sender program here and a receiver program here.
So now let's compile it.
So you see.
Finn.
And we have to use minus L.
Minus.
Oh.
Send.
And now we have created an object file.
Send here and let's compile this as well.
You minus.
Minus one.
Ah, I see.
Let's call the object file as receive.
Let's say now we have send and receive.
So we have the send and receive object files.
So now let's send the data in the send.
Then we have to specify the.
This is the message queue name.
So the message queue always starts with slash and then the message.
So we are typing the message that we want here.
Now before sending let's run this as well.
Receive.
Then we have to specify the same message queue name on which we are about to receive.
So now let's say I run the receive program first and now I am running the sending program onto the message
queue.
So if you see here, this is my data that has been sent on the message queue and the received message
in the message queue is Welcome to LSP.
So this is the same message that is sent on the sending data.
Also, if you note that in the sender program, the maximum messages is ten.
That means we can, if we go back to this figure.
So we can have by default Max Message size.
Max message is ten and each message can take up to eight bytes.
So this is the default parameters.
So if we want to set any other values other than this, we can set the attributes.
So one more point to note here is if we go back, if we go to this location slash Dave slash MQ, this
specifies all the message that is present here.
So just now we have created a new message queue called as new and it is present here and if you see
here, it is created in six six mode.
That is nothing.
But let's open the program.
So if you see here, this is the permission mode in which we have created that is six is nothing but
the user group and zero for others.
So six, six zero.
So we can see that we have created in 660 mode.
So now we are able to demo that.
We are sending the message from one process and receiving the message on the other process.
And as one more note here.
Here while sending the message We are specifying a zero here.
This last parameter is the priority in which we send.
So we can specify the different priorities for different messages.
And based on the priority, the messages can be received in the receiving end.