C++ Programming on Linux - POSIX Message Queue Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is hui welcome to watch my video c plus plus programming linux in this short video we are going to demonstrate another very popular message queue mechanism post-6 message queue so the power 6 message queue allow process to exchange date in the form of message this api is distinct from that provided by csmv message queue so we have made a short video previous video to demonstrate how to create a message to communicate between process using system away message queue so this is a power 6 message queue and distinct from the system way message queue but provide a similar functionalities so here under linux so we create a similar uh program called a mq send which is going to send a message so this to in order to use posix mysql we have included mq.h fcnd dot h and the c states dot h and this because this is a function to open the message queue is using the imq open and so this function needs these three hydra files so and we have to include one two three header files so this is a very simple program so we define the queue name called myq and permit six four four maximum message is 16 and the message size we just defined the 1 0 24 so this is a maximization q it is a message size in the queue so for declare this is we have to declare the structure mq underscore attr which is the queue attribute so here on the linux and the specific scrub we need a miq attr type which is a queue attribute and the m q d underscore t is the q descriptor so it's q descriptor so we have q descriptor called mq ds which is a mq code type and we declare buffer which is the size and the priority and [Music] politics mysql has a priority so you can send the message with the priority number so from lower to upper so upper is always in front of a lower message so what we do first we set up the attribute which is the maxim message and the queue size and we just set the default flag is zero and we set up a memory called buffer so first one we just easily just use an mq open to open or create a message queue so for this function in the four parameters first is the queue name which is the way defined called my underscore queue and the flag second is called create read write and no block and the third parameter is the permission it's q permit we have defined and last one is the attribute which we set up here before we call this so if this mq open correctly so we just from the command console using gateline function to get the input message into the buffer and we call the other function called synth to send message to put the message into the queue so this function in the first parameter is the mq descriptor and buffer which we get the input of the date and the straight line the buffer which is the side length of the message and priority in this demo we didn't put a priority so and if our input of the message is called end we just get after from loop and after we get up loop we send the end to the our receiver and we close the queue so this is our sending and we have created another one called mq receiving this is our receiving so our receiving using the same queue name same permit same messages size and the same the maximum message and so in the receiving part first we still need to open and the queue name and the flag is o and score rd only it means read only and the permit is same and the attribute exactly same as we set up so if we open correctly we make a loop so if attribute attr mq current messages not equal zero what that means it means there is a message in our queue so we set up a memory and we use mq receiving and read our queue which is mqt scripter which we get from open and the buffer and the into the buffer and the maximum message size and the priority and the return is the real number byte we read so if number by reader greater than zero which means we read some message from our queue and we just print the byte rate how many bytes we read and what message we read if the message was received is the end which is a break loop and yeah get out from look exit from loot loop and then we close the messenger queue so this is our sending and the receiving service compile this and then make so we just created two terminal top we are going to do the receiving so now we are receiving we just read under the bottom one we are going to do the sending so power 6 message queue every time we make a crater queue it will be defined in the dive m-q-u-e so this is the cure we created here so if we make a tail q we can see currently there's no message in the queue there's no notification in the queues so here if we goes to send so we are is sending hello this is my test message one so you can see the total byte received is 31 bytes and this is a hello this is a hello this is my so this is this is my message too so we send another one this is the 20 and the message too so we send the end so we terminated the sending and terminated receiving so this is how we using message queue and actually it's a pretty easy and for the power 6 message queue we have another way to for the receiving we can using the notice and the reading to read the message so let's say the message queue for that is so here it's uh we create another way to go so we put a receiving into a function called a thread function which is we can also go inside the thread function we just okay so in the main function for the main function we still declare that my mq descriptor and attribute for exactly for the this way we have created a signal event and called using signal invent which is uh we're using the signal to trigger the threading to starting our receiving so what we do we his naturalized attribute setup attribute exactly same as the way our example and we open uh mq and the user mq open which q name read only permit and attribute so if this is open is okay what we're going to do we set up our signal event so our signal is aside even thread so it's a signal event so for using signal we can register a notification messenger is available so we the this one we can use in the signal event so signal we can have signal known signal event signal we can give the number and the signal slide so upon the messenger delivery invoke a signal event notification function that is starting invoke a function so what we do so we set up event and this this is the signal and this is the threading function which is a declared function for receiving an attribute the signal attribute we just set up now and we pass the parameter which is the address of a reference of our mq descriptor so after we set up here we just use mq notify to register our receiving is reading so once messenger in the message queue is ready they will invoke this function thread function in this stride function we are going to do the uh you know make a loop set up buffer receiving message and the printing message once the message is receiving is the end we just exit so this is another way for the for the posix message queue so we save this we make clean and then make recompiling so now we start a mq notification so this is uh we are going to this is always not starting our trading ad because we are reading we register our notice so let's see currently let's check our queue you can see currently our queue size is zero empty but we have a notification registered so now we register ready for receiving so now it's in our sending and we starting sending so we start our sending so this is my test so you can see after we put the message into queue and the message in the queue is available then our receiving and triggered trading function and we read this 31 byte this is the message and so let's say this is message tool so we can receive the message and the end we can terminate both receiving and the sending hello this is hui thanks to watch my video hopefully this is useful enjoyable it's going to be great to have your feedback
Info
Channel: Hui Yuan
Views: 468
Rating: 5 out of 5
Keywords:
Id: xNUdIveEQxI
Channel Id: undefined
Length: 13min 26sec (806 seconds)
Published: Fri Feb 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.