0

Can any one tell me how to use MSG_EXCEPT flag in "msgrcv" function on message queue with example?

I am trying to do it but it will give me an error like : MSG_EXCEPT undeclared I have entered all the header files for "msgrcv" function.

please give me solution with sample code.

I am uploading my sample code of receiver side.

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;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

The problem with MSG_EXCEPT is that it relies on a poorly documented feature in that it needs _GNU_SOURCE defined.

If you compile with gcc -D_GNU_SOURCE <and all your usual compiler switches> Receiver.c you should find that the problem will be solved.

For a more detailed explanation of this see MSG_EXCEPT not defined.

Kev Scott
  • 21
  • 5