[转]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 前 ...
随机推荐
- Android开发使用TotalControl调试遇到的问题(备注)
背景:Android开发,使用TotalControl控制手机进行调试. 过程:Eclipse调试一直正常,某天下午突然提示:无法启动ADB.exe,请确认是否在对应的路径下. 怀疑是自己程序写的有问 ...
- window系统下,简单的FTP上传和下载操作
先假设有一FTP服务器,FTP服务器:qint.ithot.net,用户名:username 密码:user1234.在本地电脑D:盘创建一个文件夹"qint".将要上传的文件 ...
- 【转】监听按钮除OnClick外其他事件的方法,附简易改编的UIButton类
http://lib.csdn.net/article/unity3d/38463 作者:IceFantasyLcj 大家好,我是雨中祈雨.一直以来,CSDN都是我最好的编程助手.这是我在CSDN的第 ...
- vert.x学习(三),Web开发之Thymeleaf模板的使用
在vert.x中使用Thymeleaf模板,需要引入vertx-web-templ-thymeleaf依赖.pom.xml文件如下 <?xml version="1.0" e ...
- flex 弹性布局
采用 flex 布局的元素称为容器,其所有子元素称为项目.常用: 容器上可以设置六个属性:flex-direction.flex-wrap.flex-flow.justify-content ...
- linux权限系统
Linux权限分为 r(4):可读 , w(2)可写 , x(1)可执行 , -无权限 , 可以通过ls -l 文件名查看权限 , 如 ls -l 文件名 输出: -rwxrw---x. root r ...
- AJAX的封装(包括跨域问题)
注意: 1.同域下支持get和post方法 2.跨域问题必须得到后台的支持 3.跨域只支持get方法 function AJAX(obj){ //做网络请求的时候,参数以"对象"的 ...
- 在Centos6下面安装Python3.4
yum源里头好像没有python3.4 在Python的官网下载Python3.4的源代码 然后: 安装依赖包: yum groupinstall "Development tools&qu ...
- 使用JCIFS获取远程共享文件
package com.jadyer.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOExc ...
- 奥威软件Speed-BI荣获2016年度中国大数据最佳云平台奖
(原文转自:http://www.powerbi.com.cn/page110?article_id=210) 2016年12月16日,“科技原力觉醒,引领创新巅峰”—2016创新影响力年会暨国家产业 ...