2008年8月27日 星期三

Linux POSIX message queue 经验总结

Linux的Posix message queue是一个特殊的文件系统(file system).在使用之前需要mount
$ mkdir /dev/mqueue
$ mount -t mqueue none /dev/mqueue

the default max message number( attr->mq_maxmsg): 10
the upper limit of max message number ( attr->mq_maxmsg): HARD_MAX or (131072 / sizeof(void *)) (32768 on Linux/86).
The default and minimum value for msgsize_max is 8192 bytes; the upper limit is INT_MAX (2147483647 on Linux/86).
system message queue max number: default 256, the range 0 to INT_MAX.

if a message que is not removed by mq_unlink(), a message queue will exist until the system is shut down.

message queue的命名规则,
Each message queue is identified by a name of the form: /somename.
Two processes can operate on the same queue by passing the same name to mq_open().

函数用法:
Message queues are created and opened using mq_open(3)
Messages are transferred to and from a queue using mq_send(3) and mq_receive(3).
When a process has finished using the queue, it closes it using mq_close(3).
When the queue is no longer required, it can be deleted using mq_unlink(3).
Queue attributes can be retrieved and (in some cases) modified using mq_getattr(3) and mq_setattr(3).
A process can request asynchronous notification of the arrival of a message on a previously empty queue using mq_notify(3).

优先级定义
Message priorities range from 0 (low) to sysconf(_SC_MQ_PRIO_MAX) - 1 (high). On Linux, sysconf(_SC_MQ_PRIO_MAX) returns 32768, but POSIX.1-2001 only requires an implementation to support priorities in the range 0 to 31;

特殊Notify
mq_notify,可以不用阻塞方式来获取有空到队列有消息的通知,用来注册callback函数。

0 评论: