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来说.必须应该先有读取者存在.否则先有写入者是没有意义的. 而消息队列则不同,它是一个消息链表,有足够写权限的线程可往别的队列中放置消息,有足够读权限的线程可从队列中取走消息.每一个 ...
随机推荐
- 【codeforces gym】Increasing Costs
Portal --> Increasing Costs Description 给你一个\(n\)个点无重边无自环的无向连通图,每条边有一个边权,对于每一条边,询问去掉这条边之后有多少个点到\( ...
- 在mvc中 怎么给@Html.HiddenFor()赋值
@Html.HiddenFor(model => model.CreatedBy, new { @value=currentInfo.UserID}) value始终是null@Html.Tex ...
- python的reduce函数的使用方法详解以及使用案例,相加,相乘(处理一个序列,然后把序列进程合并操作)
1.求列表的数字相加之和,还是之前的习惯,写for循环来实现 num_1=[1,2,3,4,5,6,7,8,9] a=0 for n in num_1: #a=a+n a+=n print (a) C ...
- pymc
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- Java基础-包(package)的声明与访问
Java基础-包(package)的声明与访问 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.包的概念 Java中的包,其实就是我们电脑系统中的文件夹,包里存放的是程序员生成的 ...
- python操作mongo脚本
#!/usr/bin/python# -*- coding: utf-8 -*- import sysimport osimport jsonfrom pymongo import MongoClie ...
- bzoj千题计划109:bzoj1019: [SHOI2008]汉诺塔
http://www.lydsy.com/JudgeOnline/problem.php?id=1019 题目中问步骤数,没说最少 可以大胆猜测移动方案唯一 (真的是唯一但不会证) 设f[i][j] ...
- DP整理(未完待续)
一.资源问题 T1 机器分配 已知条件:每家公司分配x台机器的盈利 令f[i][j]表示前i公司分配j台机器的最优解 转移:f[i][j]=max(f[i-1][j-k]+w[i][k]) 初始化:f ...
- android onActivityResult的执行
1.如果activity中重写了onActivityResult函数,同时添加在该activity的fragment也重写了onActivtyResult函数,那么会执行Activity的onActi ...
- Python核心编程——Chapter9
好久没写过Python了,前一阵子忙这忙那的,都几乎把Python给丢掉了,话不多说,马上开始. 9.1.文件过滤.显示一个文件的所有行,并且忽略以井号开头的行. 其实这个题目比较基础,用shell语 ...