0

谁能告诉我如何在消息队列的“msgrcv”函数中使用 MSG_EXCEPT 标志,例如?

我正在尝试这样做,但它会给我一个错误,例如:MSG_EXCEPT undeclared 我已经输入了“msgrcv”函数的所有头文件。

请用示例代码给我解决方案。

我正在上传接收方的示例代码。

Receiver.c

#include <stdio.h>
#include <sys/msg.h>
#include <error.h>
#include <strings.h>
#include <mqueue.h>
#include <sys/ipc.h>
#include <sys/types.h>

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),4, MSG_EXCEPT | MSG_NOERROR | IPC_NOWAIT)==-1)
  {  
     perror("msgrcv");           
  }

  if(num_messages==0)
  {
        printf("Queue is empty\n");
  }
  else
  {     
    printf("%s \n", msg.text);
  }

  return 0;
}
4

1 回答 1

0

MSG_EXCEPT 的问题在于它依赖于一个记录不充分的特性,因为它需要定义 _GNU_SOURCE。

如果你用你编译gcc -D_GNU_SOURCE <and all your usual compiler switches> Receiver.c你应该会发现问题会得到解决。

有关这方面的更详细说明,请参阅未定义的 MSG_EXCEPT

于 2014-07-27T14:40:41.323 回答