I guess this
if ((msqid = msgget(key, 0666)) ==key-1)
should be
if ((msqid = msgget(key, 0666)) == -1)
From msgrcv
ERRORS
The msgrcv() function will fail if:
...
[EINVAL]
msqid is not a valid message queue identifier.
Furthermore, message_buf.m
must not be a pointer but a member
typedef struct msgbuf1
{
long mtype;
M1 m;
} message_buf;
You can then save this call to malloc
rbuf->m=malloc(sizeof(M1));
and the call to msgrcv
should be
if (msgrcv(msqid, rbuf, sizeof(rbuf->m), 1, 0) < 0)
because otherwise, msgrcv
will overwrite your stack.
Update:
From msgget
ERRORS
[ENOENT]
A message queue identifier does not exist for the argument key and (msgflg & IPC_CREAT) is 0.
This means, you must call
if ((msqid = msgget(key, IPC_CREAT | 0666)) == -1)
at least for the first time you call this function.