gpt4 book ai didi

c - 询问 msgrcv ,不适用于根据优先级接受消息

转载 作者:行者123 更新时间:2023-11-30 19:38:12 25 4
gpt4 key购买 nike

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


}
}

最佳答案

我修复了它,只是我必须在 x 的值处设置一个条件,以确保 3 个条件之一满足或都不满足 ..

关于c - 询问 msgrcv ,不适用于根据优先级接受消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com