1. 创建/获取一个消息队列

#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <mqueue.h>
mqd_t mq_open(const char *name, int oflag);	//专用于打开一个消息队列
mqd_t mq_open(const char *name, int oflag, mode_t mode,
              struct mq_attr *attr);

参数:

name:  消息队列名字;

oflag: 与open函数类型, 可以是O_RDONLY, O_WRONLY, O_RDWR, 还可以按位或上O_CREAT, O_EXCL, O_NONBLOCK.

mode: 如果oflag指定了O_CREAT, 需要指定mode参数;

attr: 指定消息队列的属性;

返回值:

成功: 返回消息队列文件描述符;

失败: 返回-1;

注意-Posix IPC名字限制:

1. 必须以”/”开头, 并且后面不能还有”/”, 形如:/file-name;

2. 名字长度不能超过NAME_MAX

3. 链接时:Link with -lrt.

/** System V 消息队列

通过msgget来创建/打开消息队列

int msgget(key_t key, int msgflg);

**/

2. 关闭一个消息队列

int mq_close(mqd_t mqdes);
/** System V 消息队列没有类似的该函数调用**/

3. 删除一个消息队列

int mq_unlink(const char *name);
/** System V 消息队列
通过msgctl函数, 并将cmd指定为IPC_RMID来实现
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
**/
//示例
int main()
{
    mqd_t mqid = mq_open("/abc", O_CREAT|O_RDONLY, 0666, NULL);
    if (mqid == -1)
        err_exit("mq_open error");
    cout << "mq_open success" << endl;
    mq_close(mqid);
    mq_unlink("/abc");
    cout << "unlink success" << endl;
}

4. 获取/设置消息队列属性

int mq_getattr(mqd_t mqdes, struct mq_attr *attr);
int mq_setattr(mqd_t mqdes, struct mq_attr *newattr,
                        struct mq_attr *oldattr);

参数:

newattr: 需要设置的属性

oldattr: 原来的属性

//struct mq_attr结构体说明
struct mq_attr
{
    long mq_flags;       /* Flags: 0 or O_NONBLOCK */
    long mq_maxmsg;      /* Max. # of messages on queue: 消息队列能够保存的消息数 */
    long mq_msgsize;     /* Max. message size (bytes): 消息的最大长度 */
    long mq_curmsgs;     /* # of messages currently in queue: 消息队列当前保存的消息数 */
};

/** System V 消息队列

通过msgctl函数, 并将cmd指定为IPC_STAT/IPC_SET来实现

int msgctl(int msqid, int cmd, struct msqid_ds *buf);

**/

/** 示例: 获取消息队列的属性
**/
int main(int argc,char **argv)
{
    mqd_t mqid = mq_open("/test", O_RDONLY|O_CREAT, 0666, NULL);
    if (mqid == -1)
        err_exit("mq_open error");

    struct mq_attr attr;
    if (mq_getattr(mqid, &attr) == -1)
        err_exit("mq_getattr error");
    cout << "Max messages on queue: " << attr.mq_maxmsg << endl;
    cout << "Max message size: " << attr.mq_msgsize << endl;
    cout << "current messages: " << attr.mq_curmsgs << endl;

    mq_close(mqid);
    return 0;
}

5. 发送消息

int mq_send(mqd_t mqdes, const char *msg_ptr,
           size_t msg_len, unsigned msg_prio);

参数:

msg_ptr: 指向需要发送的消息的指针

msg_len: 消息长度

msg_prio: 消息的优先级

/** System V 消息队列

通过msgsnd函数来实现消息发送

int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

**/

/** 示例: 向消息队列中发送消息, prio需要从命令行参数中读取 **/
struct Student
{
    char name[36];
    int age;
};
int main(int argc,char **argv)
{
    if (argc != 2)
        err_quit("./send <prio>");

    mqd_t mqid = mq_open("/test", O_WRONLY|O_CREAT, 0666, NULL);
    if (mqid == -1)
        err_exit("mq_open error");

    struct Student stu = {"xiaofang", 23};
    unsigned prio = atoi(argv[1]);
    if (mq_send(mqid, (const char *)&stu, sizeof(stu), prio) == -1)
        err_exit("mq_send error");

    mq_close(mqid);
    return 0;
}

6. 从消息队列中读取消息

ssize_t mq_receive(mqd_t mqdes, char *msg_ptr,
                       size_t msg_len, unsigned *msg_prio);

参数:

msg_len: 读取的消息的长度, 注意: 此值一定要等于mq_attr::mq_msgsize的值, 该值可以通过mq_getattr获取, 但一般是8192字节     [this must be greater than the mq_msgsize attribute of the queue (see mq_getattr(3)).]

msg_prio: 保存获取的消息的优先级

返回值:

成功: 返回读取的消息的字节数

失败: 返回-1

注意: 读取的永远是消息队列中优先级最高的最早的消息, 如果消息队列为, 如果不指定为非阻塞模式, 则mq_receive会阻塞;

/** System V 消息队列

通过msgrcv函数来实现消息发送的

ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);

**/

/** 示例: 从消息队列中获取消息 **/
int main(int argc,char **argv)
{
    mqd_t mqid = mq_open("/test", O_RDONLY);
    if (mqid == -1)
        err_exit("mq_open error");

    struct Student buf;
    int nrcv;
    unsigned prio;
    struct mq_attr attr;
    if (mq_getattr(mqid, &attr) == -1)
        err_exit("mq_getattr error");

    if ((nrcv = mq_receive(mqid, (char *)&buf, attr.mq_msgsize, &prio)) == -1)
        err_exit("mq_receive error");

    cout << "receive " << nrcv << " bytes, priority: " << prio << ", name: "
         << buf.name << ", age: " << buf.age << endl;

    mq_close(mqid);
    return 0;
}

7. 建立/删除消息到达通知事件

int mq_notify(mqd_t mqdes, const struct sigevent *sevp);

参数sevp:

NULL: 表示撤销已注册通知;

非空: 表示当消息到达且消息队列当前为空, 那么将得到通知;

通知方式:

1. 产生一个信号, 需要自己绑定

2. 创建一个线程, 执行指定的函数

注意: 这种注册的方式只是在消息队列从空到非空时才产生消息通知事件, 而且这种注册方式是一次性的!

//sigevent结构体
struct sigevent
{
    int          sigev_notify; /* Notification method */
    int          sigev_signo;  /* Notification signal */
    union sigval sigev_value;  /* Data passed with notification */
    void       (*sigev_notify_function) (union sigval);  /* Function used for thread notification (SIGEV_THREAD) */
    void        *sigev_notify_attributes; /* Attributes for notification thread (SIGEV_THREAD) */
    pid_t        sigev_notify_thread_id; /* ID of thread to signal (SIGEV_THREAD_ID) */
};
union sigval            /* Data passed with notification */
{
    int     sival_int;         /* Integer value */
    void   *sival_ptr;         /* Pointer value */
};

sigev_notify代表通知的方式: 一般常用两种取值:SIGEV_SIGNAL, 以信号方式通知; SIGEV_THREAD, 以线程方式通知

如果以信号方式通知: 则需要设定一下两个参数:

sigev_signo: 信号的代码

sigev_value: 信号的附加数据(实时信号)

如果以线程方式通知: 则需要设定以下两个参数:

sigev_notify_function

sigev_notify_attributes

/** Posix IPC所特有的功能, System V没有 **/

/**示例: 将下面程序多运行几遍, 尤其是当消息队列”从空->非空”, 多次”从空->非空”, 当消息队列不空时运行该程序时, 观察该程序的状态;
**/
mqd_t mqid;
long size;
void sigHandlerForUSR1(int signo)
{
    //将数据的读取转移到对信号SIGUSR1的响应函数中来
    struct Student buf;
    int nrcv;
    unsigned prio;
    if ((nrcv = mq_receive(mqid, (char *)&buf, size, &prio)) == -1)
        err_exit("mq_receive error");

    cout << "receive " << nrcv << " bytes, priority: " << prio << ", name: "
         << buf.name << ", age: " << buf.age << endl;
}

int main(int argc,char **argv)
{
    // 安装信号响应函数
    if (signal(SIGUSR1, sigHandlerForUSR1) == SIG_ERR)
        err_exit("signal error");

    mqid = mq_open("/test", O_RDONLY);
    if (mqid == -1)
        err_exit("mq_open error");

    // 获取消息的最大长度
    struct mq_attr attr;
    if (mq_getattr(mqid, &attr) == -1)
        err_exit("mq_getattr error");
    size = attr.mq_msgsize;

    // 注册消息到达通知事件
    struct sigevent event;
    event.sigev_notify = SIGEV_SIGNAL;  //指定以信号方式通知
    event.sigev_signo = SIGUSR1;        //指定以SIGUSR1通知
    if (mq_notify(mqid, &event) == -1)
        err_exit("mq_notify error");

    //死循环, 等待信号到来
    while (true)
        pause();

    mq_close(mqid);
    return 0;
}
/** 示例:多次注册notify, 这样就能过多次接收消息, 但是还是不能从队列非空的时候进行接收, 将程序改造如下:
**/
mqd_t mqid;
long size;
struct sigevent event;
void sigHandlerForUSR1(int signo)
{
    // 注意: 是在消息被读走之前进行注册,
    // 不然该程序就感应不到消息队列"从空->非空"的一个过程变化了
    if (mq_notify(mqid, &event) == -1)
        err_exit("mq_notify error");

    //将数据的读取转移到对信号SIGUSR1的响应函数中来
    struct Student buf;
    int nrcv;
    unsigned prio;
    if ((nrcv = mq_receive(mqid, (char *)&buf, size, &prio)) == -1)
        err_exit("mq_receive error");

    cout << "receive " << nrcv << " bytes, priority: " << prio << ", name: "
         << buf.name << ", age: " << buf.age << endl;
}

int main(int argc,char **argv)
{
    // 安装信号响应函数
    if (signal(SIGUSR1, sigHandlerForUSR1) == SIG_ERR)
        err_exit("signal error");

    mqid = mq_open("/test", O_RDONLY);
    if (mqid == -1)
        err_exit("mq_open error");

    // 获取消息的最大长度
    struct mq_attr attr;
    if (mq_getattr(mqid, &attr) == -1)
        err_exit("mq_getattr error");
    size = attr.mq_msgsize;

    // 注册消息到达通知事件
    event.sigev_notify = SIGEV_SIGNAL;  //指定以信号方式通知
    event.sigev_signo = SIGUSR1;        //指定以SIGUSR1通知
    if (mq_notify(mqid, &event) == -1)
        err_exit("mq_notify error");

    //死循环, 等待信号到来
    while (true)
        pause();

    mq_close(mqid);
    return 0;
}

mq_notify 注意点总结:

1. 任何时刻只能有一个进程可以被注册为接收某个给定队列的通知;

2. 当有一个消息到达某个先前为空的队列, 而且已有一个进程被注册为接收该队列的通知时, 只有没有任何线程阻塞在该队列的mq_receive调用的前提下, 通知才会发出;

3. 当通知被发送给它的注册进程时, 该进程的注册被撤销. 进程必须再次调用mq_notify以重新注册(如果需要的话),但是要注意: 重新注册要放在从消息队列读出消息之前而不是之后(如同示例程序);

附-查看已经成功创建的Posix消息队列

#其存在与一个虚拟文件系统中, 需要将其挂载到系统中才能查看

Mounting the message queue filesystem On Linux, message queues are created in a virtual filesystem.

(Other implementations may also  provide such a feature, but the details are likely to differ.)  This

file system can be mounted (by the superuser, 注意是使用root用户才能成功) using the following commands:

mkdir /dev/mqueue

mount -t mqueue none /dev/mqueue

还可以使用cat查看该消息队列的状态, rm删除:

cat /dev/mqueue/abc

rm abc

还可umount该文件系统

umount /dev/mqueue

附-Makefile

.PHONY: clean all
CC = g++
CPPFLAGS = -Wall -g
BIN = main
SOURCES = $(BIN.=.cpp)
all: $(BIN)

%.o: %.c
	$(CC) $(CPPFLAGS) -c $^ -o $@
main: main.o
	$(CC) $(CPPFLAGS) $^ -lrt -o $@

clean:
	-rm -rf $(BIN) *.o bin/ obj/ core

Linux IPC实践(7) --Posix消息队列的更多相关文章

  1. [转]Linux进程通信之POSIX消息队列

    进程间的消息队列可以用这个实现,学习了下. http://blog.csdn.net/anonymalias/article/details/9799645?utm_source=tuicool&am ...

  2. linux c编程:Posix消息队列

    Posix消息队列可以认为是一个消息链表. 有足够写权限的线程可以往队列中放置消息, 有足够读权限的线程可以从队列中取走消息 在某个进程往一个队列写入消息前, 并不需要另外某个进程在该队列上等待消息的 ...

  3. linux网络编程之posix消息队列

    在前面已经学习了System v相关的IPC,今天起学习posix相关的IPC,关于这两者的内容区别,简单回顾一下: 而今天先学习posix的消息队列,下面开始: 接下来则编写程序来创建一个posix ...

  4. Linux IPC实践(10) --Posix共享内存

    1. 创建/获取一个共享内存 #include <sys/mman.h> #include <sys/stat.h> /* For mode constants */ #inc ...

  5. POSIX 消息队列相关问题

    一.查看和删除消息队列要想看到创建的posix消息队列,需要在root用户下执行以下操作:# mkdir /dev/mqueue# mount -t mqueue none /dev/mqueue删除 ...

  6. Linux进程间通信(IPC)编程实践(十二)Posix消息队列--基本API的使用

    posix消息队列与system v消息队列的区别: (1)对posix消息队列的读总是返回最高优先级的最早消息,对system v消息队列的读则能够返回随意指定优先级的消息. (2)当往一个空队列放 ...

  7. Linux IPC实践(4) --System V消息队列(1)

    消息队列概述 消息队列提供了一个从一个进程向另外一个进程发送一块数据的方法(仅局限于本机); 每个数据块都被认为是有一个类型,接收者进程接收的数据块可以有不同的类型值. 消息队列也有管道一样的不足:  ...

  8. Linux IPC POSIX 消息队列

    模型: #include<mqueue.h> #include <sys/stat.h> #include <fcntl.h> mq_open() //创建/获取消 ...

  9. Linux环境编程之IPC进程间通信(五):Posix消息队列1

    对于管道和FIFO来说.必须应该先有读取者存在.否则先有写入者是没有意义的. 而消息队列则不同,它是一个消息链表,有足够写权限的线程可往别的队列中放置消息,有足够读权限的线程可从队列中取走消息.每一个 ...

随机推荐

  1. JavaScript正则表达式模式匹配(6)——常用的正则表达式

    1.检查邮政编码 var pattern=/[1-9][0-9]{5}/; // 必须是6位,必须是数字,第一位不能为0 var str='274200'; alert(pattern.test(st ...

  2. 《Python黑帽子》_1设置Python环境安装wingIDE

    1首先你得有个Kali 检测python版本 安装pip 2安装wingIDE 网站 http://www.wingware.com 获取WingIDE 3解压wingide并且解决依赖关系 下载后在 ...

  3. android M Launcher之LauncherModel (三)

    通过前两篇的分析,我们已经知道了LauncherModel的初始化及工作流程,如果您还不熟悉的话请看前两篇博文 android M Launcher之LauncherModel (一) android ...

  4. Swift中String和NSString的一个不同之处

    我们知道在Swift中String和NSString是可以互相转换使用的-额-应该是在绝大数情况下可以互相转换使用.在某些情况下可能还有一丝丝略微的差别:比如在涉及到处理字符串中字符索引的时候. 我们 ...

  5. springMVC源码分析--ControllerBeanNameHandlerMapping(八)

    在上一篇博客springMVC源码分析--AbstractControllerUrlHandlerMapping(六)中我们介绍到AbstractControllerUrlHandlerMapping ...

  6. 【安卓开发】为什么不能往Android的Application对象里存储数据

    在一个App里面总有一些数据需要在多个地方用到.这些数据可能是一个 session token,一次费时计算的结果等.通常为了避免activity之间传递对象的开销 ,这些数据一般都会保存到持久化存储 ...

  7. antlr 4新特性总结及与antlr v3的不同

    antlr 4新特性总结及与antlr v3的不同 学习曲线低.antlr v4相对于v3,v4更注重于用更接近于自然语言的方式去解析语言.比如运算符优先级,排在最前面的规则优先级最高: 层次更清晰. ...

  8. [Vim]新建python文件自动添加python header

    使用vim脚本来实现的,使用了模板,几行代码就能实现很实用. ~/.vimrc 中的代码 "auto add pyhton header --start autocmd BufNewFile ...

  9. Vibrator控制手机震动

    Vibrator控制手机震动 效果图 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9049755 添加权限 & ...

  10. shell的输入和输出

    1.echo    echo [option] string -e 解析转义字符 -n 回车不换行,linux系统默认回车换行 转移字符 \c \t \f \n #!/bin/bash #echo e ...