Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 877125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:33:16+00:00 2026-05-15T11:33:16+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:33:17+00:00Added an answer on May 15, 2026 at 11:33 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 476k
  • Answers 476k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this instead: <input type="submit" onclick="this.disabled='disabled';form.submit()"> Edit: Added form. as… May 16, 2026 at 4:55 am
  • Editorial Team
    Editorial Team added an answer No, there is not. If you are using setMovementMethod(LinkMovementMethod.getInstance()); in… May 16, 2026 at 4:55 am
  • Editorial Team
    Editorial Team added an answer That site is incorrectly gzipping the response regardless of the… May 16, 2026 at 4:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.