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

问题描述 投票:1回答:2

当msgget运行时,我收到一个错误,我的代码在返回msgget行时给我一个返回:“:Function not Implemented”。我已经尝试了很多选项,我在Windows 10上运行UBUNTU请大家帮忙,这里是我的代码和终端窗口结果:CODE:

#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: %d\n",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 Exist\n");
                    exit(EXIT_FAILURE);
                }
                else{
                    printf("Queue Found: %d\n",msgQid);
                    if((msgctl(msgQid, IPC_RMID, NULL))==-1){
                        fprintf(stderr,"Failed to Delete Queue\n");
                        exit(EXIT_FAILURE);
                    }
                    else{
                        printf("Queue Deleted: %d\n", msgQid);
                    }
                }
                exit(0);    
            }
        }
            else{
                //if not -c/-C or -d/-D then error
                fprintf(stderr, "Incorrect Usage of Flag\n");
                exit(EXIT_FAILURE);
            }
        }
        else{
            fprintf(stderr, "Invalid Key: %s\n", 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("%s\n", 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, %d\n", msgQid, sndbuf.mtype, sndbuf.mtext, (int)buf_lng);
                perror("Message Send");
                exit(EXIT_FAILURE);
            }
            exit(0);
        }   
    }
    else{
        printf("DOES NOT FIT CRITERIA\n");  
    }
}

终端窗口:

victoria @ DESKTOP-865C589:/ mnt / c / Users / toria / Documents $ gcc -o MSGQ msgQtest.c victoria @ DESKTOP-865C589:/ mnt / c / Users / toria / Documents $ ./MSGQ -c 100 msgget:退出... :功能未实现victoria @ DESKTOP-865C589:/ mnt / c / Users / toria / Documents $

c++ c linux message-queue
2个回答
2
投票

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


1
投票

Windows上不支持消息队列,即使使用Cygwin也是如此。但是看看this GitHub thread似乎这些电话将在不久的将来实施。

© www.soinside.com 2019 - 2024. All rights reserved.