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 4058188
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:02:52+00:00 2026-05-20T15:02:52+00:00

Dear Friends, Any idea why the msgrcv is receiving a blank buffer? Here is

  • 0

Dear Friends,
Any idea why the msgrcv is receiving a blank buffer?

Here is the code:

enter code here
 #include <sys/msg.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>

 typedef struct mymsg {
  long mtype;
  char mtext[24];
 }mymsg;

 int main()
 {
  int msqid;
  mymsg msg,buff;
  msqid=msgget(IPC_PRIVATE,IPC_CREAT|IPC_EXCL);

  if(msqid==-1){
  perror("FAiled to create message queue\n");
  }
  else{
  printf("Message queue id:%u\n",msqid);
  }
  msg.mtype=1;
  strcpy(msg.mtext,"This is a message");
  if(msgsnd(msqid,&msg,sizeof(msg.mtext),0)==-1){
   perror("msgsnd failed:");
  }
  else{
   printf("Message sent successfully\n");
  }
 //ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);

  // msgrcv(msqid,buff.mtext,sizeof(msg.mtext),1,0); This was error
  msgrcv(msqid,&buff,sizeof(msg.mtext),1,0);  // This is correct (Thanks to Erik)
  printf("The message received is: %s\n",buff.mtext);
 }

   Output:
   [root@dhcppc0 message_queue]# ./a.out
   Message queue id:294919
   Message sent successfully
   The message received is: 
                                                    1,1           Top
  • 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-20T15:02:53+00:00Added an answer on May 20, 2026 at 3:02 pm

    msgbuf.mtype must be set to 1 – since you’re telling msgrcv that you want messages of type 1.

    Alternatively, you could set msgbuf.mtype to any positive value, and then tell msgrcv that you want any message type by passing 0 as the msgtyp argument.

    Also, msgrcv expects a pointer to a msgbuf:

    msgrcv(msqid,&buff,sizeof(msg.mtext),1,0);
    

    EDIT: Tested working source:

     #include <sys/msg.h>
     #include <unistd.h>
     #include <sys/types.h>
     #include <stdio.h>
     #include <string.h>
    
     typedef struct mymsg {
      long mtype;
      char mtext[24];
     }mymsg;
    
     int main()
     {
      int msqid;
      mymsg msg,buff;
      msqid=msgget(IPC_PRIVATE,IPC_CREAT|IPC_EXCL);
    
      if(msqid==-1){
      perror("FAiled to create message queue\n");
      }
      else{
      printf("Message queue id:%u\n",msqid);
      }
      msg.mtype=1; // was there failed to copy
      strcpy(msg.mtext,"This is a message");
      if(msgsnd(msqid,&msg,sizeof(msg.mtext),0)==-1){
       perror("msgsnd failed:");
      }
      else{
       printf("Message sent successfully\n");
      }
     //ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
    
      msgrcv(msqid,&buff,sizeof(msg.mtext),1,0);
      printf("The message received is: %s\n",buff.mtext);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear Friends good afternoon. My problem may be this is very basic one i.e.
Dear friends, I need your help. I need to send .bmp file to another
Dear Friends from Stackoverflow, Please help me with a problem that i'm having when
Dear friends, i'm concerned if i'm making a bad use of references in C++
Dear Friends I have a Div of Images. <div class=img_team_container> <div class=img_team_subcontain> <div class=img_team><a
Dear friends, this is a script which simply upload file and insert filename into
hi dear friends and good morning The following question may be complicated and critical
Hi dear friends i have 495 cells and a textField for inserting number and
dear all,this is my code at xampp: $sql = INSERT INTO oqc_defect ; $sql.=
dear all, i'll take some data from the database. and join two tables.the code

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.