/* 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. 从function的定义看JavaScript的预加载

    在JavaScript中定义一个函数,有两种写法: function ftn(){} // 第一种 var ftn = function(){} // 第二种 有人说,这两种写法是完全等价的.但是在解 ...

  2. md5sum/opensll md5

    http://m.blog.csdn.net/article/details?id=42041329 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Mess ...

  3. python学习(24) 使用Xpath解析并抓取美女图片

    Xpath最初用来处理XML解析,同样适用于HTML文档处理.相比正则表达式更方便一些 Xpath基本规则 nodename 表示选取nodename 节点的所有子节点 / 表示当前节点的直接子节点 ...

  4. 802.11 ------ Beacon帧、Beacon Interval、TBTT、Listen Interval、TIM、DTIM

    Beacon帧:Beacon的实际发送一般都是采用最低速率的,其包含两个原因,1)beacon帧是一个广播帧,其没有ACK反馈,所以无法设置重传机制,2)beacon帧目的是广播AP的基本信息,所以希 ...

  5. GetVersionEx 正确获取windows10版本

    vs2008直接将下面xml保存成文件添加到资源文件 vc的话insert-->Resource-->Custom-->输入24,ok-->id改为1-->把下面内容保存 ...

  6. python 多线程中的同步锁 Lock Rlock Semaphore Event Conditio

    摘要:在使用多线程的应用下,如何保证线程安全,以及线程之间的同步,或者访问共享变量等问题是十分棘手的问题,也是使用多线程下面临的问题,如果处理不好,会带来较严重的后果,使用python多线程中提供Lo ...

  7. Java HashMap源码分析

    貌似HashMap跟ConcurrentHashMap是面试经常考的东西,抽空来简单分析下它的源码 构造函数 /** * Constructs an empty <tt>HashMap&l ...

  8. 贪心问题:区间覆盖 POJ 1328 Rader Installation

    题目:http://poj.org/problem?id=1328 题意:给定海岛个数,雷达半径,输入各个海岛坐标,求能覆盖所有海岛的最少雷达数 题解: 1. 贪心的区间覆盖问题,尽量让每个雷达覆盖更 ...

  9. http协议POST请求头content-type主要的四种取值

    介绍: 在此之前对content-type理解很肤浅,因此必须记录下来现在的理解,以便回顾 Content-Type,从名字上可以理解为内容类型,但在互联网上专业术语叫“媒体类型”,即MediaTyp ...

  10. 值得关注的sql-on-hadoop框架

    http://www.infoq.com/cn/news/2014/06/sql-on-hadoop 数据的操作语言是SQL,因此很多工具的开发目标自然就是能够在Hadoop上使用SQL.这些工具有些 ...