这是我的代码,在最后一部分,msgrecv 不接受来自队列的消息,这些消息根据正确的优先级,例如:10 是最重要的接受然后 type=20 然后 type=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");
}
}
}