linux 进程间通信系列6,使用消息队列(message queue)

概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了。

1,创建消息队列(message queue)

2,写消息到消息队列(message queue)

3,从消息队列(message queue)读消息

3,删除消息队列(message queue)

1,创建消息队列(message queue)

#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h> int main(){
int msgid; msgid = msgget(IPC_PRIVATE, 0600);
if(msgid < 0){
perror("msgget");
return 1;
} printf("%d\n", msgid);
return 0;
}

github源代码

用下面的命令,能够查看到上面的程序创建的共享内存。

ipcs -q

执行后的结果:

------ Message Queues --------
key msqid owner perms used-bytes messages
0x00000000 32768 ys 600 0 0

2,写消息到消息队列(message queue)

#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h> #define MTEXTSIZE 10 int main(int argc, char* argv[]){
int msgid;
struct msgbuf{
long mtype;
char mtext[MTEXTSIZE];
}mbuf; if(argc != 2){
printf("wrong argc");
return 1;
} msgid = atoi(argv[1]); mbuf.mtype = 777;
memset(mbuf.mtext, 0, sizeof(mbuf.mtext));
mbuf.mtext[0] = 'A'; if(msgsnd(msgid, &mbuf, MTEXTSIZE, 0) != 0){
perror("msgsnd");
return 1;
} return 0;
}

github源代码

执行方法:【ipcs -q】执行后,得到下面的数字。

./a.out 789884

执行后:ipcs -q 发现, message下面的数字从0变为1了,说明消息队列里有了一个消息。

------ Message Queues --------
key msqid owner perms used-bytes messages
0x00000000 32768 ys 600 10 1

3,从消息队列(message queue)读消息

#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h> #define MTEXTSIZE 10 int main(int argc, char* argv[]){
int msgid, msgtype; struct msgbuf{
long mtype;
char mtext[MTEXTSIZE];
}mbuf; if(argc != 3){
printf("wrong argc");
return 1;
} msgid = atoi(argv[1]);
msgtype = atoi(argv[2]); if(msgrcv(msgid, &mbuf, MTEXTSIZE, msgtype, 0) <= 0){
perror("msgrcv");
return 1;
} printf("%c\n", mbuf.mtext[0]);
return 0;
}

github源代码

执行方法:必须指定在写入消息是的type,也就是777

./a.out 32768 777

执行后,ipcs -q发现,message下面的数字,由1变为0了,说明消息队列里没有消息了。

------ Message Queues --------
key msqid owner perms used-bytes messages
0x00000000 32768 ys 600 0 0

4,删除消息队列(message queue)

#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h> int main(int argc, char* argv[]){
int msgid;
msqid_ds mds; if(argc != 2){
printf("wrong argv\n");
return 1;
} msgid = atoi(argv[1]); if(msgctl(msgid, IPC_RMID, &mds) != 0){
perror("msgctl");
return 1;
} return 0;
}

执行方法:

./a.out 32768

执行后,ipcs -q发现,消息队列本身都没有了。

------ Message Queues --------
key msqid owner perms used-bytes messages

用命令行删除共享内存:【ipcs -q】执行后,得到下面的数字。

ipcrm -q id

github源代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ linux 进程间通信系列6,使用消息队列(message queue)的更多相关文章

  1. Linux进程间通信——使用System V 消息队列

    消息队列 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法. 每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构.我们可以通过发送消息来避免命名管道的同步和阻塞问 ...

  2. Linux:进程通信之消息队列Message实例

    /*send.c*/ /*send.c*/ #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h&g ...

  3. 浅谈消息队列 Message Queue

    消息队列:在消息传递的过程中暂时保存消息的容器,充当发送者和接受者的中间人 消息队列的基本操作 using System; using System.Messaging; namespace MQ { ...

  4. c/c++ linux 进程间通信系列5,使用信号量

    linux 进程间通信系列5,使用信号量 信号量的工作原理: 由于信号量只能进行两种操作等待和发送信号,即P(sv)和V(sv),他们的行为是这样的: P(sv):如果sv的值大于零,就给它减1:如果 ...

  5. c/c++ linux 进程间通信系列7,使用pthread mutex

    linux 进程间通信系列7,使用pthread mutex #include <stdio.h> #include <stdlib.h> #include <unist ...

  6. c/c++ linux 进程间通信系列4,使用共享内存

    linux 进程间通信系列4,使用共享内存 1,创建共享内存,用到的函数shmget, shmat, shmdt 函数名 功能描述 shmget 创建共享内存,返回pic key shmat 第一次创 ...

  7. c/c++ linux 进程间通信系列3,使用socketpair,pipe

    linux 进程间通信系列3,使用socketpair,pipe 1,使用socketpair,实现进程间通信,是双向的. 2,使用pipe,实现进程间通信 使用pipe关键点:fd[0]只能用于接收 ...

  8. c/c++ linux 进程间通信系列2,使用UNIX_SOCKET

    linux 进程间通信系列2,使用UNIX_SOCKET 1,使用stream,实现进程间通信 2,使用DGRAM,实现进程间通信 关键点:使用一个临时的文件,进行信息的互传. s_un.sun_fa ...

  9. c/c++ linux 进程间通信系列1,使用signal,kill

    linux 进程间通信系列1,使用signal,kill 信号基本概念:  软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核 ...

随机推荐

  1. ASP.NET Core 系列目录

    目录: ASP.NET Core 2.0 : 一. 概述 ASP.NET Core 2.0:二. 开发环境 ASP.NET Core 2.0 : 三. 项目结构 ASP.NET Core 2.0 : ...

  2. Java基础2:基本数据类型与常量池

    本文会结合虚拟机对引用和对象的不同处理来介绍三大特性的原理. 三大特性:继承 封装 多态 继承 Java中的继承只能单继承,但是可以通过内部类继承其他类来实现多继承. public class Son ...

  3. java~spring-ioc的使用

    spring-ioc的使用 IOC容器在很多框架里都在使用,而在spring里它被应用的最大广泛,在框架层面 上,很多功能都使用了ioc技术,下面我们看一下ioc的使用方法. 把服务注册到ioc容器 ...

  4. 从0打卡leetcode之day 3 -- 最大子序列和

    前言 就有要把leetcode的题刷完,每天一道题,每天进步一点点 从零打卡leetcode之day 3 题目描述: 给定一个int类型的数组,求最大子序列的和. 也就是说,从这个数组中截取一个子数组 ...

  5. Django 系列博客(十二)

    Django 系列博客(十二) 前言 本篇博客继续介绍 Django 中的查询,分别为聚合查询和分组查询,以及 F 和 Q 查询. 聚合查询 语法:aggregate(*args, **kwargs) ...

  6. ROS笔记1 安装及创建一个ROS Package

    安装 跟着官方的安装指引来就行了.安装前要先确定自己的ros版本和ubuntu版本.这二者是一一对应的. http://wiki.ros.org/ROS/Installation 主要是package ...

  7. Qt的内存管理机制

    当我们在使用Qt时不可避免得需要接触到内存的分配和使用,即使是在使用Python,Golang这种带有自动垃圾回收器(GC)的语言时我们仍然需要对Qt的内存管理机制有所了解,以更加清楚的认识Qt对象的 ...

  8. babel版本兼容报错处理:Plugin/Preset files are not allowed to export objects

    原文地址: https://www.cnblogs.com/jiebba/p/9618930.html 1.为什么会报错 ? 这里抱着错误是因为 babel 的版本冲突. 多是因为你的 babel 依 ...

  9. 第57章 GrantValidationResult - Identity Server 4 中文文档(v1.0.0)

    该GrantValidationResult类模型补助确认为扩展授权和资源所有者密码授权的结果. 最常见的用法是使用身份验证(成功用例): context.Result = new GrantVali ...

  10. C#/VB.NET 给Word文档添加/撤销书签

    在现代办公环境中,阅读或者编辑较长篇幅的Word文档时,想要在文档中某一处或者几处留下标记,方便日后查找.修改时,需要在相对应的文档位置插入书签.那对于开发者而言,在C#或者VB.NET语言环境中,如 ...