首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >消息队列回调

消息队列回调
EN

Stack Overflow用户
提问于 2016-04-23 11:08:33
回答 1查看 1K关注 0票数 1

我正在努力学习和理解什么是消息队列。我在这里得到了代码(我从互联网上复制了它们,并将它们修改为与我的示例相关的代码)。它们是send.c,允许您输入一些简单的文本操作并将其发送到消息队列。C文件将接收这些操作,计算它并将结果打印到屏幕上。

接下来我想要做的(但我不知道如何)是做Recve.c计算操作,然后它会将每个结果发送到send.c中的每个消息。所以请帮帮我,我有点卡住了

发送者c:

代码语言:javascript
复制
#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;
}

C.收到:

代码语言:javascript
复制
#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;
}

当您运行这些文件时,它们将如下所示:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-23 11:42:25

据我所知,你需要:

  1. 请求队列,允许发送方向接收方发送计算请求。
  2. 每个发送方允许接收方将其结果发送给请求者的通道。

为此,发送方必须创建一个适当的通道(无论您喜欢什么,即使是一个特定的消息队列(如果您想要的话),并在其请求中发送一个id,以便通道响应。

在现实生活中,这可能与这样的场景相对应:您在N号处调用服务并发出请求+“完成后请在M号回电给我”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36810023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档