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

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. centos6.8下安装nginx

    我用的阿里云上的镜像,make的时候总是出错,后来说是gcc安装不完整,要重新用下面命令安装 下: yum install gcc gcc-c++ gcc-g77 接下来下载nginx用到的一些库 w ...

  2. <三剑客> 老二:sed命令用法

    sed命令的用法: sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space ...

  3. python 收集测试日志--格式

    Python的logging模块提供了通用的日志系统,这个模块提供不同的日志级别,并可以采用不同的方式记录日志,比如文件,HTTP GET/POST,SMTP,Socket等,甚至可以自己实现方式记录 ...

  4. Ant Design Pro (中后台系统)教程

    一.概念:https://pro.ant.design/docs/getting-started-cn(官方网站) 1.Ant Design Pro 是什么:  https://www.cnblogs ...

  5. Linux配置Selenium+Chrome+Python

    ---恢复内容开始--- 最近一个月没有更新博客了.最近都在复习LINUX与PYTHON知识.决定以后在LINUX环境下学习新知识. 包括后期的框架学习与平台知识方面. 直接记录今天下午的学习成果. ...

  6. selenium2-java 浏览器的三种弹窗处理

    alert弹窗 confirm弹窗 prompt弹窗 点击确定         // 选取警告弹窗           Alert alert=driver.switchTo().alert();   ...

  7. Html5 学习笔记 【PC固定布局】 实战6 咨询页面

    最终效果: Html页面代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta char ...

  8. MSDN上关于WinDbg的手册

    参考:http://msdn.microsoft.com/en-us/library/windows/hardware/ff540507(v=vs.85).aspx 这是最靠谱的参考了,比.hh要直观 ...

  9. T1218:取石子游戏

    [题目描述] 有两堆石子,两个人轮流去取.每次取的时候,只能从较多的那堆石子里取,并且取的数目必须是较少的那堆石子数目的整数倍,最后谁能够把一堆石子取空谁就算赢. 比如初始的时候两堆石子的数目是25和 ...

  10. Engineer Assignment(暴力+状压dp)

    题意: n个工程,m个研究员,每个工程需要Ci个领域(X1,X2..Xci)的研究员 ,每个研究员会Di个不同的领域(X1,X2..Xdi),要完成一个工程必须使得分配给这个工程的研究员覆盖了这个工程 ...