question about msgrcv()

Randall G Chan chanr@us.ibm.com
Mon Dec 18 15:37:00 GMT 2000


Sorry about that last message.  Accidently hit the send button.

I had a question about msgrcv().  If msgsz argument is supposed to specify
the max size of the message to be recieved, why would this return an errno
of E2BIG when I am asking for the sizeof(mymsg_t) - sizeof(long)?  The
message size is in fact that,1024, however, when ipcs is run, it indicates
1028 (obviously including the size of the mtype).  I was wondering if this
was by design or am I just missing something.  This obviously works if I
specify MSG_NOERROR, however, I may not want to truncate the data after.

Thanks,
Randy Chan
Mylex - An IBM Company
Software Engineer
chanr@us.ibm.com

--------------------------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>

typedef struct mymsg
{
    long a;
    unsigned long b[256];
} mymsg_t;

typedef struct mymsg2
{
    long a;
    unsigned long b[512];
} mymsg2_t;

int main(void)
{
    key_t           key   = 0x00ABCDEF;
    int             msqid = 0;
    mymsg_t         sndmsg;
    mymsg2_t        rcvmsg;
    int             i;
    int             szmsg = 256;

    memset(&sndmsg, 0, sizeof(sndmsg));

    for(i = 0; i < 512; i++)
        rcvmsg.b[i] = 0xFFFFFFFF;

    sndmsg.a = 0x22;
    sndmsg.b[szmsg - 3] = 0x20;

    if((msqid = msgget(key, IPC_CREAT)) == -1)
    {
        perror("msgget()");
        exit(-1);
    }

    if(msgsnd(msqid, &sndmsg, sizeof(mymsg_t), 0) == -1)
    {
        perror("msgsnd()");
        exit(-1);
    }

    printf("%lX\n", rcvmsg.b[szmsg]);

    if(msgrcv(msqid, &rcvmsg, sizeof(mymsg_t) - sizeof(long), 0, 0) == -1)
    {
        fprintf(stderr, "errno = %d\n", errno);
        perror("msgrcv()");
        exit(-1);
    }

    printf("%lX\n", rcvmsg.b[szmsg]);
    printf("0x%lX\n", rcvmsg.a);
    printf("%lX %lX %lX %lX %lX %lX %lX %lX\n",
           rcvmsg.b[szmsg - 4], rcvmsg.b[szmsg - 3], rcvmsg.b[szmsg - 2],
           rcvmsg.b[szmsg - 1], rcvmsg.b[szmsg],     rcvmsg.b[szmsg + 1],
           rcvmsg.b[szmsg + 2], rcvmsg.b[szmsg + 3]);

    if(msgctl(msqid, IPC_RMID, 0) == -1)
    {
        perror("msgrcv()");
        exit(-1);
    }

    return 0;
}



More information about the Gcc-bugs mailing list