0
我有兩個程序通過IPC隊列來回發送和接收消息。但是,有時msgrcv函數會收到一條空白消息,而不是接收實際通過隊列發送的內容。我已經註釋了一個我認爲應該可以工作的修復程序,但我想在此查看,看看這是否是使用msgrcv和msgsnd的正確方法。msgrcv獲取空白郵件
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;
}
的msgsnd:
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;
}
msgrcv接收128字節,這是爲隊列消息設置的最大字節大小。然而,字符串似乎仍然不時出現空白。 – Zac 2011-05-04 21:48:14