【问题标题】:Why wont my message passing IPC code work? (C)为什么我的消息通过 IPC 代码不起作用? (C)
【发布时间】:2023-03-12 12:15:01
【问题描述】:

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

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

运行时,消息发送和接收均失败(返回值

【问题讨论】:

  • 失败后使用 perror("msgget");和 perror("msgsnd");打印错误信息
  • 我得到“msgsnd:功能未实现”和“msgrcv:功能未实现”。我找不到解决这个问题的任何答案,我似乎拥有所有必要的内容。

标签: c linux ipc


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

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

    由于您未指定任何权限(平面 IPC_CREAT 和其他任何内容),因此您的发送和接收呼叫必然会失败并出现 Permission denied 错误。

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

【讨论】:

    【解决方案2】:

    “功能未实现”(错误代码 ENOSYS)表示您的 Linux 版本已关闭此功能。这并不奇怪,因为它被认为已经过时了。

    您可以重新构建内核以启用该功能(如果您使用标准内核可能会很痛苦)或使用不同的 IPC 机制,例如管道。

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 2013-08-06
      • 2016-01-23
      相关资源
      最近更新 更多