C 消息队列回调
我试图学习和理解什么是消息队列。我在这里得到了代码(我从互联网上复制了它们,并对它们进行了一些修改,使之与我的示例相关)。它们是send.c,它允许您在文本中输入一些简单的操作并将其发送到消息队列。文件receive.c将接收这些操作,计算并将结果打印到屏幕上 我接下来要做的(但我不知道如何做)是让receive.c计算操作,然后它将把每个结果发送到send.c中的每个消息。所以请帮帮我,我有点困了:( 发送.c:C 消息队列回调,c,linux,message-queue,C,Linux,Message Queue,我试图学习和理解什么是消息队列。我在这里得到了代码(我从互联网上复制了它们,并对它们进行了一些修改,使之与我的示例相关)。它们是send.c,它允许您在文本中输入一些简单的操作并将其发送到消息队列。文件receive.c将接收这些操作,计算并将结果打印到屏幕上 我接下来要做的(但我不知道如何做)是让receive.c计算操作,然后它将把每个结果发送到send.c中的每个消息。所以请帮帮我,我有点困了:( 发送.c: #include <stdio.h> #include <st
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main() {
struct my_msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("send.c", 'B')) == -1) {
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0777 | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
printf("Enter lines of message, ^D to quit:\n");
buf.mtype = 1;
while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL) {
int len = strlen(buf.mtext);
if (buf.mtext[len-1] == '\n') {
buf.mtext[len-1] = '\0';
}
if (msgsnd(msqid, &buf, len+1, 0) == -1) {
perror("msgsnd");
}
}
if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构my_msgbuf{
长型;
字符多行文字[200];
};
int main(){
结构my_msgbuf buf;
int msqid;
钥匙(t)钥匙;;
如果((key=ftok(“send.c”,“B”))=-1){
佩罗尔(“ftok”);
出口(1);
}
如果((msqid=msgget(键0777 | IPC_CREAT))=-1){
佩罗尔(“msgget”);
出口(1);
}
printf(“输入消息行,^D退出:\n”);
buf.mtype=1;
while(fgets(buf.mtext,sizeof buf.mtext,stdin)!=NULL){
int len=strlen(buf.mtext);
if(buf.mtext[len-1]=='\n'){
buf.mtext[len-1]='\0';
}
如果(msgsnd(msqid和buf,len+1,0)=-1){
perror(“msgsnd”);
}
}
if(msgctl(msqid,IPC_RMID,NULL)=-1){
perror(“msgctl”);
出口(1);
}
返回0;
}
c.收到:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
struct my_msgbuf {
long mtype;
char mtext[200];
};
int calculate(char mtext[200]) {
int result = 0;
char number_1[20];
char number_2[20];
char operator;
int pos = 0;
for (int i = 0; i < strlen(mtext); i++) {
if (mtext[i] == '+' || mtext[i] == '-' || mtext[i] == '*' || mtext[i] == '/') {
operator = mtext[i];
pos = i + 2;
break;
}
number_1[i] = mtext[i];
}
number_1[pos-3] = '\0';
for (int j = pos; j <= strlen(mtext); j++) {
number_2[j - pos] = mtext[j];
}
switch(operator) {
case '+':
result = atoi(number_1) + atoi(number_2);
break;
case '-':
result = atoi(number_1) - atoi(number_2);
break;
case '*':
result = atoi(number_1) * atoi(number_2);
break;
case '/':
result = atoi(number_1) / atoi(number_2);
break;
}
return result;
}
int main() {
struct my_msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("send.c", 'B')) == -1) {
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0777 | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
printf("Ready to receive messages...\n");
for(;;) {
if (msgrcv(msqid, &buf, sizeof buf.mtext, 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
int result = calculate(buf.mtext);
printf("%s = %d\n", buf.mtext, result);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构my_msgbuf{
长型;
字符多行文字[200];
};
int计算(字符多行文字[200]){
int结果=0;
字符数_1[20];
字符数_2[20];
字符算子;
int pos=0;
for(int i=0;i 据我所知,对于(int j=pos;j,您需要:
一个请求队列,用于让发送方向接收方发送计算请求
每个发送方的一个通道,用于让接收方将其结果发送给请求方
为此,发送方必须创建一个适当的通道(无论您喜欢什么,如果需要,甚至是一个特定的消息队列),并在其请求中发送一个id,以便该通道进行应答
在现实生活中,这可能对应于这样一种场景:您拨打N号服务并给出您的请求+“请在完成后再打M号电话给我”