c - 消息队列回调

标签 c linux message-queue

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

我接下来要做的(但我不知道怎么做)是让 receive.c 计算操作,然后它将每个结果发送到来自 send.c 的每条消息。所以请帮帮我,我有点卡住了:(

发送.c:

#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:

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

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

最佳答案

据我了解,您需要:

  1. 一个请求队列,让发送方向接收方发送计算请求
  2. 为每个发送者提供一个 channel ,让接收者将其结果发送给请求者。

为此,发送者必须创建一个合适的 channel (任何你喜欢的 channel ,如果你愿意,甚至是一个特定的消息队列),并在其请求中发送一个 id 以供 channel 应答。

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

关于c - 消息队列回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36810023/

相关文章:

linux - 访问外部设备时 ARM Cortex 上的 SIGBUS

c - 需要算法方面的帮助(堆栈溢出)

c - 如何阻止control-d退出c中的shell

将 16 字节 IPv6 转换为冒号分隔的字符串

python - 有没有办法确定 Linux PID 是否暂停?

linux - SSH 权限被拒绝(公钥)

php - 连接时rabbitmq错误

Kubernetes 消息消费者可扩展性

c - 函数中的 12 天圣诞节 C 程序

java - ActiveMQ中的并发消息消费