[转]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进程间通信——使用消息队列
下面来说说如何用不用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linux进程间通信——使用命名管道 一.什么是消息队列 消息队列提 ...
- 练习--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 前 ...
随机推荐
- Ruby-随机数
--随机0-1 rand --随机一个范围 rand(1..100)
- 205 Reset Content
https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html The server has fulfilled the request and the ...
- 【Git学习笔记】撤销修改
工作区下的.git文件夹其实是Git的版本库,Git的版本库里存了很多东西,其中最重要的就是称为 stage 的暂存区,还有Git为我们自动创建的第一个分支 master ,以及指向master的一个 ...
- web应用程序开发原理
企业应用计算的演变为1.主机/哑终端的集中计算模式: 2.客户机/服务器计算模式:3.浏览器 /服务器计算模式.其中,1具有部署方面的优势,但在一台计算机中进行全部的处理,应用程序难于维护,难于 ...
- 用css实现网站切角效果 使用css3属性:渐变
都是大量的练习,老师练习乒乓球花了大量时间,十万次一个动作的重复,高中班主任说过,世上没有天才,只是重复的次数多了,自然被认作了天才,小小班的学生之所以厉害是因为他们重复一个知识点次数多,所以没有一 ...
- GIT远程仓库地址变更
将VS2013的解决方案添加到GIT源代码管理后会增加.gitattributes和.gitignore 2个文件以及.git目录 设置远程地址的文件在.git目录下的config文件中 直接修改上图 ...
- Hyper-V 2012 R2 故障转移群集之建立域控(AD DS)与加入域
Windows 2012 R2建立域控(AD DS)与加入域 Active Directory概述: 使用 Active Directory(R) 域服务 (AD DS) 服务器角 ...
- 接入WebSocket记录
为什么用 WebSocket 因为APP里面有个聊天功能,需要服务器主动推数据到APP.HTTP 通信方式只能由客户端主动拉取,服务器不能主动推给客户端,如果有实时的消息,要立刻通知客户端就麻烦了,要 ...
- 如何理解泛型中的new()约束
一:为什么需要New约束 假设有这样一个需求,它需要在定义一个泛型类时同时实例化T对象.有网友说了:"这还不简单,我立刻给你写一个",刷刷刷,得到以下的例子. public cla ...
- Windows Server 2008 R2域控组策略设置禁用USB
问题: Windows Server 2008 R2域控服务器如何禁用客户端使用USB移动存储(客户端操作系统需要 Windows Vista以上的操作系统,XP以下的操作系统不能禁用USB移动存储) ...