Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use mqueue in a c program on a Linux based system?

Tags:

c

linux

ipc

mqueue

How do I use mqueue (message queue) in a c program on a Linux based system?

I'm looking for some good code examples that can show how this is done in a correct and proper way, maybe a howto.

like image 526
Johan Avatar asked Jun 16 '10 18:06

Johan


People also ask

How does Message Queue work in Linux?

A message queue is a linked list of messages stored within the kernel and identified by a message queue identifier. A new queue is created or an existing queue opened by msgget(). New messages are added to the end of a queue by msgsnd().

Can we use queue in C?

A queue can be implemented with any programming language such as C, Java, Python, etc.

Is there a queue library in C?

C is not an object-oriented language, and it doesn't have standard libraries for things like queues.


2 Answers

The following is a simple example of a server that receives messages from clients until it receives an "exit" message telling it to stop.

The code for the server:

#include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <errno.h> #include <mqueue.h>  #include "common.h"  int main(int argc, char **argv) {     mqd_t mq;     struct mq_attr attr;     char buffer[MAX_SIZE + 1];     int must_stop = 0;      /* initialize the queue attributes */     attr.mq_flags = 0;     attr.mq_maxmsg = 10;     attr.mq_msgsize = MAX_SIZE;     attr.mq_curmsgs = 0;      /* create the message queue */     mq = mq_open(QUEUE_NAME, O_CREAT | O_RDONLY, 0644, &attr);     CHECK((mqd_t)-1 != mq);      do {         ssize_t bytes_read;          /* receive the message */         bytes_read = mq_receive(mq, buffer, MAX_SIZE, NULL);         CHECK(bytes_read >= 0);          buffer[bytes_read] = '\0';         if (! strncmp(buffer, MSG_STOP, strlen(MSG_STOP)))         {             must_stop = 1;         }         else         {             printf("Received: %s\n", buffer);         }     } while (!must_stop);      /* cleanup */     CHECK((mqd_t)-1 != mq_close(mq));     CHECK((mqd_t)-1 != mq_unlink(QUEUE_NAME));      return 0; } 

The code for the client:

#include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <mqueue.h>  #include "common.h"   int main(int argc, char **argv) {     mqd_t mq;     char buffer[MAX_SIZE];      /* open the mail queue */     mq = mq_open(QUEUE_NAME, O_WRONLY);     CHECK((mqd_t)-1 != mq);       printf("Send to server (enter \"exit\" to stop it):\n");      do {         printf("> ");         fflush(stdout);          memset(buffer, 0, MAX_SIZE);         fgets(buffer, MAX_SIZE, stdin);          /* send the message */         CHECK(0 <= mq_send(mq, buffer, MAX_SIZE, 0));      } while (strncmp(buffer, MSG_STOP, strlen(MSG_STOP)));      /* cleanup */     CHECK((mqd_t)-1 != mq_close(mq));      return 0; } 

The common header:

#ifndef COMMON_H_ #define COMMON_H_  #define QUEUE_NAME  "/test_queue" #define MAX_SIZE    1024 #define MSG_STOP    "exit"  #define CHECK(x) \     do { \         if (!(x)) { \             fprintf(stderr, "%s:%d: ", __func__, __LINE__); \             perror(#x); \             exit(-1); \         } \     } while (0) \   #endif /* #ifndef COMMON_H_ */ 

Compiling:

gcc -o server server.c -lrt gcc -o client client.c -lrt 
like image 50
the_void Avatar answered Sep 20 '22 10:09

the_void


#include <stdio.h> #include <fcntl.h> #include <mqueue.h>  int main(int argc, char *argv[]) {     mqd_t mq;               // message queue     struct mq_attr ma;      // message queue attributes     int status = 0;     int a = 5;     int b = 0;      printf("a = %d, b = %d\n", a, b);      // Specify message queue attributes.     ma.mq_flags = 0;                // blocking read/write     ma.mq_maxmsg = 16;              // maximum number of messages allowed in queue     ma.mq_msgsize = sizeof(int);    // messages are contents of an int     ma.mq_curmsgs = 0;              // number of messages currently in queue      // Create the message queue with some default settings.     mq = mq_open("/test_queue", O_RDWR | O_CREAT, 0700, &ma);      // -1 indicates an error.     if (mq == -1)     {         printf("Failed to create queue.\n");         status = 1;     }      if (status == 0)     {         status = mq_send(mq, (char *)(&a), sizeof(int), 1);     }      if (status == 0)     {         status = mq_receive(mq, (char *)(&b), sizeof(int), NULL);     }      if ((status == 0) && (mq_close(mq) == -1))     {         printf("Error closing message queue.\n");         status = 1;     }      if ((status == 0) && (mq_unlink("test_queue") == -1))     {         printf("Error deleting message queue.\n");         status = 1;     }      printf("a = %d, b = %d\n", a, b);      return status; }  
like image 21
Amardeep AC9MF Avatar answered Sep 21 '22 10:09

Amardeep AC9MF