Unix IPC之Posix消息队列(1)
部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html
IPC对象的持续性:http://book.51cto.com/art/201006/207275.htm
消息队列可以认为是一个消息链表,某个进程往一个消息队列中写入消息之前,不需要另外某个进程在该队列上等待消息的达到,这一点与管道和FIFO相反。Posix消息队列与System V消息队列的区别如下:
1. 对Posix消息队列的读总是返回最高优先级的最早消息,对System V消息队列的读则可以返回任意指定优先级的消息。
2. 当往一个空队列放置一个消息时,Posix消息队列允许产生一个信号或启动一个线程,System V消息队列则不提供类似的机制。
Posix消息队列操作函数如下:
头文件及部分定义:
#include <mqueue.h>
typedef int mqd_t;
/* Establish connection between a process and a message queue NAME and
return message queue descriptor or (mqd_t) -1 on error. OFLAG determines
the type of access used. If O_CREAT is on OFLAG, the third argument is
taken as a `mode_t', the mode of the created message queue, and the fourth
argument is taken as `struct mq_attr *', pointer to message queue
attributes. If the fourth argument is NULL, default attributes are
used. */
extern mqd_t mq_open (__const char *__name, int __oflag, ...)
__THROW __nonnull (()); /* Removes the association between message queue descriptor MQDES and its
message queue. */
extern int mq_close (mqd_t __mqdes) __THROW; /* Remove message queue named NAME. */
extern int mq_unlink (__const char *__name) __THROW __nonnull (());
下面采用上面的函数,写程序进程测试。
程序1(mqcreate1.c):创建一个消息队列,其名字是作为命令行参数指定。程序如下:
#include "unpipc.h" int
main(int argc, char **argv)
{
int c, flags;
mqd_t mqd; // linux下是int类型 flags = O_RDWR | O_CREAT;
while ( (c = Getopt(argc, argv, "e")) != -)
{
switch (c)
{
case 'e':
flags |= O_EXCL;
break;
}
}
if (optind != argc - )
err_quit("usage: mqcreate [ -e ] <name>"); mqd = Mq_open(argv[optind], flags, FILE_MODE, NULL); Mq_close(mqd);
exit();
}
程序2(mqunlink.c):删除一个消息队列。程序如下:
#include "unpipc.h" int
main(int argc, char **argv)
{
if (argc != )
err_quit("usage: mqunlink <name>"); Mq_unlink(argv[]); exit();
}
注:代码需在Unix网络编程-卷2的程序包中才能编译通过。
Posix消息队列是建立在系统的虚拟文件系统中,若要查看,可将其挂载到系统的文件系统中;
mount命令格式如下:

挂载命令如下:
[dell@localhost pxmsg]$ mkdir /tmp/mqueue
[dell@localhost pxmsg]$ mount -t mqueue none /tmp/mqueue
mount: only root can do that
[dell@localhost pxmsg]$ sudo !-
sudo mount -t mqueue none /tmp/mqueue
[sudo] password for dell:
程序运行结果:
[dell@localhost pxmsg]$ ./mqunlink /temp.1234
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量 0
[dell@localhost pxmsg]$ ./mqcreate1 /temp.1234
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量 0
-rw-r--r--. 1 dell dell 80 8月 12 20:10 temp.1234
[dell@localhost pxmsg]$ ./mqunlink /temp.1234
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量 0
[dell@localhost pxmsg]$
说明:为什么要这样做,可在shell下运行一下命令查询:
man 7 mq_overview
这里选取部分说明文档:
Mounting the message queue file system
On Linux, message queues are created in a virtual file system. (Other
implementations may also provide such a feature, but the details are
likely to differ.) This file system can be mounted (by the superuser)
using the following commands: # mkdir /dev/mqueue
# mount -t mqueue none /dev/mqueue Each message queue is identi fied by a name of the form /somename. Two processes can operate on the
same queue by passing the same name to mq_open().
Unix IPC之Posix消息队列(1)的更多相关文章
- Unix IPC之Posix消息队列(3)
struct mq_attr { long mq_flags; /* message queue flag : 0, O_NONBLOCK */ long mq_maxmsg; /* max numb ...
- Unix IPC之Posix消息队列(2)
/* Query status and attributes of message queue MQDES. */ extern int mq_getattr (mqd_t __mqdes, stru ...
- IPC通信:Posix消息队列
IPC通信:Posix消息队列 消息队列可以认为是一个链表.进程(线程)可以往里写消息,也可以从里面取出消息.一个进程可以往某个消息队列里写消息,然后终止,另一个进程随时可以从消息队列里取走这些消息. ...
- UNIX IPC: POSIX 消息队列 与 信号
POSIX消息队列可以注册空队列有消息到达时所触发的信号,而信号触发对应的信号处理函数. 下面是一份基本的消息队列和信号处理结合的代码(修改自UNIX网络编程:进程间通信) #include < ...
- UNIX IPC: POSIX 消息队列
首先在我的MAC OSX上试了一下虽然有_POSIX_MESSAGE_PASSING的宏定义,但是用gcc编译会提示没有mqueue.h头文件,先放一边.在Ubuntu上使用正常,不过POSIX消息队 ...
- Linux IPC实践(7) --Posix消息队列
1. 创建/获取一个消息队列 #include <fcntl.h> /* For O_* constants */ #include <sys/stat.h> /* For m ...
- Linux进程间通信(IPC)编程实践(十二)Posix消息队列--基本API的使用
posix消息队列与system v消息队列的区别: (1)对posix消息队列的读总是返回最高优先级的最早消息,对system v消息队列的读则能够返回随意指定优先级的消息. (2)当往一个空队列放 ...
- Linux IPC POSIX 消息队列
模型: #include<mqueue.h> #include <sys/stat.h> #include <fcntl.h> mq_open() //创建/获取消 ...
- Linux环境编程之IPC进程间通信(五):Posix消息队列1
对于管道和FIFO来说.必须应该先有读取者存在.否则先有写入者是没有意义的. 而消息队列则不同,它是一个消息链表,有足够写权限的线程可往别的队列中放置消息,有足够读权限的线程可从队列中取走消息.每一个 ...
随机推荐
- 解题:SDOI 2017 数字表格
题面 反演题,推式子么=.= $\prod\limits_{d=1}^{min(n,m)}\prod\limits_{i=1}^n\prod\limits_{j=1}^m[gcd(i,j)==d]fi ...
- 【agc003D】Anticube
Portal --> agc003D Description 给你\(n\)个数,要从里面选出最多的数满足这些选出来的数中任意两个数的乘积都不是立方数 Solution (为什么感觉最近这种解法 ...
- 在eclipse中安装 Activiti Designer插件
转: Activiti系列——如何在eclipse中安装 Activiti Designer插件 这两天在评估jbpm和Activiti,需要安装一个Activiti Designer插件试用一下. ...
- Chapter 4(栈与队列)
1.栈的顺序存储结构 //*********************************stack_array.h************************************ #ifn ...
- P2054 [AHOI2005]洗牌
P2054 [AHOI2005]洗牌 题目描述 为了表彰小联为Samuel星球的探险所做出的贡献,小联被邀请参加Samuel星球近距离载人探险活动. 由于Samuel星球相当遥远,科学家们要在飞船中度 ...
- 内存操作函数memmove,memcpy,memset
通过字符串的学习,我们知道字符串操作函数的操作对象是字符串,并且它的结束标志是结束符\0,当然这个说的是不 受限制的字符串函数.然而当我们想要将一段内存的数据复制到另一块内存时,我们不能使用字符串操作 ...
- Nginx记录-Nginx介绍
Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 ...
- windows查找端口占用/ 终结端口占用 ------------windows小技巧
前沿 我是一名小程序员,经常通过一些类似tomcat,jettry 等服务器工具 调试项目.有时候莫名其妙的就会出现 程序关闭不正常的情况!去查端口又死活找不到!最后只能重启电脑 后面,在网上查了一些 ...
- noi题库(noi.openjudge.cn) 1.11编程基础之二分查找T01、02、04
T01 查找最接近的元素 描述 在一个非降序列中,查找与给定值最接近的元素. 输入 第一行包含一个整数n,为非降序列长度.1 <= n <= 100000.第二行包含n个整数,为非降序列各 ...
- uploadify IE11 不兼容问题(不显示图片)
1.进入uploadify官网demo : http://www.uploadify.com/demos/ 2. 显示 (确认flash为最新版本) 3.更换其它浏览器一切正常 4.原因:I ...