进程间通信之消息队列
来源:互联网 发布:我是皇弓箭进阶数据 编辑:程序博客网 时间:2024/06/06 00:54
消息队列由操作系统提供,一个进程向另一个进程发送有类型数据块。
生命周期:随内核。
消息队列的实现:
comm.h:#ifndef _COMM_H_#define _COMM_H_#include<stdio.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>#include<string.h>#define PATHNAME "."#define PROJ_ID 0x6666#define SERVER_TYPE 1#define CLIENT_TYPE 2#define SIZE 128struct msgbuf{ long mtype; char mtext[SIZE];};int createMsgQueue();int getMsgQueue();int sendMsg(int msgid,long type,const char* msg);int recvMsg(int msgid,long type,char out[]);int destroyMsgQueue(int);#endifcomm.c:#include "comm.h"int commMsgQueue(int flag){ key_t k=ftok(PATHNAME,PROJ_ID); if(k<0) { perror("ftok"); return -1; } //int msg_id = msgget(k,IPC_CREAT|IPC_EXCL|0666); int msg_id = msgget(k,flag); if(msg_id<0) { perror("msgget"); return -2; } return msg_id;}int createMsgQueue(){ return commMsgQueue(IPC_CREAT|IPC_EXCL|0666);}int getMsgQueue(){ return commMsgQueue(IPC_CREAT);}int sendMsg(int msgid,long type,const char* info){ struct msgbuf msg; msg.mtype=type; strcpy(msg.mtext,info); if(msgsnd(msgid,&msg,sizeof(msg.mtext),0)<0) { perror("msgsnd"); return -1; } return 0;}int recvMsg(int msgid,long type,char out[]){ struct msgbuf msg; if(msgrcv(msgid,&msg,sizeof(msg.mtext),type,0)<0) { perror("msgrcv"); return -1; } strcpy(out,msg.mtext); return 0;}int destroyMsgQueue(int msgid){ if(msgctl(msgid,IPC_RMID ,NULL)<0) { perror("msgctl"); return -3; } return 0;}server.c:#include "comm.h"int main(){ int msgid = createMsgQueue(); printf("msgid:%d\n",msgid); //sleep(5); //const char* msg="hello bit\n"; char buf[SIZE]; while(1) { // recv -->send recvMsg(msgid,CLIENT_TYPE,buf); printf("client# %s\n",buf); printf("please enter# "); fflush(stdout); ssize_t s=read(0,buf,sizeof(buf)-1); if(s>0) { buf[s-1]='\0'; sendMsg(msgid,SERVER_TYPE,buf); } //sleep(1); } destroyMsgQueue(msgid); return 0;}client.c:#include "comm.h"int main(){ int msgid = getMsgQueue(); printf("msgid:%d\n",msgid); char buf[SIZE]; while(1) { // send --> recv printf("please enter# "); fflush(stdout); ssize_t s=read(0,buf,sizeof(buf)-1); if(s>0) { buf[s-1]='\0'; sendMsg(msgid,CLIENT_TYPE,buf); } recvMsg(msgid,SERVER_TYPE,buf); printf("server# %s\n",buf); } return 0;}Makefile:client_=clientserver_=servercc=gcccliSrc = client.c comm.cserSrc = server.c comm.c.PHONY:allall:$(client_) $(server_)$(client_):$(cliSrc) $(cc) -o $@ $^$(server_):$(serSrc) $(cc) -o $@ $^.PHONY:cleanclean: rm -f $(client_) $(server_)
0 0
- 进程间通信之消息队列通信
- linux进程间通信之消息队列
- 进程间通信之消息队列篇
- linux进程间通信之消息队列
- 进程间通信之消息队列
- linux进程间通信之消息队列
- Linux进程间通信之消息队列
- 进程间通信之消息队列
- 进程间通信之消息队列
- 进程间的通信之消息队列
- 进程间通信之消息队列
- linux 进程间通信之消息队列
- 进程间通信之消息队列
- linux进程间通信之消息队列
- linux进程间通信之消息队列
- 进程间通信之消息队列
- 进程间通信之消息队列
- 进程间通信之消息队列
- Linux shell脚本传参,传入数组
- git如何生成ssh key
- 欢迎使用CSDN-markdown编辑器
- android模拟聊天界面recyclerview实现
- 操作系统——进程线程(一)
- 进程间通信之消息队列
- Log4j基本使用及其部分参数总结
- 操作系统(七)-----进程的一些基本概念
- 算法笔记_055-蓝桥杯练习 Tricky and Clever Password (Java)
- 操作系统-总结一
- AndroidStudio2.3出现一直Refresh问题解决方案
- Hibernate 关系映射
- R 语言文件读写
- 使用swift3创建自己的pod