gpt4 book ai didi

c - 为什么我的消息传递 IPC 代码不起作用? (C)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:21 24 4
gpt4 key购买 nike

我正在尝试编写一个程序来实现父进程和子进程之间的基本消息传递。我正在 windows 10 的 linux 子系统上执行此操作。我以前从未使用过 C,所以过去两天我一直在摸索和阅读教程,但我似乎无法让它工作.我最多只能创建无错误的消息队列。这是我的代码,以我对我正在做的事情的最佳理解评论:

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>

//this is the structure of the message i'm sending to the queue
struct message
{
long messagetype;
char text[10];
};

int main()
{

key_t key = 2222;
int msqid = msgget(key, IPC_CREAT); //create a message queue with the key


pid_t parentpid = getpid();
pid_t childpid = fork(); //these are mostly unused for now
if(childpid < 0) //fork failed
{
printf("fork failed\n");
return 1;
} else if(childpid == 0) //in child process
{
struct message sndmsg; //create a message to be send to the queue
printf("input message\n");
scanf("%s", sndmsg.text); //get the messages text fro input
printf("Sending message to queue: %s\n", sndmsg.text);
sndmsg.messagetype = 1; //set message type to 1
if(msgsnd(msqid, &sndmsg, sizeof(sndmsg.text), 0) < 0 ) // no idea what the last parameter really means here. check if message send fails
{
printf("error sending message\n");
} else
{
printf("sent message with text: %s\n",sndmsg.text);
}

printf("child process\n");
} else
{
wait(NULL); //wait until child process is done
struct message rcvmsg; //create a message to recieve the test from the queue
rcvmsg.messagetype = 1; //matching the message type here
if(msgrcv(msqid, &rcvmsg, sizeof(rcvmsg.text), 1, 0) < 0) //again, no idea what the last parameter does here. Checking to see if message recieve fails.
{
printf("error recieving message\n");
} else
{
printf("recieved message text : %s\n", rcvmsg.text);
}
printf("Parent process\n");
}
return 0;
}

当我运行时,消息发送和接收都失败(返回值 < 0)。我完全不知道 IPC_CREATE、IPC_NOWAIT 等是什么意思。通常就是最后一个参数在 msgsnd 和 msgrcv 中的作用。在示例中,我看到人们使用 0660 | IPC_CREAT,没有解释 0660 是什么。任何人都可以阐明我在我的代码中做错了什么,或者一般性地为通常使用 C# 和 Java 的人解释 C 中的消息传递,因为现在它基本上是黑魔法。到目前为止,我发现的每一个资源或教程一旦达到某个点就会让我头疼。谢谢。

最佳答案

  • 关于 0660 | IPC_CREAT:msggetmsgflg 参数是一个标志集合,即一些位应该设置在那里。 IPC_CREAT 是这些位之一,它的存在指示内核创建消息队列。低 9 位(在本例中为 0660)编码队列的权限

    在此示例中,0660 是一个八进制数,编码三个三元组位:110110000。这些三元组中的位表示队列所有者、所有者组的成员和其余队列的读取写入执行 权限世界分别。在这种情况下,用户和组被允许读取和写入队列,其余的没有任何权限。

    由于您未指定任何权限(飞机 IPC_CREAT 并且没有其他任何内容),您的发送和接收调用必然会失败并出现 Permission denied 错误。

  • Function not implemented 是一个完全不同的野兽。我对 windows 上的 linux 了解不多,但恐怕现在你运气不好。参见 herehere .消息队列尚未实现。

关于c - 为什么我的消息传递 IPC 代码不起作用? (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49165267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com