/* 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. 野指针(Wild pointer)和悬垂指针(dangling pointer)

    详细参考如下: Dangling pointer(悬垂指针.迷途指针)和 Wild pointer(野指针) 迷途指针经常出现在混杂使用malloc() 和 free() 库调用: 当指针指向的内存释 ...

  2. svnsync备份

    参考:https://www.cnblogs.com/zz0412/p/svnsync.html https://blog.csdn.net/windone0109/article/details/4 ...

  3. Linux操作系统常见安装方式

    Linux操作系统常见安装方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在window操作系统安装程序只需要点点鼠标就能搞定的事情,但是在Linux操作系统中,尤其是字符终端 ...

  4. Shell记录-Shell命令(定时任务)

    在Linux系统中, at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron(crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因 ...

  5. centos7.2 rabbitmq3.6.2源码部署

    1.安装所有依赖包yum install -y gcc ncurses ncurses-base ncurses-devel ncurses-libs ncurses-static ncurses-t ...

  6. javascript精雕细琢(一):var let const function声明的区别

    目录 引言 一.var 二.let 三.const 四.function 五.总结 引言        在学习javascript的过程中,变量是无时无刻不在使用的.那么相对应的,变量声明方法也如是. ...

  7. 51 Free Data Science Books

    51 Free Data Science Books A great collection of free data science books covering a wide range of to ...

  8. html_entity_decode() 将 HTML 实体转成字符原型

    PHP html_entity_decode() 适用于PHP 4.3.0+,将HTML 实体转成字符. html_entity_decode(包含HTML 实体的字符串, 可选如何解码引号, 可选字 ...

  9. AspNetPager控件的最基本用法

    AspNetPager控件是一个基于.net的第三方免费开源控件,具有开发高效.使用方便.功能完整等优点.它弥补了GridView内置分页以及PageDatasource类辅助分页的不足,将分页数据逻 ...

  10. 【转】C# Graphics类详解

    Brush 类 .NET Framework 4 定义用于填充图形形状(如矩形.椭圆.饼形.多边形和封闭路径)的内部的对象. 属于命名空间:  System.Drawing 这是一个抽象基类,不能进行 ...