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

  • SEARCH
  • Home
  • 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 104541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:17:52+00:00 2026-05-11T01:17:52+00:00

I have the following application which replicates an issue I’m having in a larger

  • 0

I have the following application which replicates an issue I’m having in a larger application with system v message queues. basically, the main function generates a key, then creates a message queue with msgget(). Then 3 forks are spawned, each with a different id. each of them runs msgrcv with a different posative number (so they are waiting for different messages).

Main then sleeps for a few seconds a sends a message to id = 3. However it isn’t the third thread that wakes up but a different one instead. This code is completely isolated so you can try it out yourself. What’s wrong with this code?

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/wait.h>  struct dummy_struct {     long mtype;     char message[255]; };  int msg_queue_id; void recv_thread(int id);  int main(int argc, char **argv) {     int i;     key_t key;     struct dummy_struct dummy = { 3, 'hello' };      //create a unique key     if (key = ftok('/mnt/mydocuments/code/sys_v_fork_test/main.c', 'a') == -1)     {         printf('ftok didn't work\n');         exit(1);     }      //create the unix sys 5 message queue     if ((msg_queue_id = msgget(key, 0644 | IPC_CREAT)) == -1)     {         printf('msgget failed\n');         exit(1);     }     else         printf('my message queue id: %i\n', msg_queue_id);      //fork off multiple recievers     for (i = 1; i < 4; i++) // <- NOTE: 1 -> 4     {         if (fork() == 0)             recv_thread(i);     }      printf('sleeping\n');     sleep(5);      //wait a little then send a message      printf('sending message\n');     if (msgsnd(msg_queue_id, &dummy, sizeof(struct dummy_struct), 0) == -1)     {         printf('msgsnd failed\n');     }     printf('main thread exiting');     _exit(0); }  void recv_thread(int id) {     struct dummy_struct dummy;      printf('recv_thread with id: %i\n', id);      if (msgrcv(msg_queue_id, &dummy, sizeof(struct dummy_struct), id, 0) == -1)         printf('error in msgrcv\n');     else         printf('thread %i got %s back\n', id, dummy.message); } 

If I wait for 2 that means messages whose struct contains a mtype set to exactly 2. 3 for 3 and so one. My point of reference was this guide: http://www.ecst.csuchico.edu/~beej/guide/ipc/mq.html. Can anyone help please? (you may need to modify the ftok line of code to point to a valid file on your own machine to test successfully). I’m running Fedora 10 on an EeePC 1000H

  • 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. 2026-05-11T01:17:53+00:00Added an answer on May 11, 2026 at 1:17 am

    Ahh think i’ve fixed it. It’s because I was using an int rather than a long for the first member set in the structure for the ‘mtype’. passing in { 1l, ‘hello’ } instead and changing the definition of i to long seems to have fixed it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You could add the user's username as a tracking/segmentation) variable...… May 11, 2026 at 8:53 am
  • added an answer Best practices with AS eventlisteners are: Make it weak (as… May 11, 2026 at 8:53 am
  • added an answer I have amended your code and produced a working example.… May 11, 2026 at 8:53 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.