problem with msgrcv() when executed for a second time
Hi Again,
I use message queues to communicate data between two processes, namely, A and B.
A basically sends a message to B using msgsnd() as follows (the standard steps):
struct myMsg
{
long int mtype;
char mtext[1500];
} myBuf;
// Set the fields in myBuf here ...
myBuf.mtype = 1;
...
// Create the message queue if it does not exist
mqId = msgget(mqKey, IPC_CREAT | IPC_EXCL | 0660 );
// buf is struct msqid_ds, mqKey is set using ftok()
buf.qbytes = 200000; // size of the message queue
...
// resize the message queue to a non-default one
msgctl(mqId, IPC_SET, buf);
while(1)
{
msgsnd(mqId, &myBuf, sizeof(struct myMsg), 0);
}
***********************************************
B receives the messages the following way (the standard steps):
struct myMsg
{
long int mtype;
char mtext[1500];
} myBuf;
// mqKey is the same as the one returned by ftok() in A process
// Open the message queue if it exists
mqId = msgget(mqKey, IPC_CREAT | 0660 );
while(1)
{
msgrcv(mqId, &myBuf, sizeof(struct myMsg), 1, MSG_NOERROR);
}
*************************************************
A is always started before B.
When I first run A and B after booting the system, they work fine -- I can observe it.
However, after I stop them and "ipcrm" the message queue without rebooting the system, if I start them again, A works fine, however, msgrcv() in B crashes even without returning -1. (I can verify that the execution reaches the msgrcv() by checking the application logs)
Any idea ? If I missed any information required, please feel free to ask.
Thanks.
|