Skip to content

Instantly share code, notes, and snippets.

@filosofisto
Last active December 29, 2017 13:10
Show Gist options
  • Save filosofisto/7d6d6ee35914df6383c2674a1e80ffa0 to your computer and use it in GitHub Desktop.
Save filosofisto/7d6d6ee35914df6383c2674a1e80ffa0 to your computer and use it in GitHub Desktop.
Create and insert data in mq_queue Posix. Tested in Linux (OpenSUSE Tumbleweed) and Solaris 5.11.
cmake_minimum_required(VERSION 3.9)
project(posix_mq_send)
set(CMAKE_CXX_STANDARD 98)
set(GCC_COVERATE_LINK_FLAGS "-lrt")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERATE_LINK_FLAGS}")
add_executable(posix_mq_send main.cpp)
#include <iostream>
#include <cstdlib>
#include <mqueue.h>
#include <errno.h>
#include <cstring>
#ifdef sun
#include <fcntl.h>
#endif
using namespace std;
int main(int argc, char **argv) {
cout << "Message Posix Sample" << endl;
if (argc < 2) {
cerr << "Usage: " << endl
<< "\t" << argv[0] << " <data>" << endl
<< "\tWhere <data> is text for send to queue" << endl;
return EXIT_FAILURE;
}
mqd_t queue;
queue = mq_open("/mq_test", O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR, NULL);
if (queue == static_cast<mqd_t>(-1)) {
cerr << "Error on open queue mq_test: "
<< strerror(errno)
<< endl;
return EXIT_FAILURE;
}
if (mq_send(queue, argv[1], strlen(argv[1]), 0) == -1) {
cerr << "Error on send data to queue: "
<< strerror(errno)
<< endl;
return EXIT_FAILURE;
}
cout << "Message opened success" << endl;
cout << "Message inserted in queue" << endl;
if (mq_close(queue) == -1) {
cerr << "Error on close queue: "
<< strerror(errno)
<< endl;
return EXIT_FAILURE;
}
cout << "Message close success" << endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment