0

我正在练习 IPC 消息队列,我编写了下面的程序来从队列中发送和获取消息。程序在 Linux 平台上运行良好。

但是当我在 Windows 中运行 WSL 上的程序时,我看到多个 ipcs -q具有相同键和 msgqid 的条目,看起来像 WSL 中的某种错误。

-> uname -a
Linux kumars-pc 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

下面是我的程序:

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

#define MAX_PATHNAME_SIZE 100
#define MAX_MESSAGE_SIZE 100


static struct mymsg {
        int msg_type;
        char text[MAX_MESSAGE_SIZE];
    } msg;

int open_msg_queue(key_t key) {
    int id;
    if ((id = msgget(key, IPC_CREAT | 0660)) == -1) {
        printf("Error while creating msg Queue\n");
        return -1;
    }
    return id;
}

int msg_snd(int id, struct mymsg *buf) {
    int status;
    size_t msg_size;
    msg_size = sizeof(struct mymsg);
    if((status = msgsnd(id, buf, msg_size, 0)) == -1) {
        printf("Error in appending msg to queue %i\n, %d\n", id, EXIT_FAILURE);
        return status;
    }
    return status;
}

size_t msg_rcv(int id, struct mymsg *buf) {
    size_t msg_size;
    int length;
    length = sizeof(struct mymsg);
    if ((msg_size = msgrcv(id, buf, length, 0, 0)) == -1) {
        printf("Error in fetching the msg from the queue: %i\n", id);
    }
    return msg_size;
}

int main(int argc, char *argv[]) {
    key_t msg_queue_key;
    int msg_queue_id;
    int status;
    char filepath[MAX_PATHNAME_SIZE];
    size_t msg_size;
    char mode[2];

    if(argc < 3) {
        printf("Usage: binary <path> <s/r>");
        exit(1);
    }
    // exit if filename is larger than MAX_PATHNAME_SIZE
    if(strlen(argv[1]) > MAX_PATHNAME_SIZE) {
        printf("File path too long\n");
        exit(1);
    }
    strcpy(filepath, *++argv);
    printf("filepath: %s\n", filepath);
    
    // Create IPC identier key
    if ((msg_queue_key = ftok(filepath, 'a')) == -1) {
        printf("Error in creating key for message queue\n");
        exit(1);
    }
    
    printf("key: %x\n", msg_queue_key);
    msg_queue_id = open_msg_queue(msg_queue_key);
    if (msg_queue_id == -1) {
        exit(1);
    }
    printf("msg_queue_id: %i\n", msg_queue_id);
    strcpy(mode, *++argv);
    if (strcmp(mode, "s") == 0) {
        msg.msg_type = 1;
        strcpy(msg.text, "Hello World");
        status = msg_snd(msg_queue_id, &msg);
    }
    if (strcmp(mode, "r") == 0) {
        msg_size = msg_rcv(msg_queue_id, &msg);
        printf("msg: %s\n", msg.text);
    }

}

发送消息前:

-> ipcs

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status

------ Semaphore Arrays --------
key        semid      owner      perms      nsems

让我们运行我的程序

-> cc basic/IPC/msg_queue.c
-> ./a.out /tmp/test.txt s    # s indicates send the message
filepath: /tmp/test.txt
key: 6110e08b
msg_queue_id: 19

消息已发送,请检查ipcs -q条目

-> ipcs -q

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1
0x6110e08b 19         kumars     660        104          1

正如我们可以看到具有相同键和 msgqid 的多个条目,我不知道为什么会这样。

让我们使用消息并再次列出消息队列条目

-> ./a.out /tmp/test.txt r    # r is to fetch msg from the queue
filepath: /tmp/test.txt
key: 6110e08b
msg_queue_id: 19
msg: Hello World

如您所见,味精已正确获取“Hellow World”

-> ipcs -q

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0
0x6110e08b 19         kumars     660        0            0

我们可以看到 msg 是从队列中消耗的,我只是想知道为什么这么多条目

4

0 回答 0