zoukankan      html  css  js  c++  java
  • Unix IPC之Posix消息队列(1)

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

    IPC对象的持续性:http://book.51cto.com/art/201006/207275.htm

    消息队列可以认为是一个消息链表,某个进程往一个消息队列中写入消息之前,不需要另外某个进程在该队列上等待消息的达到,这一点与管道和FIFO相反。Posix消息队列与System V消息队列的区别如下:

    1. 对Posix消息队列的读总是返回最高优先级的最早消息,对System V消息队列的读则可以返回任意指定优先级的消息。
    2. 当往一个空队列放置一个消息时,Posix消息队列允许产生一个信号或启动一个线程,System V消息队列则不提供类似的机制。

    Posix消息队列操作函数如下:

      头文件及部分定义:

    #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):删除一个消息队列。程序如下:

    #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的程序包中才能编译通过。

    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().
  • 相关阅读:
    Mysql的左外连接丶右外连接与内连接的区别
    常见的异常种类
    Mysql
    JSTL 标签库
    VMware的虚拟机为什么ip地址老是自动变化?
    redis出现的问题
    在Linux系统下启动了tomcat,但是在游览器中进步了服务
    Linux下安装JDK 与 tomcat
    wamp配置多站点域名
    终端链接操作redis
  • 原文地址:https://www.cnblogs.com/fengkang1008/p/4725497.html
Copyright © 2011-2022 走看看