struct mq_attr
{
long mq_flags; /* message queue flag : 0, O_NONBLOCK */
long mq_maxmsg; /* max number of messages allowed on queue*/
long mq_msgsize; /* max size of a message (in bytes)*/
long mq_curmsgs; /* number of messages currently on queue */
}; /* Receive the oldest from highest priority messages in message queue
MQDES. */
ssize_t mq_receive (mqd_t __mqdes, char *__msg_ptr, size_t __msg_len,
unsigned int *__msg_prio); /* Add message pointed by MSG_PTR to message queue MQDES. */
int mq_send (mqd_t __mqdes, __const char *__msg_ptr, size_t __msg_len,
unsigned int __msg_prio);

程序1:向消息队列中发送消息。可指定消息大小及优先级。

// mqsend.c

#include    "unpipc.h"

int
main(int argc, char **argv)
{
mqd_t mqd;
void *ptr;
size_t len;
unsigned int prio; if (argc != )
err_quit("usage: mqsend <name> <#bytes> <priority>");
len = atoi(argv[]);
prio = atoi(argv[]); mqd = Mq_open(argv[], O_WRONLY); ptr = Calloc(len, sizeof(char)); // 使用calloc函数分配一个长度为len的缓冲区,默认被初始化为0
Mq_send(mqd, ptr, len, prio); // 写入消息队列 exit();
}

程序2:从消息队列读出消息。

// mqreveive.c

#include    "unpipc.h"

int
main(int argc, char **argv)
{
int c, flags;
mqd_t mqd;
ssize_t n;
unsigned int prio;
void *buff;
struct mq_attr attr; flags = O_RDONLY;
while ( (c = Getopt(argc, argv, "n")) != -)
{
switch (c)
{
case 'n': // 命令行中带-n表示以非阻塞模式
flags |= O_NONBLOCK;
break;
}
}
if (optind != argc - )
err_quit("usage: mqreceive [ -n ] <name>"); mqd = Mq_open(argv[optind], flags);
Mq_getattr(mqd, &attr);
// 这里开辟的缓冲区需要>=消息的最大长度,以便能够容纳接收到的消息
buff = Malloc(attr.mq_msgsize); // 根据最大消息的大小开辟缓冲区以接收消息 n = Mq_receive(mqd, buff, attr.mq_msgsize, &prio); // 这里的len长度需要>=消息的最大长度,否则mq_receive会立即返回EMSGSIZE错误
printf("read %ld bytes, priority = %u\n", (long) n, prio); exit();
}

运行结果:

[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量
[dell@localhost pxmsg]$ ./mqcreate1 /msgqueue
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量 [dell@localhost pxmsg]$ sudo mount -t mqueue none /tmp/mqueue
[sudo] password for dell:
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量
-rw-r--r--. dell dell 8月 : msgqueue [dell@localhost pxmsg]$ ./mqsend /msgqueue
[dell@localhost pxmsg]$ ./mqsend /msgqueue
[dell@localhost pxmsg]$ ./mqsend /msgqueue
[dell@localhost pxmsg]$ ls -l /tmp/mqueue/
总用量
-rw-r--r--. dell dell 8月 : msgqueue [dell@localhost pxmsg]$ ./mqreceive /msgqueue
read bytes, priority =
[dell@localhost pxmsg]$ ./mqreceive /msgqueue
read bytes, priority =
[dell@localhost pxmsg]$ ./mqreceive /msgqueue
read bytes, priority =
[dell@localhost pxmsg]$ ./mqreceive /msgqueue
^C
// 这里默认是阻塞方式,当消息队列为空时将会被阻塞,所以下面我们使用非阻塞方式。
[dell@localhost pxmsg]$ ./mqreceive -n /msgqueue
mq_receive error: Resource temporarily unavailable
[dell@localhost pxmsg]$

Unix IPC之Posix消息队列(3)的更多相关文章

  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消息队列(2)

    /* Query status and attributes of message queue MQDES. */ extern int mq_getattr (mqd_t __mqdes, stru ...

  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. bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形

    USACO划水中... 题目中要求经过原点的三角形数目,但这种三角形没什么明显的特点并不好求,所以可以求不经过原点的三角形数量. 对于一个非法三角形,它离原点最近的那条边连接的两个点所连的两条边一定在 ...

  2. SenseTime Ace Coder Challenge 暨 商汤在线编程挑战赛 A. 地铁站

    //其实比赛的时候就想到这方法了,但看到数据太吓人,就没写//看着标程,实际上就是这方法,太坑爹…… /* 假设值为k,对于图中任意两点,圆1半径k/t1,圆2半径k/t2 圆1与圆2的交集为可以设置 ...

  3. Docker registry私有仓库(七)

    Docker registry私有仓库搭建基本几步流程(采用nginx+认证的方式) 1. 申请免费的ssl证书 https://buy.wosiqn.com/free 2. 设置nginx ssl证 ...

  4. 使用SSH-Xsheel文件传输

    借助XShell,使用linux命令sz可以很方便的将服务器上的文件下载到本地,使用rz命令则是把本地文件上传到服务器. 使用命令前需要 apt-get install lrzsz 如果提示源找不到 ...

  5. Hadoop生态圈-Hive的自定义函数之UDTF(User-Defined Table-Generating Functions)

    Hadoop生态圈-Hive的自定义函数之UDTF(User-Defined Table-Generating Functions) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  6. SQL优化:

    今日给人查找数据,时间关系,写个比较粗暴的SQL语句:   #2s587ms#直接将所有表关联,比较粗暴 select go.businessId,dd.dict_namefrom fn_xte.gt ...

  7. (转)Tomcat配置调优与安全总结

    tomcat配置调优与安全总结 作为运维,避免不了与tomcat打交道,然而作者发现网络上关于tomcat配置和调优安全的文章非常散,通过参考各位大神的相关技术文档,根据作者对tomcat的运维经验, ...

  8. 互斥量、条件变量与pthread_cond_wait()函数的使用,详解(一)

    1. 首先pthread_cond_wait 的定义是这样的 The pthread_cond_wait() and pthread_cond_timedwait() functions are us ...

  9. Maven项目导出jar包配置

    <!-- 第一种打包方式 (maven-jar-plugin), 将依赖包和配置文件放到jar包外 --> <build> <sourceDirectory>src ...

  10. 【数据库-MySql】清空所有表格的所有数据

    方式一. drop procedure if exists del_all_tb; delimiter $$ create procedure del_all_tb(db char(20)) begi ...