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. OAuth 2.0 授权码请求

    关于OAuth 2.0,请参见下面这两篇文章(墙裂推荐): <OAuth 2.0> <Spring Security OAuth 2.0> 纸上得来终觉浅,绝知此事要躬行.理论 ...

  2. Unable to build: the file dx.jar was not loaded from the SDK folder

    eclipse 运行 android 时失败了,提示 Unable to build: the file dx.jar was not loaded from the SDK folder! 解决办法 ...

  3. Solr 12 - 部署SolrCloud中遇到的问题 + 解决方法

    目录 1 ZooKeeper管理配置文件的另一种方法 2 Solr服务不能访问 3 部分节点处于"Recovering"或"Gone"状态 4 Solr集群不稳 ...

  4. [工具]PyCharm激活、注册码无效解决办法

    前言 我是个 Pythoner,开发工具一直使用的 JetBrains 的 PyCharm.我师傅告诉过我:一个程序员一定要有一个用的很 6 的 IDE,你的开发效率会提高很多,很多... 我从小白的 ...

  5. 【Python3爬虫】12306爬虫

    此次要实现的目标是登录12306网站和查看火车票信息. 具体步骤 一.登录 登录功能是通过使用selenium实现的,用到了超级鹰来识别验证码.没有超级鹰账号的先注册一个账号,充值一点题分,然后把下载 ...

  6. C语言实现链队列的初始化&进队&出队

    /*链表实现队列的一系列操作*/ #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typed ...

  7. github总结(4)--关于git reset --hard这个命令的惨痛教训

    背景叙述: 前几天,上传自己的个站到git上的时候,手欠脑发晕的用了次git reset --hard xxxxxx 命令.由于只在线上传入了一个index.html页面(自己都不知道自己咋想的,就这 ...

  8. Java并发专题(一)认识线程

    1.1 认识线程 线程是轻量级进程,也是程序执行的一个路径,每一个线程都有自己的局部变量表.程序计数器(指向正在执行的指令指针)以及各自的生命周期,现代操作系统中一般不止一个线程在运行.比如说,当我们 ...

  9. 四种途径提高RabbitMQ传输数据的可靠性(二)

    前言 上一篇四种途径提高RabbitMQ传输消息数据的可靠性(一)已经介绍了两种方式提高数据可靠性传输的方法,本篇针对上一篇中提出的问题(1)与问题(2)提出解决常用的方法. 本文其实也就是结合以上四 ...

  10. 第41章 CORS - Identity Server 4 中文文档(v1.0.0)

    第41章 CORS IdentityServer中的许多端点将通过基于JavaScript的客户端的Ajax调用进行访问.鉴于IdentityServer最有可能托管在与这些客户端不同的源上,这意味着 ...