IPC 消息队列不适用于分叉进程

IPC Message queue not works with forked process

我正在尝试将 IPC 消息队列与分叉进程一起使用,将指针传递给动态分配的字符串,但它不起作用。

这是我做的一个简单的测试。它不打印从队列中接收到的字符串。但是,如果我尝试删除 fork() 它会完美地工作。

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

#define MSGSZ     128

typedef struct msgbuf {
    long    mtype;
    char    *mtext;
} message_buf;

int
main ()
{
    int msqid;
    char *p;
    key_t key = 129;

    message_buf sbuf, rbuf;
    p = (char *) malloc(sizeof(char) * MSGSZ);

    if ((msqid = msgget(key, IPC_CREAT|0666)) < 0) {
        perror("msgget");
        exit(1);
    }

    if (fork() == 0) {
        strcpy(p, "Did you get this?");
        sbuf.mtype = 1;
        sbuf.mtext = p;

        if (msgsnd(msqid, &sbuf, MSGSZ, IPC_NOWAIT) < 0) {
            perror("msgsnd");
            exit(1);
        }
    }
    else {
        sleep(1);

        if (msgrcv(msqid, &rbuf, MSGSZ, 0, 0) < 0) {
            perror("msgrcv");
            exit(1);
        }

        printf("Forked version: %s\n", rbuf.mtext);
        msgctl(msqid, IPC_RMID, NULL);
    }
}

问题是您正在跨进程边界发送指针。指针只在同一个进程内有效,在另一个进程中sent/used时无意义。事实上,您发送的是指针值后跟一大堆垃圾字节,因为 msgbuf.mtext 实际上不是 MSGSZ 字节大小(因此技术上调用未定义的行为)。

您需要做的是在消息中内联声明缓冲区。即,将 message_buf 定义更改为:

typedef struct msgbuf {
    long    mtype;
    char    mtext[MSGSZ];
} message_buf;

然后strcpy直接进入mtext:

strcpy(sbuf.mtext, "Did you get this?");

为清楚起见,下面是包含所描述更改的完整程序:

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

#define MSGSZ     128

typedef struct msgbuf {
    long    mtype;
    char    mtext[MSGSZ];
} message_buf;

int
main (void)
{
    int msqid;
    key_t key = 129;

    message_buf sbuf, rbuf;

    if ((msqid = msgget(key, IPC_CREAT|0666)) < 0) {
        perror("msgget");
        exit(1);
    }

    if (fork() == 0) {
        strcpy(sbuf.mtext, "Did you get this?");
        sbuf.mtype = 1;

        if (msgsnd(msqid, &sbuf, MSGSZ, IPC_NOWAIT) < 0) {
            perror("msgsnd");
            exit(1);
        }
    }
    else {
        sleep(1);

        if (msgrcv(msqid, &rbuf, MSGSZ, 0, 0) < 0) {
            perror("msgrcv");
            exit(1);
        }

        printf("Forked version: %s\n", rbuf.mtext);
        msgctl(msqid, IPC_RMID, NULL);
    }
}