询问msgrcv,不适用于接受优先级的消息

时间:2016-07-24 23:18:04

标签: c process real-time ipc msgrcv

  

这是我的代码,在最后一部分,msgrecv不接受来自队列的消息,正确的优先级,例如:10是最重要的接受然后键入= 20然后键入= 30 ...这是我的目标是以这种方式接受消息......   任何人都可以告诉我问题出在哪里?   因为接受是没有优先权的..   谢谢..   这是代码

     #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");
          }


}
 }

1 个答案:

答案 0 :(得分:0)

我修复了它,只是我必须在x的值处设置一个条件,以确保3个条件中的一个符合条件或者没有条件。