在.c文件接收函数中使用Linux中的MSGGET创建消息队列未实现错误

Creating a Message Queue using msgget in Linux in a .c file Receiving function not implementing error

本文关键字:创建 MSGGET 中的 消息 队列 错误 实现 Linux 文件 函数      更新时间:2023-10-16

我在MSGGET运行时会收到错误,当它到达MSGGET行时,我的代码正在为我返回:":未实现的功能"。我尝试了很多选项,我正在Windows 10上运行Ubuntu,请帮助,这是我的代码和终端窗口结果:代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <sys/types.h>
#include "msg.h"
void main(int argc, char *argv[]){
    int msgQid;
    int flg = IPC_CREAT | 0644;
    key_t k;
    message_buf sndbuf;
    message_buf rdbuf;
    size_t buf_lng;
    k= atoi(argv[2]);
    if(argc == 3){
        if(k>1){
            if(strcmp(argv[1],"-c")==0 || strcmp(argv[1],"-C")==0){
                //Create Message Queue
                if((msgQid=msgget(k, flg))<0){
                    perror("msgget: EXITING...n");
                    exit(EXIT_FAILURE);
                }
                else{
                    printf("Queue Created: %dn",msgQid);
                }
                exit(0);
            }
            else if(strcmp(argv[1],"-d")==0 || strcmp(argv[1],"-D")==0){
                //Delete Message Queue
                flg=0666;
                if((msgQid=msgget(k, flg))==-1){
                    printf("Message Queue Does Not Existn");
                    exit(EXIT_FAILURE);
                }
                else{
                    printf("Queue Found: %dn",msgQid);
                    if((msgctl(msgQid, IPC_RMID, NULL))==-1){
                        fprintf(stderr,"Failed to Delete Queuen");
                        exit(EXIT_FAILURE);
                    }
                    else{
                        printf("Queue Deleted: %dn", msgQid);
                    }
                }
                exit(0);    
            }
        }
            else{
                //if not -c/-C or -d/-D then error
                fprintf(stderr, "Incorrect Usage of Flagn");
                exit(EXIT_FAILURE);
            }
        }
        else{
            fprintf(stderr, "Invalid Key: %sn", argv[2]);
            exit(EXIT_FAILURE);
        }
    if(argc == 4){
        if(argv[1]=="-r" || argv[1]=="R"){
            //Receive message of type from queue
            if((msgQid=msgget(k, flg))<0){
                perror("msgget: In MSG RECEIVE");
                exit(1);
            }
            if((msgrcv(msgQid, &rdbuf, MSGSZ, k, 0)) <0){
                perror("message receive");
                exit(1);
            }
            printf("%sn", rdbuf.mtext);
            exit(EXIT_SUCCESS);
        }
    }
    if(argc == 5){
        if(argv[1]=="-s" || argv[1]=="S"){
            //Send message of type to queue
            if((msgQid=msgget(k, flg))<0){
                perror("msgget: In MSG RECEIVE");
                exit(1);
            }
            sndbuf.mtype = atoi(argv[3]);
            strncpy(sndbuf.mtext, argv[4], MSGSZ);
            buf_lng = strlen(sndbuf.mtext)+1;
            if((msgsnd(msgQid, &sndbuf, buf_lng, IPC_NOWAIT))<0){
                fprintf(stderr, "%d, %ld, %s, %dn", msgQid, sndbuf.mtype, sndbuf.mtext, (int)buf_lng);
                perror("Message Send");
                exit(EXIT_FAILURE);
            }
            exit(0);
        }   
    }
    else{
        printf("DOES NOT FIT CRITERIAn");  
    }
}

终端窗口:

victoria@desktop -865c589:/mnt/c/c/users/toria/documents $ gcc -o msgq msgqtest.c victoria@desktop -865c589:/mnt/mnt/c/c/c/users/users/users/toria/toria/documents $ ./msgq -c -c 100
MSGGET:退出...
:未实现功能victoria@desktop-865c589:/mnt/c/users/toria/documents $

不幸的是,Windows子系统Linux上没有消息队列:

消息队列在Windows上也不支持Cygwin。但是看这个github线程似乎将在不久的将来实现这些呼叫。