进程通信消息队列代码及一些注意点

发送方
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/msg.h>

#define MAXLENGTH 80

struct msg_st
{
    long msgtype;
    char text[MAXLENGTH];
};

int main()
{
    int msgid,i;
    char *snddata[4]={"Hello,world!","Prepare to rcv data!","Data is 1234!","end"};
    struct msg_st msgsnddata;

    msgid = msgget((key_t)1234,06666|IPC_CREAT);
    if(msgid == -1)
        exit(0);

    while(i<4)
    {
        msgsnddata.msgtype = 1;
        strcpy(msgsnddata.text,snddata[i]);
        msgsnd(msgid,&msgsnddata,MAXLENGTH,0);
        i++;
    }

    return 0;
}

#define MAXLENGTH 80约定消息队列中每条消息的最大长度,不可以超过系统规定的最大长度。

struct msg_st
{
long msgtype;
char text[MAXLENGTH];
};
约定每条消息的格式。long msgtype必须要有表示消息类型。text为内容,可以自己定义其它形式如int。

msgget((key_t)1234,06666|IPC_CREAT)中1234位消息队列的key,接收方需要使用。0666表示有读写权限,IPC_CREAT不存在则创建。

msgsnd(msgid,&msgsnddata,MAXLENGTH,0);中&msgsnddata为指针,MAXLENGTH数据长度,常用0

strcpy(msgsnddata.text,snddata[i]);将snddata[i]复制给前面,均为指针。

接收方

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/msg.h>

#define MSGLENGTH 80

struct msg_st
{
    long msgtype;
    char text[MSGLENGTH];
};

int main()//receive msg
{
    int msgid,msgtype;
    struct msg_st rcvdata;

    msgid = msgget((key_t)1234,0666|IPC_CREAT);
    if(msgid == -1)
    {
        printf("error msgget!\n");
        exit(0);
    }

    msgtype = 0;//rcv first msg from queue
    while(1)
    {
        msgrcv(msgid,&rcvdata,MSGLENGTH,msgtype,0);
        if(strcmp(rcvdata.text,"end") == 0)
            break;
        else
            printf("%s\n",rcvdata.text);
    }


    if(msgctl(msgid,IPC_RMID,0) == -1)//close msg queue
    {
        printf("error msgctl!\n");
        exit(0);
    }

    return 0;
}

需要循环读取消息队列,msgrcv(msgid,&rcvdata,MSGLENGTH,msgtype,0);
msgtype为0表示每次读取第一条。数据读完就没了。

msgctl(msgid,IPC_RMID,0)读完数据后关闭消息队列。

以下是一个简单的示例代码,展示了如何在Linux进程之间使用消息队列进行通信。 发送方进程: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/msg.h> #define MSG_SIZE 256 struct msg_buffer { long msg_type; char msg_text[MSG_SIZE]; }; int main() { key_t key; int msg_id; struct msg_buffer message; // 创建一个唯一的key key = ftok("msgq.txt", 'A'); // 创建消息队列 msg_id = msgget(key, 0666 | IPC_CREAT); // 设置消息类型 message.msg_type = 1; // 设置消息内容 strcpy(message.msg_text, "Hello from sender!"); // 发送消息 msgsnd(msg_id, &message, sizeof(message), 0); printf("Message sent: %s\n", message.msg_text); return 0; } ``` 接收方进程: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/msg.h> #define MSG_SIZE 256 struct msg_buffer { long msg_type; char msg_text[MSG_SIZE]; }; int main() { key_t key; int msg_id; struct msg_buffer message; // 创建一个唯一的key key = ftok("msgq.txt", 'A'); // 获取消息队列 msg_id = msgget(key, 0666 | IPC_CREAT); // 接收消息 msgrcv(msg_id, &message, sizeof(message), 1, 0); printf("Message received: %s\n", message.msg_text); // 删除消息队列 msgctl(msg_id, IPC_RMID, NULL); return 0; } ``` 这两个进程可以在同一台计算机上运行,也可以在不同的计算机上运行。在发送方进程,首先创建一个唯一的key,然后使用该key创建一个新的消息队列。接下来,设置消息类型为1,将消息内容设置为“Hello from sender!”,并将消息发送到消息队列。 在接收方进程,使用与发送方进程相同的key获取消息队列。然后使用msgrcv函数接收消息,并将消息类型设置为1,这与发送方进程设置的消息类型相同。最后,打印接收到的消息,然后使用msgctl函数删除消息队列
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值