linux IPC 消息队列(二)
我在网上想找多进程之间的通信方式,发现有人写的消息队列很好,搬过来:
common.h
#ifndef __COMMON_H_
#define __COMMON_H_ #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <string.h>
#include <time.h> #define MSG_SIZE 1024
#define FILEPATH "."
#define ID 0
#define SERVER_TYPE 1
#define CLIENT_TYPE 2 typedef struct msg_info {
long mtype;
char mtext[MSG_SIZE];
}msginfo; int CreateMessageQueue();
int GetMessageQueue();
int DeleteMessageQueue(int msgid);
int SendDataToMessageQueue(int msg_id, int send_type, char *msg);
int ReceiveDataFromMessageQueue(int msg_id, int receive_type, char *out); #endif
common.c
#include "common.h" static int CommonMessageQueue(int flags)
{
key_t _key = ftok(FILEPATH, ID);
if(_key == -) {
perror("ftok error");
return ;
}
int _msg_id = msgget(_key, flags);
if(_msg_id < ) {
perror("msgget error");
return ;
}
return _msg_id;
} int CreateMessageQueue()
{
return CommonMessageQueue(IPC_CREAT|IPC_EXCL|);
} int GetMessageQueue()
{
return CommonMessageQueue(IPC_CREAT);
} int DeleteMessageQueue(int msg_id)
{
if(msgctl(msg_id, IPC_RMID, NULL) < )
return -;
return ;
} int SendDataToMessageQueue(int msg_id, int send_type, char *msg)
{
msginfo buff;
buff.mtype = send_type;
strcpy(buff.mtext, msg);
int msg_snd = msgsnd(msg_id, (void *)&buff, sizeof(buff), );
if(msg_snd < ) {
perror("msgsnd error");
return -;
}
return ;
} int ReceiveDataFromMessageQueue(int msg_id, int receive_type, char *out)
{
msginfo buff;
int msg_rcv = msgrcv(msg_id, (void *)&buff, sizeof(buff), receive_type, );
if(msg_rcv < ) {
perror("msg_rcv error");
return -;
}
strcpy(out, buff.mtext);
return ;
}
server.c
#include "common.h" int main()
{
char buff[MSG_SIZE];
int msg_id = CreateMessageQueue(); while()
{
//send data
printf("server please enter# ");
fflush(stdout);
ssize_t s = read(, buff, sizeof(buff)-);
if(s > ) {
buff[s-] = ;
SendDataToMessageQueue(msg_id, SERVER_TYPE, buff);
printf("data has sended,wait receive......\n");
} else {
perror("read error");
return ;
} //receive data
ReceiveDataFromMessageQueue(msg_id, CLIENT_TYPE, buff);
printf("from client: %s\n", buff);
}
DeleteMessageQueue(msg_id); return ;
}
client:
#include "common.h" int main()
{
char buff[MSG_SIZE];
int msg_id = GetMessageQueue();
while() {
//receive data
ReceiveDataFromMessageQueue(msg_id, SERVER_TYPE, buff);
printf("from server:%s\n", buff); //send data
printf("client please enter# ");
fflush(stdout);
ssize_t s = read(, buff, sizeof(buff)-);
if(s <= ) {
perror("read error");
return ;
} else {
buff[s-] = ;
SendDataToMessageQueue(msg_id, CLIENT_TYPE, buff);
printf("data has sended,wait receive......\n");
}
}
return ;
}
Makefile:
all:client server client: common.c client.c
gcc -o $@ $^
server: common.c server.c
gcc -o $@ $^ .PHONY:clean
clean:
rm -rf client server
linux IPC 消息队列(二)的更多相关文章
- linux IPC 消息队列
		
消息队列函数原型 在建立IPC通讯时(如消息队列,共享内存)必须建立一个ID值.通常情况下,这个ID值由ftok函数得到 #inlcude <sys/types.h> #include & ...
 - linux网络编程之system v消息队列(二)
		
今天继续学习system v消息队列,主要是学习两个函数的使用,开始进入正题: 下面则开始用代码来使用一下该发送函数: 在运行之前,先查看一下1234消息队列是否已经创建: 用上次编写的查看消息队列状 ...
 - IPC——消息队列
		
Linux进程间通信——使用消息队列 下面来说说如何用不用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linux进程间通信——使用命名管 ...
 - 详解linux进程间通信-消息队列
		
前言:前面讨论了信号.管道的进程间通信方式,接下来将讨论消息队列. 一.系统V IPC 三种系统V IPC:消息队列.信号量以及共享内存(共享存储器)之间有很多相似之处. 每个内核中的 I P C结构 ...
 - linux进程间通信-消息队列
		
一 消息队列的介绍 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法. 每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构. 我们可以通过发送消息来避免命名管道的 ...
 - Linux进程间通信—消息队列
		
四.消息队列(Message Queue) 消息队列就是消息的一个链表,它允许一个或者多个进程向它写消息,一个或多个进程向它读消息.Linux维护了一个消息队列向量表:msgque,来表示系统中所有的 ...
 - Linux进程间通信-消息队列(mqueue)
		
前面两篇文章分解介绍了匿名管道和命名管道方式的进程间通信,本文将介绍Linux消息队列(posix)的通信机制和特点. 1.消息队列 消息队列的实现分为两种,一种为System V的消息队列,一种是P ...
 - RabbitMQ 消息队列 二
		
一:查看MQ的用户角色 rabbitmqctl list_users 二:添加新的角色,并授予权限 rabbitmqctl add_user xiaoyao 123456 rabbitmqctl se ...
 - linux 下消息队列发送后没有信息
		
在使用消息队列时,调用 #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...
 
随机推荐
- objc_setAssociatedObject 关联对象
			
使用场景:在分类中,不允许创建实例变量,这里就解决了此问题 参考: https://www.cnblogs.com/someonelikeyou/p/7162613.html 属性的实质:就是实例变量 ...
 - SCJP读书之知识点:
			
1:实例变量和局部变量 实例变量:是在类中进行声明的,可以有public,private等修饰符进行修饰. 局部变量:在方法中进行声明,生命周期就是方法开始,到方法结束.但是可以进行对象的引用来调用. ...
 - SQL SERVER内部函数大全
			
SQL SERVER内部函数是SQL数据库中非常重要的一类函数,下面就为您介绍SQL SERVER内部函数,如果您对此方面感兴趣的话,不妨一看. SQL SERVER内部函数: select @@CO ...
 - (转)运行jar应用程序引用其他jar包的四种方法 -- ClassLoader应用
			
转:http://longdick.iteye.com/blog/332580 大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个j ...
 - Cocos2d-x之Vector<T>
			
| 版权声明:本文为博主原创文章,未经博主允许不得转载. Vector<T>是Cocos2d-x 3.x中推出的列表容器,在cocos2d-x3.0之前的版本是Array,因此它所能容 ...
 - CSDN如何转载别人的博客
			
前言 对于喜欢逛CSDN的人来说,看别人的博客确实能够对自己有不小的提高,有时候看到特别好的博客想转载下载,但是不能一个字一个字的敲了,这时候我们就想快速转载别人的博客,把别人的博客移到自己的空间 ...
 - jmeter beanshell 写入文件
			
1.首先F:\test.txt文件为空
 - 分布式系统架构常识:CAP理论。
			
什么是CAP理论? 2000年7月,加州大学伯克利分校的Eric Brewer教授在ACM PODC会议上提出CAP猜想.2年后麻省理工学院的Seth Gilbert和NancyLynch从理论上证明 ...
 - [已解决]报错: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Users/mac/Ana
			
报错代码: pip3 install gerapy 报错内容: Could not install packages due to an EnvironmentError: [Errno 13] Pe ...
 - python基本数据类型的问题
			
关于python中的索引和切片: 在之前看的视屏中是这么描述的:索引值以 0 为开始值,-1 为从末尾的开始位置. 然后今天忽然有了醍醐灌顶的感觉,索引值以 0 为开始值: 就是说从左向右以0开始递增 ...