c/c++ linux 进程间通信系列6,使用消息队列(message queue)
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;
}
用下面的命令,能够查看到上面的程序创建的共享内存。
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;
}
执行方法:【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;
}
执行方法:必须指定在写入消息是的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
c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854
c/c++ linux 进程间通信系列6,使用消息队列(message queue)的更多相关文章
- Linux进程间通信——使用System V 消息队列
消息队列 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法. 每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构.我们可以通过发送消息来避免命名管道的同步和阻塞问 ...
- Linux:进程通信之消息队列Message实例
/*send.c*/ /*send.c*/ #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h&g ...
- 浅谈消息队列 Message Queue
消息队列:在消息传递的过程中暂时保存消息的容器,充当发送者和接受者的中间人 消息队列的基本操作 using System; using System.Messaging; namespace MQ { ...
- c/c++ linux 进程间通信系列5,使用信号量
linux 进程间通信系列5,使用信号量 信号量的工作原理: 由于信号量只能进行两种操作等待和发送信号,即P(sv)和V(sv),他们的行为是这样的: P(sv):如果sv的值大于零,就给它减1:如果 ...
- c/c++ linux 进程间通信系列7,使用pthread mutex
linux 进程间通信系列7,使用pthread mutex #include <stdio.h> #include <stdlib.h> #include <unist ...
- c/c++ linux 进程间通信系列4,使用共享内存
linux 进程间通信系列4,使用共享内存 1,创建共享内存,用到的函数shmget, shmat, shmdt 函数名 功能描述 shmget 创建共享内存,返回pic key shmat 第一次创 ...
- c/c++ linux 进程间通信系列3,使用socketpair,pipe
linux 进程间通信系列3,使用socketpair,pipe 1,使用socketpair,实现进程间通信,是双向的. 2,使用pipe,实现进程间通信 使用pipe关键点:fd[0]只能用于接收 ...
- c/c++ linux 进程间通信系列2,使用UNIX_SOCKET
linux 进程间通信系列2,使用UNIX_SOCKET 1,使用stream,实现进程间通信 2,使用DGRAM,实现进程间通信 关键点:使用一个临时的文件,进行信息的互传. s_un.sun_fa ...
- c/c++ linux 进程间通信系列1,使用signal,kill
linux 进程间通信系列1,使用signal,kill 信号基本概念: 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核 ...
随机推荐
- 如何为自己的pip包打造可以执行的系统命令
1.我们在打包我们自己的Python Package的时候.我们不仅可以在代码中使用我们的package,而且可以添加一些可执行命令来执行自己的函数. 2 .我们应该怎么办呢? 1.首先新建目录以及文 ...
- 7.Git分支-分支简介、分支创建、分支切换
1.分支简介 几乎所有的版本控制系统都支持某种形式的分支.使用分支意味着可以把你的工作从开发主线上分离开来,以免影响开发主线.Git的分支是其必杀技,它相对于其它版本控制系统来说,具有难以置信的轻量性 ...
- BBS论坛(三)
3.1.cms用户名渲染和注销功能实现 显示登录的用户名 (1)app/cms/hooks.py from .views import bp import config from flask impo ...
- selenium之 chromedriver与chrome版本映射表(更新至v2.43)
看到网上基本没有最新的chromedriver与chrome的对应关系表,便兴起整理了一份如下,希望对大家有用: chromedriver版本 支持的Chrome版本 chromedriver版本 支 ...
- 『素数 Prime判定和线性欧拉筛法 The sieve of Euler』
素数(Prime)及判定 定义 素数又称质数,一个大于1的自然数,除了1和它自身外,不能整除其他自然数的数叫做质数,否则称为合数. 1既不是素数也不是合数. 判定 如何判定一个数是否是素数呢?显然,我 ...
- Quartz.NET学习笔记(二) Job和JobDetails
Job和JobDetails的关系 接一篇的例子 ISchedulerFactory schedFact = new StdSchedulerFactory(); IScheduler sched = ...
- dotnet core使用开源组件FastHttpApi进行web应用开发
FastHttpApi相对于asp.net mvc来说有着更轻量和性能上的优势,性能上面就不在这里介绍了(具体可查看 https://github.com/IKende/FastHttpApi).在这 ...
- Content Security Policy (CSP) 介绍
当我不经意间在 Twitter 页面 view source 后,发现了惊喜. <!DOCTYPE html> <html lang="en"> <h ...
- 剖析HBase负载均衡和性能指标
1.概述 在分布式系统中,负载均衡是一个非常重要的功能,在HBase中通过Region的数量来实现负载均衡,HBase中可以通过hbase.master.loadbalancer.class来实现自定 ...
- ASP.NET Core 框架源码地址
ASP.NET Core 框架源码地址 https://github.com/dotnet/corefx 这个是.net core的 开源项目地址 https://github.com/aspnet ...