5

当我需要使用 Linux 消息队列编写简单的时间服务器和客户端时,我有任务。服务器打开一个消息队列,客户端发送一个带有其 PID 的请求(类型为 1 的消息),服务器读取该消息并发送一条 PID 类型的消息(取自读取的消息)。我把所有的代码都放在下面,因为我不知道我在哪里犯了错误。我不是 Linux 编程专家。甚至不知道我写的服务器是否正确。

服务器和客户端包含的文件(我需要这样写)。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <string.h>
#include <signal.h>

#define QUEUE 100
#define PERM_ALL 0777

typedef struct my_msgbuf {
    long mtype;
    int pid;
} ClientMessage;

typedef struct my_msgbuf2 {
    long mtype;
    struct tm time;
} TimeMessage;

服务器

int m_queue;

void cleanup(int signum) {
    if (msgctl(m_queue, IPC_RMID, NULL) == -1) {
        printf("Something happen on clean up\n");
        exit(1);
    }
    exit(signum);
}

int main() {
    ClientMessage pid_message;
    TimeMessage t;
    time_t clock;
    struct tm *cur_time;

    if ((m_queue = msgget(QUEUE, PERM_ALL | IPC_CREAT)) == -1) {
        printf("Can't create and open message queue\n");
        exit(1);
    }
    printf("created message queue = %d\n", m_queue);
    fflush(stdout);
    //t = malloc(sizeof(TimeMessage));
    signal(SIGINT, cleanup);

    while (1) {
        if (msgrcv(m_queue, &pid_message, sizeof(pid_message.pid), 1, 0) == -1) {
            break;
        } else {
            t.mtype = pid_message.pid;
            clock = time(NULL);
            cur_time = localtime(&clock);
            memcpy(&t.time, cur_time, sizeof(struct tm));
            msgsnd(m_queue, &t, sizeof(struct tm), 0);
        }
    }

    cleanup(0);
}

客户

int main() {
    int m_queue;
    TimeMessage *t;
    ClientMessage client;

    if ((m_queue = msgget(QUEUE, PERM_ALL)) == -1) {
        perror("Error in opening queue");
        exit(1);
    }

    client.mtype = 1;
    client.pid = getpid();

    while (1) {
        if (msgsnd(m_queue, &client, sizeof(client.pid), 0) == -1) {
            perror("Error sending to queue");
            exit(1);
        } else {
            if (msgrcv(m_queue, t, sizeof(struct tm), client.pid, 0) == -1) {
                perror("Error reading from queue");
                exit(1);
            }   
            printf("time: %d:%d:%d\n", t->time.tm_hour, t->time.tm_min, t->time.tm_sec);
        }
    }
    return 0;
}

两个程序都编译没有错误,但客户端返回“从队列读取错误”msgrcv 返回 -1。

4

1 回答 1

9

After adding the perror it appears that you have got the error stating "Bad Address" (EFAULT) which means that "The address pointed to by msgp (buffer) isn't accessible". From the code it appears that there has been no memory allocated to TimeMessage *t so you can either allocate memory or just change it to TimeMessage t and pass &t instead of t to msgrcv. Also size should be sizeof t (assuming the change from *t to t, or sizeof(TimeMessage) for *t) instead of sizeof(struct tm) (& obviously you would change printf statement accordingly)
Hope this helps!

于 2012-05-22T17:39:59.983 回答