#include <iostream> #include <fcntl.h> #include <sys/stat.h> #include <mqueue.h> #include <unistd.h> #include <string.h> using namespace std; const string QueueName{"/DemoQueue"}; int main() { int flags {O_RDONLY | O_CREAT}; int mode {S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP}; mqd_t queue = mq_open(QueueName.c_str(), flags, mode, nullptr); if(queue == -1) { perror("mq_open:"); return 1; } struct mq_attr newAttrib; mq_getattr(queue, &newAttrib); size_t maxLength = newAttrib.mq_msgsize; char * msg = new char[maxLength]; size_t len; unsigned int prio; bool done = false; while(not done) { ssize_t len = mq_receive(queue, msg, maxLength, &prio); if (len == -1) { perror("mq_receive"); done = true; } else { msg[len] = 0; cout << "Got a message of length " << len << " and priority " << prio << endl; cout << msg << endl; if (strcmp(msg,"stop") == 0) { done = true; } sleep(1); cout << endl; mq_getattr(queue, &newAttrib); cout << "There are " << newAttrib.mq_curmsgs << " in the queue "<< endl; } } mq_close(queue); // look at /dev/mqueue mq_unlink(QueueName.c_str()); delete msg; return 0; }