Question

I am implementing message queue in C linux. I am sending an integer = 17 and receiving integer = 0. Please see below and let me know what's wrong with my msgsnd and msgrcv functions. Please give attention to this: will rbuf store data in rbuf->m->msglen or in rbuf->mtype.

In sending process

msgsnd(msqid, sbuf,sizeof(int), 0);
printf("\nmsglen = %d",rbuf->m->msglen);  // 17

In receiving process. Both have same msqid. I have verified it.

msgrcv(msqid, rbuf, sizeof(int), 1, 0);
printf("\nmsglen = %d",rbuf->m->msglen); // 0

//msqid=98305, some valid id

here is my struct definations defined in another file.

typedef struct message1
{
    int msglen;
    unsigned char *cp;
}msg1;

typedef struct msgbuf
{
    long    mtype;
    msg1    *m;
} message_buf;
Was it helpful?

Solution

You are sending a message which contains a pointer to your message1 struct. The receiving process dereferences that pointer, but in that process it does not point to the same thing. In fact I am surprised you didn't get a segfault.

You should define msgbuf like this:

typedef struct msgbuf
{
    long    mtype;
    msg1    m;
} message_buf;

So that the msg1 struct is contained within the msgbuf rather than pointed to by it.

Also, the size you need to specify is sizeof(message_buf), not sizeof(int).

OTHER TIPS

//header files
#include"msgbuf.h"  //where I have put my structures
#define AUTOMATIC 1
#define USER_DATA 2

int main()
{
    int msgflg = IPC_CREAT | 0666,ch,len=0;
    size_t msgsize = 0;
    int msqid;
    key_t key;
    message_buf *sbuf;
    char ans;
    char *data;
    key = ftok("/home/user",15);

    printf("Do you want to send messages\t");
    scanf("%c",&ans);
    getchar();
    if (((ans=='y' || ans=='Y') && (msqid = msgget(key, msgflg )) ==-1))
    {       perror("msgget");
            exit(1);
    }
    else
    fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);

    while(ans=='y' || ans=='Y')
    {
            printf("\n1. Automatic data\n2. Enter data\n3. Exit\nEnter your choice\t");
            scanf("%d",&ch);
            getchar();
            switch(ch)
            {
                    case AUTOMATIC: len=strlen("Did you get this?");

                                    sbuf=malloc(len+sizeof(int));
                                    memset(sbuf, 0, sizeof(message_buf));
                                    sbuf->m=malloc(len+sizeof(int));
                                    memset(sbuf->m, 0, sizeof(msg1));

                                    sbuf->m->msglen=len;
                                    sbuf->m->cp=malloc(sizeof(len));
                                    strncpy(sbuf->m->cp, "Did you get this?",strlen("Did you get this?"));
                                    sbuf->m->cp[strlen(sbuf->m->cp)]='\0';
                                    break;
                    case USER_DATA: printf("\nEnter data\t");
                                    fflush(stdout);
                                    len = getline(&data, &msgsize, stdin);

                                    sbuf=malloc(len+sizeof(int));
                                    memset(sbuf, 0, sizeof(message_buf));
                                    sbuf->m=malloc(len+sizeof(int));
                                    memset(sbuf->m, 0, sizeof(msg1));

                                    strcpy(sbuf->m->cp, data);
                                    sbuf->m->cp[strlen(sbuf->m->cp)]='\0';
                                    sbuf->m->msglen=len;
                                    break;
                    case 3: msgctl(msqid, IPC_RMID, NULL);
                            printf("\nQueue with q id = %d is removed\n",msqid);
                            exit(1);
                    default:printf("\nTRY AGAIN\t");
                            scanf("%c",&ans);
                            getchar();
            }
            printf("\nmsglen = %d\nmsgcp= %s",sbuf->m->msglen,sbuf->m->cp);
             /* Send a message */
            sbuf->mtype=1;
            if (msgsnd(msqid, sbuf,2*sizeof(int), 0) < 0)
            {
        printf("Msg q id= %d\nMsg type= %d\nMsg Text %s\nMsg Len= %d\n", msqid, sbuf->mtype, sbuf->m->cp,sbuf->m->msglen);
                    perror("msgsnd");
                    exit(1);
            }
            else
            printf("\nMessage: %s\n Sent\n", sbuf->m->cp);
    }
    msgctl(msqid, IPC_RMID, NULL);
    printf("\nQueue with q id = %d is removed\n",msqid);
    return 0;
}

Now Receiving code

//header files
#include"msgbuf.h"
int main()
{
    int msqid;
    key_t key;
    int msgflg = 0666;
    message_buf  *rbuf;
    int msg_len_rcvd=0;

    rbuf=malloc(150);
    rbuf->m=malloc(100);
    key = ftok("/home/user",15);
    if ((msqid = msgget(key, msgflg)) ==-1)
    {
            perror("msgget");
            exit(1);
    }
    printf("\n\n%d\n",msqid);
    /* Receive an answer of message type 1.   */
    while(1)
    {
            if ( (msg_len_rcvd=msgrcv(msqid, rbuf, 2*sizeof(int), 1, 0)) < 0)
            {
                    perror("msgrcv");
                    exit(1);
            }
            else
            {
                    printf("\n Number of bytes received:: %d", msg_len_rcvd);
                    printf("\nmtype1 = %d",rbuf->mtype);
                    printf("\nmsglen= %d",rbuf->m->msglen);
            }
            break;
    }
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top