IPC キューを介して互いにメッセージを送受信する 2 つのプログラムがあります。ただし、msgrcv 関数は、キューを介して実際に送信されたものを受信する代わりに、空白のメッセージを取得することがあります。うまくいくと思われる修正をコメントアウトしましたが、ここで確認して、これが msgrcv と msgsnd を使用する適切な方法であるかどうかを確認したかったのです。
msgrcv:
int readqueue(int qid, long type, char *msg)
{
int retval;
// Current fix for blank messages
/* strcpy(MsgQueue.Message, "");
while (strcmp(MsgQueue.Message, "") == 0)
{
retval = msgrcv(qid, &MsgQueue, MSGSIZE, (long)type, 0);
if (strcmp(MsgQueue.Message, "") == 0)
printf("msgrcv fail\n");
}*/
retval = msgrcv(qid, &MsgQueue, MSGSIZE, (long)type, 0);
strcpy(msg, MsgQueue.Message);
return retval;
}
メッセージ数:
int sendqueue(int qid, long type, char *msg)
{
struct msqid_ds stat_buf, *pstat_buf;
int av, retval;
pstat_buf = &stat_buf;
av = 0;
/* Make sure there's space in the queue */
do
{
retval = msgctl( qid, IPC_STAT, pstat_buf);
if (retval == -1)
{
fprintf(stderr, "msgctl in sendqueue failed! Error = %d\n", errno);
return retval;
}
} while ( pstat_buf->msg_qbytes - pstat_buf->msg_cbytes == 0);
strcpy(MsgQueue.Message, msg);
MsgQueue.MsgType = (long)type;
retval = msgsnd(qid, &MsgQueue, MSGSIZE, 0);
memset(MsgQueue.Message, '\0', MSGSIZE-1);
return retval;
}