In the previous post we saw the various functions available for creation of message queues.
Now let us look at an example for creating and communication using message queues.
The first step is to create a message queue using mq_open
Note that the name of the message queue begins with a "/" which is mandatory.
The flag O_CREAT signifies that the queues is being created new, and we are not opening a previously existing queue.
0666 gives read and write permissions to every one
We are creating the queue with default attributes hence leaving the last argument as NULL.
If a process wants to recieve data from the queue, it will need a buffer into which the data will have to be placed, hence we create a buffer for 10 characters as follows.
To recieve data from the queue we need to make use of the function mq_receive, which requires the attribute, "message size", of the queue to be passed to it.
To get the message size of the queue we created we need to make use of the function
mq_getattr as follows.
We allocate memory for the structure and then pass the queue descriptor and the structure pointer to mq_getattr, which fills the strucutre with the relevant data.
Now we can get the message size from the structure by accessing the mq_msgsize field, i.e. attr1->mq_msgsize.
Now we can call mq_recevie to get data from the queue .
The full code would look as follows.
recieve.c
Now we need a process to send the messages into the queue.
To send messages we again need to open the queue, but no create it. Hence the call to mq_open would be
Note that the name of the queue, test_q, is same as the one we used while creation making sure that we are sending and receiving on the same queue.
Let us create a buffer having data that has to be sent.
To send data we make use of the function mq_send.
The full code will look as follows
send.c
To see the queue in operation, open a terminal and compile the above two programs.
Note that for compiling these we need to pass the option "-lrt" to the compiler.
Open two terminals and go the directory where the above executable were created.
In the first terminal execute the recieve program
The terminal should go into a wait state, waiting for a message. (It is a blocking receive)
Open the second terminal and execute the send program
As soon as the send program is executed, the terminal waiting in the receive program will recieve the message, and print the same before exiting.
Thus the two processes could communicate with each other using the message queues.
In the future posts we will see how can the message priority be used.
Are you having trouble in receiving the bitcoin from other wallets in Blockchain account? Is the error troubling you and there’s nothing you can do about it? If you don’t know how to handle all these queries and looking for solutions, you can always ask for the team to get solutions that are easy to execute. Connect with the team for better results and users can have conversation with the team anytime for providing quality solutions via calling on Blockchain support phone number (833) 993-0690. Whenever you are in doubt, you can approach the team and avail quality-driven solution.
ReplyDeleteMore info visit here-
Blockchain support number
Crypto customer support number