/* 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)的更多相关文章

  1. Unix IPC之Posix消息队列(1)

    部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...

  2. Unix IPC之Posix消息队列(3)

    struct mq_attr { long mq_flags; /* message queue flag : 0, O_NONBLOCK */ long mq_maxmsg; /* max numb ...

  3. IPC通信:Posix消息队列

    IPC通信:Posix消息队列 消息队列可以认为是一个链表.进程(线程)可以往里写消息,也可以从里面取出消息.一个进程可以往某个消息队列里写消息,然后终止,另一个进程随时可以从消息队列里取走这些消息. ...

  4. UNIX IPC: POSIX 消息队列 与 信号

    POSIX消息队列可以注册空队列有消息到达时所触发的信号,而信号触发对应的信号处理函数. 下面是一份基本的消息队列和信号处理结合的代码(修改自UNIX网络编程:进程间通信) #include < ...

  5. UNIX IPC: POSIX 消息队列

    首先在我的MAC OSX上试了一下虽然有_POSIX_MESSAGE_PASSING的宏定义,但是用gcc编译会提示没有mqueue.h头文件,先放一边.在Ubuntu上使用正常,不过POSIX消息队 ...

  6. Linux IPC实践(7) --Posix消息队列

    1. 创建/获取一个消息队列 #include <fcntl.h> /* For O_* constants */ #include <sys/stat.h> /* For m ...

  7. Linux进程间通信(IPC)编程实践(十二)Posix消息队列--基本API的使用

    posix消息队列与system v消息队列的区别: (1)对posix消息队列的读总是返回最高优先级的最早消息,对system v消息队列的读则能够返回随意指定优先级的消息. (2)当往一个空队列放 ...

  8. Linux IPC POSIX 消息队列

    模型: #include<mqueue.h> #include <sys/stat.h> #include <fcntl.h> mq_open() //创建/获取消 ...

  9. Linux环境编程之IPC进程间通信(五):Posix消息队列1

    对于管道和FIFO来说.必须应该先有读取者存在.否则先有写入者是没有意义的. 而消息队列则不同,它是一个消息链表,有足够写权限的线程可往别的队列中放置消息,有足够读权限的线程可从队列中取走消息.每一个 ...

随机推荐

  1. Luogu 1063 能量项链(动态规划)

    Luogu 1063 能量项链(动态规划) Description 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某 ...

  2. 【树状数组】【P4113】[HEOI2012]采花

    Description 给定一个长度为 \(n\) 的序列,有 \(m\) 次询问,每次询问一段区间,求区间中有多少个数出现次数超过 \(1\) 次 Limitation \(n,~m~\leq~2~ ...

  3. shell 变量匹配

    ${var%pattern} ${var%%pattern} ${var#pattern} ${var##pattern} ${var%pattern},${var%%pattern} 从右边开始匹配 ...

  4. 团体程序设计天梯赛 L1-010. 比较大小

    测试数据: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Code: 先确定最小,然后确定第二小 #include <stdio.h> #include < ...

  5. hackerrank答案

    regex: https://github.com/taiyang-li/hackerrank/tree/master/hackerrank/regex-exercise awk: https://g ...

  6. 彻底理解 Python 生成器

    1. 生成器定义 在Python中,一边循环一边计算的机制,称为生成器:generator. 2. 为什么要有生成器 列表所有数据都在内存中,如果有海量数据的话将会非常耗内存. 如:仅仅需要访问前面几 ...

  7. css基础--常用css属性02

    上篇地址:css基础--常用css属性01 本文参考菜鸟教程和w3school 1  浮动和清除浮动 在上篇的第十一节--定位中说道: CSS 有三种基本的定位机制:普通流.浮动和绝对定位. 普通流和 ...

  8. solr6.3.0升级与IK动态词库自动加载

    摘要:对于中文的搜索来说,词库系统是一个很比较重要的模块,本篇以IK分词器为例子,介绍如何让分词器从缓存或文件系统中自动按照一定频次进行加载扩展词库 Lucene.Solr或ElasticStack如 ...

  9. 如何使用vuejs过滤器

    大家再使用vue做项目时,查询功能当然必不可少,这就得使用vue强大的filter啦.其实vue内置的两个属性filterBy和orderBy已经能满足部分需求了,但是她更大的的魅力在于自定义filt ...

  10. Nginx跳转Tomcat

    conf配置: server {         listen       80;         server_name www.-------.com;         server_name_i ...