我在网上想找多进程之间的通信方式,发现有人写的消息队列很好,搬过来:

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 消息队列(二)的更多相关文章

  1. linux IPC 消息队列

    消息队列函数原型 在建立IPC通讯时(如消息队列,共享内存)必须建立一个ID值.通常情况下,这个ID值由ftok函数得到 #inlcude <sys/types.h> #include & ...

  2. linux网络编程之system v消息队列(二)

    今天继续学习system v消息队列,主要是学习两个函数的使用,开始进入正题: 下面则开始用代码来使用一下该发送函数: 在运行之前,先查看一下1234消息队列是否已经创建: 用上次编写的查看消息队列状 ...

  3. IPC——消息队列

    Linux进程间通信——使用消息队列 下面来说说如何用不用消息队列来进行进程间的通信,消息队列与命名管道有很多相似之处.有关命名管道的更多内容可以参阅我的另一篇文章:Linux进程间通信——使用命名管 ...

  4. 详解linux进程间通信-消息队列

    前言:前面讨论了信号.管道的进程间通信方式,接下来将讨论消息队列. 一.系统V IPC 三种系统V IPC:消息队列.信号量以及共享内存(共享存储器)之间有很多相似之处. 每个内核中的 I P C结构 ...

  5. linux进程间通信-消息队列

    一 消息队列的介绍 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法. 每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构. 我们可以通过发送消息来避免命名管道的 ...

  6. Linux进程间通信—消息队列

    四.消息队列(Message Queue) 消息队列就是消息的一个链表,它允许一个或者多个进程向它写消息,一个或多个进程向它读消息.Linux维护了一个消息队列向量表:msgque,来表示系统中所有的 ...

  7. Linux进程间通信-消息队列(mqueue)

    前面两篇文章分解介绍了匿名管道和命名管道方式的进程间通信,本文将介绍Linux消息队列(posix)的通信机制和特点. 1.消息队列 消息队列的实现分为两种,一种为System V的消息队列,一种是P ...

  8. RabbitMQ 消息队列 二

    一:查看MQ的用户角色 rabbitmqctl list_users 二:添加新的角色,并授予权限 rabbitmqctl add_user xiaoyao 123456 rabbitmqctl se ...

  9. linux 下消息队列发送后没有信息

    在使用消息队列时,调用 #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...

随机推荐

  1. 【RabbitMQ】Concurrency、Prefetch、exclusive

    分布式消息中间件 RabbitMQ是用Erlang语言编写的分布式消息中间件,常常用在大型网站中作为消息队列来使用,主要目的是各个子系统之间的解耦和异步处理.消息中间件的基本模型是典型的生产者-消费者 ...

  2. paper 167:GPU的使用Theano之tutorial

    Theano之使用GPU 英文版本:http://deeplearning.net/software/theano/tutorial/using_gpu.html          using the ...

  3. [CSP-S模拟测试]:阴阳(容斥+计数+递推)

    题目传送门(内部题16) 输入格式 第一行两个整数$n$和$m$,代表网格的大小.接下来$n$行每行一个长度为$m$的字符串,每个字符若为$W$代表这个格子必须为阳,若为$B$代表必须为阴,若为$?$ ...

  4. sts bug SpringJUnit4ClassRunner

    SpringJUnit4ClassRunner找不到,不会自动修复, 只能复制引用过去 import org.springframework.test.context.junit4.SpringJUn ...

  5. 写在Flutter 1.0之前

    开始 大概有半年没有写东西了,感觉时间过得飞快,18年也过一小半,趁着谷歌大会再为Flutter这个耀眼的新星,再吹一波! 都beta3了,1.0还会远吗 Flutter团队依然不紧不慢,一步一个脚印 ...

  6. PHP缓存技术OB系统函数(总结)

    PHP缓存技术OB系统函数(总结) 一.总结 一句话总结: ob相比于php普通的文件操作多了缓存机制,所以适合做php的页面静态缓存 1.为什么php使用ob做静态文件缓存? 解决header()报 ...

  7. Chrome-逆向分析JS-2获取发送请求位置(以datatables获取表格数据为例)

    剧透:就是使用了一下 Chrome Source 的 XHR/fetch Breakpoints 功能,在发送请求时在该行进入断点调试. # 一:不认识一下 XHR/fetch Breakpoints ...

  8. js中的经典案例--简易万年历

    js中的经典案例--简易万年历 html代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  9. HTML段落,换行,字符实体

    HTML段落,换行,字符实体 html段落 <p>标签定义一个文本段落,一个段落含有默认的上下间距,段落之间会用这种默认间距隔开,代码如下: <!DOCTYPE html> & ...

  10. TypeError: write() argument must be str, not bytes报错

    TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: ...