小编ker*_*ker的帖子

C语言中如何实现消息传递IPC?

我正在尝试编写一个程序来实现父进程和子进程之间的基本消息传递。我以前从未使用过 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;
    } …
Run Code Online (Sandbox Code Playgroud)

c ipc

0
推荐指数
1
解决办法
6974
查看次数

标签 统计

c ×1

ipc ×1