【问题标题】:Message Queue Callback消息队列回调
【发布时间】:2016-04-26 21:32:37
【问题描述】:

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

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

send.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;
}

receive.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;
}

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

【问题讨论】:

    标签: c linux message-queue


    【解决方案1】:

    据我了解,您需要:

    1. 一个请求队列,让发送方向接收方发送计算请求
    2. 每个发送者都有一个通道,让接收者将其结果发送给请求者。

    为此,发送者必须创建一个适当的频道(无论你喜欢什么,如果你愿意,甚至可以是特定的消息队列),并在其请求中发送一个 id 供频道回答。

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

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 2013-03-21
      • 2014-11-17
      • 1970-01-01
      • 2020-11-27
      • 2011-02-11
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      相关资源
      最近更新 更多