Linux进程间通信——使用消息队列
- int msgget(key_t, key, int msgflg);
- int msgsend(int msgid, const void *msg_ptr, size_t msg_sz, int msgflg);
- struct my_message{
- long int message_type;
- /* The data you wish to transfer*/
- };
- int msgrcv(int msgid, void *msg_ptr, size_t msg_st, long int msgtype, int msgflg);
- int msgctl(int msgid, int command, struct msgid_ds *buf);
- struct msgid_ds
- {
- uid_t shm_perm.uid;
- uid_t shm_perm.gid;
- mode_t shm_perm.mode;
- };
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/msg.h>
- struct msg_st
- {
- long int msg_type;
- char text[BUFSIZ];
- };
- int main()
- {
- int running = 1;
- int msgid = -1;
- struct msg_st data;
- long int msgtype = 0; //注意1
- //建立消息队列
- msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
- if(msgid == -1)
- {
- fprintf(stderr, "msgget failed with error: %d\n", errno);
- exit(EXIT_FAILURE);
- }
- //从队列中获取消息,直到遇到end消息为止
- while(running)
- {
- if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1)
- {
- fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
- exit(EXIT_FAILURE);
- }
- printf("You wrote: %s\n",data.text);
- //遇到end结束
- if(strncmp(data.text, "end", 3) == 0)
- running = 0;
- }
- //删除消息队列
- if(msgctl(msgid, IPC_RMID, 0) == -1)
- {
- fprintf(stderr, "msgctl(IPC_RMID) failed\n");
- exit(EXIT_FAILURE);
- }
- exit(EXIT_SUCCESS);
- }
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/msg.h>
- #include <errno.h>
- #define MAX_TEXT 512
- struct msg_st
- {
- long int msg_type;
- char text[MAX_TEXT];
- };
- int main()
- {
- int running = 1;
- struct msg_st data;
- char buffer[BUFSIZ];
- int msgid = -1;
- //建立消息队列
- msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
- if(msgid == -1)
- {
- fprintf(stderr, "msgget failed with error: %d\n", errno);
- exit(EXIT_FAILURE);
- }
- //向消息队列中写消息,直到写入end
- while(running)
- {
- //输入数据
- printf("Enter some text: ");
- fgets(buffer, BUFSIZ, stdin);
- data.msg_type = 1; //注意2
- strcpy(data.text, buffer);
- //向队列发送数据
- if(msgsnd(msgid, (void*)&data, MAX_TEXT, 0) == -1)
- {
- fprintf(stderr, "msgsnd failed\n");
- exit(EXIT_FAILURE);
- }
- //输入end结束输入
- if(strncmp(buffer, "end", 3) == 0)
- running = 0;
- sleep(1);
- }
- exit(EXIT_SUCCESS);
- }


Linux进程间通信——使用消息队列的更多相关文章
- [转]Linux进程间通信——使用消息队列
点击此处阅读原文 另收藏作者ljianhui的专栏初学Linux 下面来说说如何使用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linu ...
- 练习--LINUX进程间通信之消息队列MSG
https://www.ibm.com/developerworks/cn/linux/l-ipc/part3/ 继续坚持,或许不能深刻理解,但至少要保证有印象. ~~~~~~~~~~~~~~ 消息队 ...
- Linux进程间通信(消息队列/信号量+共享内存)
写在前面 不得不说,Deadline果真是第一生产力.不过做出来的东西真的是不堪入目,于是又花了一早上重写代码. 实验内容 进程通信的邮箱方式由操作系统提供形如 send()和 receive()的系 ...
- Linux进程间通信之消息队列
本文依据以下思路展开,首先从宏观上阐述消息队列的机制,然后以具体代码为例进一步阐述该机制,最后试着畅想一下该通信机制潜在的应用. 消息队列是在两个不相关进程间传递数据的一种简单.高效方式,她独立于发送 ...
- Linux进程间通信(二) - 消息队列
消息队列 消息队列是Linux IPC中很常用的一种通信方式,它通常用来在不同进程间发送特定格式的消息数据. 消息队列和之前讨论过的管道和FIFO有很大的区别,主要有以下两点(管道请查阅我的另一篇文章 ...
- linux 进程间通信之 消息队列
消息队列就是一个消息的链表. 能够把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程能够向中依照一定的规则加入新消息.有读权限的进程则能够读走消息. 读走就没有了.消息队列是 ...
- linux下的进程间通信之消息队列
概念: 进程彼此之间可以通过IPC消息进行通信.进程产生的每条消息都被发送到一个IPC消息队列中,这条消息一直存放在队列中,直到另一个进程将其读走为止. 优点:可以通过发送消息来几乎完全避免命名管道的 ...
- 进程间通信--POSIX消息队列
相关函数: mqd_t mq_open(const char *name, int oflag); mqd_t mq_send(mqd_t mqdes, const char *msg_ptr, si ...
- linux 进程间消息队列通讯
转自:http://blog.csdn.net/lifan5/article/details/7588529 http://www.cnblogs.com/kunhu/p/3608589.html 前 ...
随机推荐
- 使用AES加密的帮助类
在开发中经常使用加密/解密对一些内容进行处理,比如密码在存入数据库之前先经过加密处理等等,这里就把一个加密帮助类代码贴出来,供以后查找使用. 这个帮助类主要功能是对字符串和字节数组进行加密解密处理. ...
- Inno Setup GIF 显示插件 GIFCtrl (V2.1 版本)
原文 http://restools.hanzify.org/article.asp?id=79 引用来自 test.iss ; -- test.iss --; restools; http://r ...
- tftp使用方法
参数说明:-l 是local的缩写,后跟存在于Client的源文件名,或下载Client后 重命名的文件名. -r 是remote的缩写,后跟Se ...
- HTTP based RESTful APIs - asp.net web api
1.HTTP http://www.w3.org/Protocols/rfc2616/rfc2616.html 2.REST是什么http://www.ics.uci.edu/~fielding/pu ...
- flex——dictionary跟Object的区别与遍历
AS3中Object和 Dictionary都可以用来保存key-value形式的数据,Dictionary类和Object唯一的区别在于:Dictionary对象可以使用非字符串作为键值对的键.例如 ...
- 微信小程序 app.json 配置
我们使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 以下是一个包含了所有配置选项的简单配置app.json : { " ...
- VB读写Excel
近期用excel和VB比較多,就简单的学习了一下VB中对Excel的处理.今天就介绍一些吧. 在VB中要想调用Excel,须要打开VB编程环境“project”菜单中的“引用”项目 ...
- 使用javascript oop开发滑动(slide) 菜单控件
这里使用原生的javascript,用面向对象的方式创建一个容易维护使用方便的滑动菜单 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...
- 绿色mysql启动脚本
启动脚本如下:./mysqld_safe --defaults-file=/export/servers/mysql-5.5.38/my.cnf --ledir=/export/servers/mys ...
- LevelDB.NET性能测试
最近了解了一下LevelDB,发觉这个嵌入式的K-V数据性能不错,所以顺便想在使用层面了解一下.由于LevelDB也有针对.net的实现,所以就针对了LevelDB.NET进行了一个简单的读写压力测试 ...