0

0

Linux --进程间通信--消息队列

黄舟

黄舟

发布时间:2017-01-18 10:43:54

|

1945人浏览过

|

来源于php中文网

原创

一、消息队列的定义 
消息队列能够弥补管道的不足,实现双向交互数据,是一个进程向另一进程发送进程块的方法。与管道不同的是,管道是基于字节流的,消息队列是基于消息的,且消息队列的读取不一定是先进先出。

二、消息队列的创建
通过函数int messget(key_t key,int msgflg);创建

key:端口号,可以有 ftok生成。
msgflg:
ipc_crtat 若果 ipc不存在,则创建一个ipc资源,
ipc_excl:一般和 ipc_creat一起使用可以保证所得的对象是新建的,而不是打开已有的。
三、向消息队列读数据
msgrcv从队列中取的消息

ssize_t msgrcv(int msqid,void *msgp,size_t msgsz,long msgtyp,int msgflg);

msgsnd将消息放进队列中

int msgsnd(int msqid,const void *msgp,size_t msgsz,int msgflg);

四、设置消息队列属性
int msgctl(int msgqid,int cmd,struct msqid_ds *buf);

*** 可以用man 函数名 查看函数***

程序代码:

coom.h

1 #pragma once  
  2 #include  
  3 #include  
  4 #include  
  5 #include  
  6 #include  
  7 #include  
    
  8 #define _PATH_ "."  
  9 #define _PROCID_ 0x776  
 10 #define _SIZE 1024  
 11 #define SERVE_MSGTYPE 1  
 12 #define CLIECT_MSGTYPE 2  
   
 13 typedef struct message  
 14 {  
 15   long mtype;  
 16   char mtext[_SIZE];  
 17 }message;  
 18 int create_msg(int msg_num);  
 19 int drstroy_msg(int msg_id);  
 20 int set_msg();  
 21 int get_msg();  
 22 int msg_send(int msg_id,const char* msg,long type);  
 23 int msg_rec(int msg_id ,char buf[],long type);

comm .c

1 #include"comm.h"  
  2  int create_msg(int msg_num)  
  3 {  
  4   key_t _key=ftok(_PATH_,_PROCID_);  
  5   if(_key < 0)  
  6   {  
  7     perror("ftok");  
  8     return -1;  
  9   }  
 10   int ret= msgget(_key,msg_num);  
 11    if(ret < 0)  
 12    {  
 13     perror("msgget");  
 14     return -1;  
 15    }  
 16  return ret;  
 17 }  
 18 int drstroy_msg(int msg_id)  
 19 {  
 20   if(msgctl(msg_id,IPC_RMID,NULL) < 0)  
 21   {  
 22     perror("msgctl");  
 23     return -1;  
 24   }else{  
 25     printf("remove message_queue\n");  
 26     return 0;  
 27   }  
 28 }  
 29 int set_msg()  
 30 {  
 31   umask(0);  
 32   return create_msg(IPC_CREAT |IPC_EXCL |0666);  
 33 }  
 34 int get_msg()  
 35 {  
 36     return create_msg(IPC_CREAT);  
 37 }  
 38 int msg_send(int msg_id,const char* msg,long type)  
 39 {  
 40   struct message _msg;  
 41   _msg.mtype=type;  
 42   strcpy(_msg.mtext,msg);  
 43   if(msgsnd(msg_id,&_msg,strlen(_msg.mtext),0)< 0)  
 44   {  
 45     perror("msg_send error");  
 46     return -1;  
 47   }  
 48   return 0;  
 49 }  
 50 int msg_rec(int msg_id,char buf[],long type)  
 51 {  
 52   struct message _msg;  
 53   memset(_msg.mtext,'\0',_SIZE);  
 54   if(msgrcv(msg_id,&_msg,_SIZE,type,0)< 0)  
 55   {  
 56     perror("msg_receve error");  
 57     return -1;  
 58   }  
 59   strcpy(buf,_msg.mtext);  
 60   return 0;  
 61 }  
 62

client.c

1 #include"comm.h"  
  2 int main()  
  3 {  
  4  int msg_id=get_msg();  
  5  if(msg_id<0)  
  6  {  
  7   perror("error");  
  8   exit(1);  
  9  }  
 10   char buf[_SIZE];  
 11  while(1)  
 12  {  
 13   fflush(stdout);  
 14   printf("please client input: ");  
 15   memset(buf,'\0',_SIZE);  
 16   fgets (buf,sizeof(buf),stdin);  
 17   if(msg_send(msg_id,buf,CLIECT_MSGTYPE)< 0)  
 18     {  
 19       perror("send fail");  
 20       exit(1);  
 21     }  
 22   
 23   if(msg_rec(msg_id,buf,SERVE_MSGTYPE)<0)  
 24     {  
 25       perror("recve fail");  
 26       exit(1);  
 27     }  
 28   printf("serve:%s" ,buf);  
 29  }  
 30   return 0;  
 31 }

server.c

1 #include "comm.h"  
  2 int main()  
  3 {  
  4   int msg_id=set_msg();  
  5   if(msg_id<0)  
  6   {  
  7     perror("mig_id");  
  8     exit(1);  
  9   }  
 10   printf("%d\n",msg_id);  
 11   char buf[_SIZE];  
 12   while(1)  
 13   {  
 14      if(msg_rec(msg_id,buf,CLIECT_MSGTYPE)< 0)  
 15     {  
 16       perror("recve fail");  
 17       exit(1);  
 18     }  
 19     else  
 20     {  
 21       printf("client :%s",buf);  
 22     }  
 23     printf("server please input: ");  
 24     fflush(stdout);  
 25     memset(buf,'\0',_SIZE);  
 26     fgets(buf,_SIZE,stdin);  
 27     if(msg_send(msg_id,buf,SERVE_MSGTYPE)<0)  
 28     {  
 29       perror("send fail");  
 30       exit(1);  
 31     }  
 32   }  
 33   drstroy_msg(msg_id);  
 34   return 0;  
 35 }

运行结果:

Android AsyncChannel源码分析 WORD版
Android AsyncChannel源码分析 WORD版

本文档主要讲述的是Android AsyncChannel源码分析;AsyncChannel类用于处理两个Handler之间的异步消息传递,消息传递的Handler可以出于同一进程,也可以处于不同进程,不同进程之间的Handler消息传递使用Android的Binder通信机制来实现。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看

下载

327.jpg

以上就是Linux --进程间通信--消息队列的内容,更多相关内容请关注PHP中文网(www.php.cn)!

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
java连接字符串方法汇总
java连接字符串方法汇总

本专题整合了java连接字符串教程合集,阅读专题下面的文章了解更多详细操作。

7

2026.02.05

java中fail含义
java中fail含义

本专题整合了java中fail的含义、作用相关内容,阅读专题下面的文章了解更多详细内容。

8

2026.02.05

控制反转和依赖注入区别
控制反转和依赖注入区别

本专题整合了控制反转和依赖注入区别、解释、实现方法相关内容。阅读专题下面的文章了解更多详细教程。

11

2026.02.05

钉钉脑图插图教程合集
钉钉脑图插图教程合集

本专题整合了钉钉脑图怎么插入图片、钉钉脑图怎么用相关教程,阅读专题下面的文章了解更多详细内容。

24

2026.02.05

python截取字符串方法汇总
python截取字符串方法汇总

本专题整合了python截取字符串方法相关合集,阅读专题下面的文章了解更多详细内容。

2

2026.02.05

Java截取字符串方法合集
Java截取字符串方法合集

本专题整合了Java截取字符串方法汇总,阅读专题下面的文章了解更多详细操作教程。

1

2026.02.05

java 抽象方法
java 抽象方法

本专题整合了java抽象方法定义、作用教程等内容,阅读专题下面的文章了解更多详细内容。

2

2026.02.05

Eclipse创建jsp文件教程合集
Eclipse创建jsp文件教程合集

本专题整合了Eclipse创建jsp文件、创建jsp项目等等内容,阅读专题下面的文章了解更多详细教程。

26

2026.02.05

java 字符串转数字
java 字符串转数字

本专题整合了java如何字符串转数字相关内容,阅读专题下面的文章了解更多详细教程。

4

2026.02.05

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 8.5万人学习

Git 教程
Git 教程

共21课时 | 3.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号