博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IPC研究(6) -- 消息队列(message queue)
阅读量:6356 次
发布时间:2019-06-23

本文共 3579 字,大约阅读时间需要 11 分钟。

hot3.png

"In many ways, message queues are like named pipes, but without the complexity associated with opening and closing the pipe.
However, using messages doesn’t get you away from the problems that you have with named pipes,
such as blocking on full pipes.
Message queues provide a reasonably easy and efficient way of passing data between two unrelated
processes. They have the advantage over named pipes that the message queue exists independently of
both the sending and receiving processes, which removes some of the difficulties that occur in synchronizing the opening and closing of named pipes."
以上这段话摘自"Linux程序设计“。对于named pipe的开关同步,真心有点麻烦,如果处理不好,就会导致两个进程互锁。简单示例:
processA |-- named pipe1 --| processB
         |-- named pipe2 --|
A打开pipe1来读(action1),然后打开pipe2来写(action2)。
B打开pipe2来读(action3),然后打开pipe1来写(action4)。
以上这种情况,死锁必然发生!(当然,如果你用nonblock方式打开就另说了。)
#include <sys/msg.h>
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
int msgget(key_t key, int msgflg);
int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg);
int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg);
函数原型。和semaphore以及shared memory很像,没什么特别的。(不愧都是system V一族的。)
struct my_message {
long int message_type;
/* The data you wish to transfer */
}
message必须要以long int开头。可以用它来实现简单优先级队列。
If you simply want to retrieve messages in
the order in which they were sent, set msgtype to 0. If you want to retrieve only messages with a specific message type, set msgtype equal to that value. If you want to receive messages with a type of n or smaller, set msgtype to -n.

然后,无耻又无聊的照书上抄了一个例子:

/**recv**/#include 
#include
#include
#include
#include
#include
struct my_msg_st{ long int my_msg_type; char sometext[BUFSIZ];};int main(){ int running = 1; int msgid; struct my_msg_st msg; long int msg_to_recv = 0; msgid = msgget( (key_t)1234, 0666 | IPC_CREAT); if (msgid < 0) { fprintf(stderr, "msgget failed with error: %d \n", errno); exit(EXIT_FAILURE); } while (running) { if (msgrcv(msgid, (void*)&msg, BUFSIZ, msg_to_recv, 0) == -1) { fprintf(stderr, "msgrcv failed with error %d \n", errno); exit(EXIT_FAILURE); } printf("You wrote: %s", msg.sometext); if (strncmp(msg.sometext, "end", 3) == 0) { running = 0; } } if (msgctl(msgid, IPC_RMID, 0) < 0) { fprintf(stderr, "msgctl(IPC_RMID) failed\n"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS);}/**send**/#include
#include
#include
#include
#include
#include
#define MAX_TEXT 512struct my_msg_st{ long int my_msg_type; char sometext[BUFSIZ];};int main(){ int running = 1; int msgid; struct my_msg_st msg; char buf[BUFSIZ]; msgid = msgget( (key_t)1234, 0666 | IPC_CREAT); if (msgid < 0) { fprintf(stderr, "msgget failed with error: %d \n", errno); exit(EXIT_FAILURE); } while (running) { printf("Enter some text: "); fgets(buf, BUFSIZ, stdin); /* fgets add \0 to the end of the buf */ msg.my_msg_type = 1; strcpy(msg.sometext, buf); if (msgsnd(msgid, (void *)&msg, MAX_TEXT, 0) < 0) { fprintf(stderr, "msgsnd failed\n"); exit(EXIT_FAILURE); } if (strncmp(buf, "end", 3)==0) { running = 0; } } exit(EXIT_SUCCESS);}

结果:

chenqi@chenqi-laptop ~/MyPro/CFiles/IPC/msg_queue $ ./recv &
[1] 19084
chenqi@chenqi-laptop ~/MyPro/CFiles/IPC/msg_queue $ ./send
Enter some text: hello
Enter some text: You wrote: hello
nice
Enter some text: You wrote: nice
to
Enter some text: You wrote: to
meet
Enter some text: You wrote: meet
you
Enter some text: You wrote: you
end
You wrote: end
[1]+  Done                    ./recv

 

转载于:https://my.oschina.net/u/158589/blog/56976

你可能感兴趣的文章
WebSocket详解(六):刨根问底WebSocket与Socket的关系
查看>>
用 Go 写一个轻量级的 ssh 批量操作工具
查看>>
网站设计之合理架构CSS 架构CSS
查看>>
OTP 22.0 RC3 发布,Erlang 编写的应用服务器
查看>>
D语言/DLang 2.085.1 发布,修复性迭代
查看>>
感觉JVM的默认异常处理不够好,既然不好那我们就自己来处理异常呗!那么如何自己处理异常呢?...
查看>>
Java 基础 之 算数运算符
查看>>
Windows下配置安装Git(二)
查看>>
一个最简单的基于Android SearchView的搜索框
查看>>
铁路开通WiFi“钱景”不明
查看>>
Facebook申请专利 或让好友及陌生人相互拼车
查看>>
电力“十三五”规划:地面光伏与分布式的分水岭
查看>>
美联社再告FBI:要求公开请黑客解锁iPhone花费
查看>>
三星电子出售希捷和夏普等四家公司股份
查看>>
任志远:当云计算遇上混合云
查看>>
思科联手发那科 用物联网技术打造无人工厂
查看>>
智慧城市首要在政府利用大数据的智慧
查看>>
2015年物联网行业:巨头展开专利大战
查看>>
以自动化测试撬动遗留系统
查看>>
网络安全初创公司存活之道
查看>>