在linux中,当消息队列为空时,消息队列的输出应该是什么?linux中的消息队列
首先我在一个消息队列中发送了2条消息,然后我从消息队列收到了2条消息。现在我再次尝试从消息队列接收消息,然后我得到垃圾值。那么告诉我为什么我得到这个垃圾值?
Receive.c
#include
#include
#include
#include
int main()
{
int msqid;
struct message
{
long type;
char text[20];
} msg;
struct msqid_ds buf;
int msgtype = 3;
int num_messages;
int count;
int key = 1234;
msqid = msgget(key,0644);
count = msgctl(msqid,IPC_STAT,&buf);
num_messages = buf.msg_qnum;
printf("Number of messages = %d\n",num_messages);
if (msgrcv(msqid, (void *) &msg, sizeof(msg.text), msgtype, MSG_NOERROR| IPC_NOWAIT)==-1);
{
perror("msgrcv");
}
/* if(num_messages==0)
{
printf("Queue is empty\n");
}
else
{ */
printf("%s \n", msg.text);
// }
return 0;
}
Send.c
#include
#include
#include
int main()
{
struct message
{
long type;
char text[20];
} msg;
int msqid;
int key = 1234;
msqid = msgget(key,IPC_CREAT|0644);
msg.type = 3;
strcpy(msg.text, "This is message 1");
msgsnd(msqid, (void *) &msg, sizeof(msg.text), IPC_NOWAIT);
strcpy(msg.text, "This is message 2");
msgsnd(msqid, (void *) &msg, sizeof(msg.text), IPC_NOWAIT);
printf("2 messages are succesfully send \n");
return 0;
}