我在运行msgget时收到错误,我的代码返回了以下错误:“:Function not Implemented”:当它到达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: %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 $
最佳答案
不幸的是,消息队列在Windows Subsystem Linux上不可用:(