Unix IPC之Posix消息队列(2)
/* Query status and attributes of message queue MQDES. */
extern int mq_getattr (mqd_t __mqdes, struct mq_attr *__mqstat)
__THROW __nonnull (()); /* Set attributes associated with message queue MQDES and if OMQSTAT is
not NULL also query its old attributes. */
extern int mq_setattr (mqd_t __mqdes,
__const struct mq_attr *__restrict __mqstat,
struct mq_attr *__restrict __omqstat)
__THROW __nonnull (());
struct mq_attr {
long mq_flags; /* message queue flags */
long mq_maxmsg; /* maximum number of messages */
long mq_msgsize; /* maximum message size */
long mq_curmsgs; /* number of messages currently queued */
long __reserved[]; /* ignored for input, zeroed for output */
};
程序1(mqgetattr.c):获取一个消息队列的默认属性。(以大写开头的函数都是对应函数的包裹函数,仅仅在里面添加了出错信息)
// mqgetattr.c
#include "unpipc.h" int
main(int argc, char **argv)
{
mqd_t mqd;
struct mq_attr attr; if (argc != )
err_quit("usage: mqgetattr <name>"); mqd = Mq_open(argv[], O_RDONLY); Mq_getattr(mqd, &attr);
printf("max #msgs = %ld, max #bytes/msg = %ld, "
"#currently on queue = %ld\n",
attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs); Mq_close(mqd);
exit();
}
运行结果:
[dell@localhost pxmsg]$ ./mqcreate1 /hello.
[dell@localhost pxmsg]$ ./mqgetattr /hello.
max #msgs = , max #bytes/msg = , #currently on queue =
[dell@localhost pxmsg]$
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/hello.
-rw-r--r--. dell dell 8月 : /tmp/mqueue/hello.
即:80KB = 10 * 8192B
程序2:指定消息队列的最大消息个数及每个消息的最大长度。
#include "unpipc.h" struct mq_attr attr; /* mq_maxmsg and mq_msgsize both init to 0 */ int
main(int argc, char **argv)
{
int c, flags;
mqd_t mqd; flags = O_RDWR | O_CREAT;
while ( (c = Getopt(argc, argv, "em:z:")) != -)
{
switch (c)
{
case 'e':
flags |= O_EXCL;
break; case 'm':
attr.mq_maxmsg = atol(optarg);
break; case 'z':
attr.mq_msgsize = atol(optarg);
break;
}
}
if (optind != argc - )
err_quit("usage: mqcreate [ -e ] [ -m maxmsg -z msgsize ] <name>"); if ((attr.mq_maxmsg != && attr.mq_msgsize == ) ||
(attr.mq_maxmsg == && attr.mq_msgsize != ))
err_quit("must specify both -m maxmsg and -z msgsize"); mqd = Mq_open(argv[optind], flags, FILE_MODE,
(attr.mq_maxmsg != ) ? &attr : NULL); Mq_close(mqd);
exit();
}
运行结果:
[dell@localhost pxmsg]$ cat /proc/sys/fs/mqueue/msg_max [dell@localhost pxmsg]$ cat /proc/sys/fs/mqueue/msgsize_max [dell@localhost pxmsg]$ ./mqcreate -m -z /hello.
[dell@localhost pxmsg]$ ./mqgetattr /hello.
max #msgs = , max #bytes/msg = , #currently on queue =
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/hello.
-rw-r--r--. dell dell 8月 : /tmp/mqueue/hello.
[dell@localhost pxmsg]$
说明:这里设置时不能超过系统设定的参数。
Unix IPC之Posix消息队列(2)的更多相关文章
- Unix IPC之Posix消息队列(1)
部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...
- Unix IPC之Posix消息队列(3)
struct mq_attr { long mq_flags; /* message queue flag : 0, O_NONBLOCK */ long mq_maxmsg; /* max numb ...
- 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来说.必须应该先有读取者存在.否则先有写入者是没有意义的. 而消息队列则不同,它是一个消息链表,有足够写权限的线程可往别的队列中放置消息,有足够读权限的线程可从队列中取走消息.每一个 ...
随机推荐
- bzoj 1539: [POI2005]Dwu-Double-row
假设一列交换表示为1,不换表示为0. 身高相同的两个人相当于给其中两列了一个限制条件,要么是两个必须相等,要么一个为零一个为一. 有了关系后我们就可以把每列当成一个点建边,边权为0表示必须相同,1为必 ...
- git 生成公匙私匙
直接 ssh-keygen -t rsa -C "*********@qq.com"也行 git config --global user.name “用户名” 用户名随便起!你能 ...
- ubuntu 安装python3.6.6
http://www.cnblogs.com/yhongji/p/9383857.html https://www.jianshu.com/p/1565f38f4236 ./configure --w ...
- WHY C++ ?(by Herb Sutter) & C++17 standard
WHY C++ ? C++
- P3355 骑士共存问题
P3355 骑士共存问题 题目描述 在一个 n*n (n <= 200)个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入 对于给定的 n*n ...
- Java基础-集合的嵌套
Java基础-集合的嵌套 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.静态导入 静态导入是在JDK1.5后的新特性,可以减少开发的代码量,但是实际用处是很一般,静态导入的标准 ...
- java基础-数组的折半查找原理
java基础-数组的折半查找原理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 如果让你写一个数组的查找功能,需求如下:在一个数组中,找一个元素,是否存在于数组中, 如果存在就返回 ...
- codevs 1070 普通递归关系
1070 普通递归关系 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 考虑以下定义在非负整数n上的递归关系 f( ...
- Linux安装python模块
# 依赖解决 # yum -y install epel-release python-devel gcc # 安装模块 # pip install psutil (若没有pip命令,可通过 yum ...
- JavaScript事件代理入门
事件代理(Event Delegation),又称之为事件委托.是 JavaScript 中常用绑定事件的常用技巧. 顾名思义,“事件代理”即是把原本需要绑定的事件委托给父元素,让父元素担当事件监听的 ...