-1

this is my code and in the last part,msgrecv does not accept the messages from the queue accourding the correct preiority , eg: 10 is most important to accept then type=20 then type=30 ... that is my goal is to accept messages in this manner ... can anyone show me where is the problem? because the acceptance is accoures without priority .. Thanks.. that is the code

     #include <sys/msg.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>
#include "fifo.h"
 typedef struct mymsg {
  long mtype;
  char mtext[100];
  int private;
 }mymsg;

 int main()
 {


  int msqid;
  mymsg msg,buff;
  msqid=msgget(6000,IPC_CREAT|0666);

  if(msqid==-1){
  perror("FAiled to create message queue\n");
  }
  else{
  printf("Message queue id:%u\n",msqid);
  }



 //ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
int x=0 ; 
    while(1){
          int privatefifo;
          if(msgrcv(msqid,&buff,sizeof(msg)-sizeof(long),10,/*IPC_NOWAIT*/MSG_NOERROR | IPC_NOWAIT)>-1)
            x=10;
          else if(msgrcv(msqid,&buff,sizeof(msg)-sizeof(long),20,/*IPC_NOWAIT*/MSG_NOERROR | IPC_NOWAIT)>-1)
            x=20;
          else if(msgrcv(msqid,&buff,sizeof(msg)-sizeof(long),30,/*IPC_NOWAIT*/MSG_NOERROR | IPC_NOWAIT)>-1)
            x=30;




          printf("The message received is: %s , , from %d\n",buff.mtext,x);
        strcpy(msg.mtext,"i replay you");
             msg.mtype=buff.mtype;

        if(msgsnd(buff.private,&msg,sizeof(msg)-sizeof(long),0)==-1){
            perror("msgsnd failed:");
          }
          else{
           printf("Message sent successfully\n");
          }


}
 }
ak ak
  • 1
  • 1
  • Please provide a [mcve]. We need to see at least what the sender code does and you also need to explain how you came to the conclusion "the acceptance is accoures without priority". But for starters, please note that `msgrcv` has no notion of "priority". The fourth parameter is not a priority but a message type. So you need to clarify what your expected behaviour is and what the actual behaviour is. – kaylum Jul 24 '16 at 23:33

1 Answers1

0

i fixed it , just i have to put a condition at the value of x to make sure that one of the 3 conditions accoured or none of them ..

ak ak
  • 1
  • 1