0

I am practicing the IPC message Queue, I have written Below program to send and fetch message from the queue. Program is working fine on Linux Platform.

But when I run the Program on WSL in windows, I see multiple entries ipcs -q with same key and msgqid, Looks like some kind of bug in 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

Below is my program:

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

}

Before sending the message:

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

Lets run my program

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

Msg is sent, check the ipcs -q entry

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

As we can see multiple entries with same key and msgqid, I dont know whey it is happening like that.

Lets Consume the Message and again list msg queue entry

-> ./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

AS you can see msg is fetched properly "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

We can see msg is consumed from the Queue, I am just wondering why so many entries

Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • I'm unable to reproduce those results on a Linux system (5.8 kernel). Even sending multiple messages that aren't yet read only results in a single entry in ```ipcs -q``` with an increasing ```messages``` count. Your code appears to be working on my system. – sj95126 Aug 14 '21 at 13:55
  • I run my code on another server and its working fine, Looks like the problem is with the WSL. – Hackaholic Aug 15 '21 at 14:34

0 Answers0