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