本地进程通信

一般的本地进程通信(即在自己的本机上实现两个不同的进程之间的数据传输)都是通过管道或者命名管道实现,可以通过消息队列或者共享内存的方法解决。
本次主要的内容是通过消息队列实现两个进程之间的数据传输,消息队列是消息传输过程中保存消息的容器,队列的主要目的是提供路由并保证消息的传递,如果发送消息的时候消息的接受者无法接受,队列会暂时存储消息,知道可以接受为止。
当然对于这个概念简单一点的理解可以是两个人在不同的地理位置使用固定电话,当然这个电话是单向接听的,只能一个人听一个人来说,中间的电话线就是消息队列。
接下来的C++实现过程如下(通过命名管道实现,因为命名管道可以创建唯一的IPC ID,从而可以与接受端建立连接):
发送端:
[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <sys/types.h>// 基本系统数据类型  
  3. #include <sys/ipc.h>//IPC(命名管道)  
  4. #include <sys/msg.h>//消息队列  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7. #include <unistd.h>//nix类系统定义符号常量的头文件,包含许多unix系统函数原型:read函数、write函数和getpid函数。  
  8. #include <string.h>  
  9. #define   BUFFER_SIZE   512  
  10. struct message  
  11. {  
  12.     long msg_type;  
  13.     char msg_text[BUFFER_SIZE];  
  14. };  
  15.   
  16. using namespace std;  
  17. int main(int argc, const char * argv[]) {  
  18.     // insert code here...  
  19. //    std::cout << "Hello, World!\n";  
  20.     int qid;  
  21.     key_t  key;//获取IPC通讯时的制定ID  
  22.     struct message  msg;  
  23.       
  24.     /*根据不同的路径和关键字产生标准的key    key_t ftok( const char * fname, int id ) 指定文件名以及id号 */  
  25.     if((key = ftok(".", 512)) == -1)  
  26.     {  
  27.         perror("ftok");  
  28.         exit(1);  
  29.     }  
  30.       
  31.     /*创建消息队列*/  
  32.     qid = msgget(key, IPC_CREAT|0666);//通过msgget 得到一个消息队列的ID  
  33.     if(qid  == -1)//创建失败  
  34.     {  
  35.         perror("msgget");  
  36.         exit(1);  
  37.     }  
  38.     printf("Open queue %d\n", qid);  
  39.     while(1)  
  40.     {  
  41.         printf("input some message to the queue:");  
  42.         if((fgets(msg.msg_text, BUFFER_SIZE, stdin)) == NULL)  
  43.         {  
  44.             puts("no message");  
  45.             exit(1);  
  46.         }  
  47.         msg.msg_type = getpid();  
  48.           
  49.         /*添加消息到消息队列中*/  
  50.         if((msgsnd(qid, &msg, strlen(msg.msg_text), 0)) <0)  
  51.         {  
  52.             perror("message posted");  
  53.             exit(1);  
  54.         }  
  55.         if(strncmp(msg.msg_text, "quit", 4) == 0)  
  56.         {  
  57.             break;  
  58.         }  
  59.     }  
  60.     exit(0);  
  61. }  
接受端:
[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <unistd.h>  
  3. #include <sys/types.h>  
  4. #include <sys/msg.h>  
  5. #include <sys/ipc.h>  
  6. #include <stdio.h>  
  7. #include <string.h>  
  8. #include <stdlib.h>  
  9.   
  10. using namespace std;  
  11. #define BUFFER_SIZE 512  
  12.   
  13. struct message  
  14. {  
  15.     long msg_tpye;  
  16.     char msg_text[BUFFER_SIZE];  
  17. };  
  18. int main(int argc, const char * argv[]) {  
  19.     // insert code here...  
  20. //    std::cout << "Hello, World!\n";  
  21.     int qid;  
  22.     key_t  key;  
  23.     struct message  msg;  
  24.       
  25.     /*根据不同的路径和关键字产生标准的key*/  
  26.     if((key = ftok(".", 512)) == -1)  
  27.     {  
  28.         perror("ftok");  
  29.         exit(1);  
  30.     }  
  31.     qid = msgget(key, IPC_CREAT|0666);  //获取消息队列的id  
  32.     if(qid < 0)  
  33.     {  
  34.         perror("msgget");  
  35.         exit(1);  
  36.     }  
  37.     printf("Open queue %d\n", qid);  
  38.       
  39.     do  
  40.     {  
  41.         memset(msg.msg_text, 0, BUFFER_SIZE);//初始化  
  42.         if((msgrcv(qid, (void*)&msg, BUFFER_SIZE, 0, 0)) <0)   //从进程的消息队列中读取信息  
  43.         {  
  44.             perror("msgrcv");  
  45.             exit(1);  
  46.         }  
  47.         printf("The message form process %ld: %s", msg.msg_tpye, msg.msg_text);  
  48.     }while(strncmp(msg.msg_text, "quit", 4));//来比较两个字符串的前n个字符,用msg.msg_text的第一个减去quit,对应字母相减,若差值为0,则继续向下减,否则返回差值  
  49.       
  50.     if((msgctl(qid, IPC_RMID, NULL)) <0)  
  51.     {  
  52.         perror("msgctl");  
  53.         exit(1);  
  54.     }  
  55.     exit(0);  
  56. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值