linux消息队列实现信息互递

简介: linux消息队列实现信息互递


消息队列

struct msgbuf{
        long mtype;
        char mtext[128];
};
int main()
{
        struct msgbuf readbuf;
        int msgid = msgget(0x1235,IPC_CREAT|0777);//创建消息队列
        if(msgid == -1){
                printf("get que failuer\n");//如果返回值为-1,说明创建失败
        }
        msgrcv(msgid,&readbuf,sizeof(readbuf.mtext),888,0);//读取消息
        printf("read from que:%s\n",readbuf.mtext);//打印输出
        struct msgbuf sendbuf = {988,"thank you for reach"};//创建消息结构体
        msgsnd(msgid,&sendbuf,strlen(sendbuf.mtext),0);//添加消息,发送上面的sendbuf的内容
        return 0;
}

消息队列收

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<string.h>
#include<sys/msg.h>
struct msgbuf{
        long mtype;
        char mtext[128];
};
int main()
{
        struct msgbuf sendbuf = {888,"this is message from quen"};
        struct msgbuf readbuf;
        int msgid = msgget(0x1235,IPC_CREAT|0777);//创建消息队列
        if (msgid == -1){
                printf("get que failuer\n");
        }
        msgsnd(msgid,&sendbuf,strlen(sendbuf.mtext),0);//添加消息
        printf("send over\n");
        msgrcv(msgid,&readbuf,sizeof(readbuf.mtext),988,0);//读取消息
        printf("reaturn from get %s\n",readbuf.mtext);//打印输出读取的消息
        return 0;
}
~            

运行显示

20200803110157677.png

相关文章
|
21天前
|
消息中间件 Linux
|
21天前
|
存储 Linux
linux查看系统版本、内核信息、操作系统类型版本
linux查看系统版本、内核信息、操作系统类型版本
71 9
|
2天前
|
消息中间件 Linux
【Linux】System V 消息队列(不重要)
【Linux】System V 消息队列(不重要)
|
14天前
|
消息中间件 存储 安全
【Linux 系统】进程间通信(共享内存、消息队列、信号量)(下)
【Linux 系统】进程间通信(共享内存、消息队列、信号量)(下)
|
14天前
|
消息中间件 算法 Linux
【Linux 系统】进程间通信(共享内存、消息队列、信号量)(上)
【Linux 系统】进程间通信(共享内存、消息队列、信号量)(上)
|
16天前
|
运维 Linux 程序员
最全查看Linux系统状态脚本_linux查询所有服务器信息的脚本,墙都不扶就服你
最全查看Linux系统状态脚本_linux查询所有服务器信息的脚本,墙都不扶就服你
最全查看Linux系统状态脚本_linux查询所有服务器信息的脚本,墙都不扶就服你
|
21天前
|
Linux Go
linux ls -la文件信息含义
linux ls -la文件信息含义
22 1
|
21天前
|
编解码 Ubuntu Linux
|
21天前
|
Linux
linux驱动层输出dev_dbg打印信息
linux驱动层输出dev_dbg打印信息
49 0
|
21天前
|
弹性计算 运维 Shell
统计Linux 进程相关数量信息
【4月更文挑战第29天】
12 0

热门文章

最新文章