Unix IPC之Posix消息隊列(1)

部分參考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.htmlhtml

IPC對象的持續性:http://book.51cto.com/art/201006/207275.htmlinux

 

消息隊列能夠認爲是一個消息鏈表,某個進程往一個消息隊列中寫入消息以前,不須要另外某個進程在該隊列上等待消息的達到,這一點與管道和FIFO相反。Posix消息隊列與System V消息隊列的區別以下:shell

1. 對Posix消息隊列的讀老是返回最高優先級的最先消息,對System V消息隊列的讀則能夠返回任意指定優先級的消息。
2. 當往一個空隊列放置一個消息時,Posix消息隊列容許產生一個信號或啓動一個線程,System V消息隊列則不提供相似的機制。編程

Posix消息隊列操做函數以下:網絡

  頭文件及部分定義:ide

#include    <mqueue.h>
typedef int mqd_t;
/* Establish connection between a process and a message queue NAME and
   return message queue descriptor or (mqd_t) -1 on error.  OFLAG determines
   the type of access used.  If O_CREAT is on OFLAG, the third argument is
   taken as a `mode_t', the mode of the created message queue, and the fourth
   argument is taken as `struct mq_attr *', pointer to message queue
   attributes.  If the fourth argument is NULL, default attributes are
   used.  */
extern mqd_t mq_open (__const char *__name, int __oflag, ...)
  __THROW __nonnull ((1));

/* Removes the association between message queue descriptor MQDES and its
   message queue.  */
extern int mq_close (mqd_t __mqdes) __THROW;

/* Remove message queue named NAME.  */
extern int mq_unlink (__const char *__name) __THROW __nonnull ((1));


下面採用上面的函數,寫程序進程測試。函數

程序1(mqcreate1.c):建立一個消息隊列,其名字是做爲命令行參數指定。程序以下:測試

#include    "unpipc.h"

int
main(int argc, char **argv)
{
    int     c, flags;
    mqd_t   mqd; // linux下是int類型

    flags = O_RDWR | O_CREAT;
    while ( (c = Getopt(argc, argv, "e")) != -1)
    {
        switch (c)
        {
            case 'e':
                flags |= O_EXCL;
                break;
        }
    }
    if (optind != argc - 1)
        err_quit("usage: mqcreate [ -e ] <name>");

    mqd = Mq_open(argv[optind], flags, FILE_MODE, NULL);

    Mq_close(mqd);
    exit(0);
}
View Code

程序2(mqunlink.c):刪除一個消息隊列。程序以下:ui

#include    "unpipc.h"

int
main(int argc, char **argv)
{
    if (argc != 2)
        err_quit("usage: mqunlink <name>");

    Mq_unlink(argv[1]);

    exit(0);
}
View Code

注:代碼需在Unix網絡編程-卷2的程序包中才能編譯經過。spa

Posix消息隊列是創建在系統的虛擬文件系統中,若要查看,可將其掛載到系統的文件系統中;

mount命令格式以下:

掛載命令以下:

[dell@localhost pxmsg]$ mkdir /tmp/mqueue
[dell@localhost pxmsg]$ mount -t mqueue none /tmp/mqueue
mount: only root can do that
[dell@localhost pxmsg]$ sudo !-1
sudo mount -t mqueue none /tmp/mqueue
[sudo] password for dell: 

程序運行結果:

[dell@localhost pxmsg]$ ./mqunlink /temp.1234
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
總用量 0
[dell@localhost pxmsg]$ ./mqcreate1 /temp.1234
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
總用量 0
-rw-r--r--. 1 dell dell 80 8月  12 20:10 temp.1234
[dell@localhost pxmsg]$ ./mqunlink /temp.1234
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
總用量 0
[dell@localhost pxmsg]$ 

說明:爲何要這樣作,可在shell下運行一下命令查詢:

man 7 mq_overview

這裏選取部分說明文檔:

Mounting the message queue file system
       On Linux, message queues are created in a virtual file system.   (Other
       implementations  may  also  provide such a feature, but the details are
       likely to differ.)  This file system can be mounted (by the  superuser)
       using the following commands:


           # mkdir /dev/mqueue
           # mount -t mqueue none /dev/mqueue

Each message queue  is  identi fied by a name of the form /somename.  Two processes can operate on the
same queue by passing the same name to mq_open().
相關文章
相關標籤/搜索