Skip to content

Instantly share code, notes, and snippets.

@avasyukov
Created December 12, 2019 13:15
Show Gist options
  • Save avasyukov/d6b69cb297980d5a6ef6c4819b597441 to your computer and use it in GitHub Desktop.
Save avasyukov/d6b69cb297980d5a6ef6c4819b597441 to your computer and use it in GitHub Desktop.
mq limits test
#include <stdio.h>
#include <mqueue.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <sys/stat.h>
int main(int argc, char** argv)
{
int message_size = argc > 1 ? atoi(argv[1]) : 16;
int queue_size = argc > 2 ? atoi(argv[2]) : 10000;
int queue_count = argc > 3 ? atoi(argv[3]) : 10;
mqd_t* queue = new mqd_t[queue_count];
struct mq_attr initial_attributes;
initial_attributes.mq_msgsize = message_size;
initial_attributes.mq_maxmsg = queue_size;
int open_flags = O_RDWR | O_CREAT | O_EXCL;
int permissions = S_IWUSR | S_IRUSR;
for(int i = 0; i < queue_count; i++) {
printf("Trying to open queue #%d with msgsize: %d, and maxmsg: %d\n", i, message_size, queue_size);
char name[64];
snprintf(name, 64, "/message_queue_test_%d", i);
queue[i] = mq_open(name, open_flags, permissions, &initial_attributes);
if(queue[i] == -1)
{
printf("Cannot open message queue\n");
printf("Errno: %d [%s]\n", errno, strerror(errno));
}
else
{
printf("Queue has been opened successfully.\n");
}
}
for(int i = 0; i < queue_count; i++) {
char name[64];
snprintf(name, 64, "/message_queue_test_%d", i);
if(queue[i] != -1)
{
printf("Closing queue\n");
mq_close(queue[i]);
mq_unlink(name);
}
}
delete[] queue;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment