- 推荐
- 实例1
- 代码
- msgque.c
- 结果
- 实例2
- 代码
- msgsnd.c
- msgrcv.c
- 结果
- 实例3
- 代码
- comm.h
- comm.c
- server.c
- client.c
- Makefile
- 结果
linux 进程间通信三 消息队列以及实例
【Linux】消息队列–实现进程间通信
进程间通信具体实现代码(两个程序间互发信息)
实例1:一个程序,自己发消息,然后自己再从队列上读消息
代码 msgque.c#include结果 实例2#include #include #include #include #include #include #define BUFSZ 512 struct message { long msg_type; char msg_text[BUFSZ]; }; int main() { int qid; key_t key; int len; struct message msg; if ((key = ftok(".", 'a')) == -1) { perror("ftok"); exit(1); } if ((qid = msgget(key,IPC_CREAT|0666)) == -1) { perror("msgget"); exit(1); } printf("Opened queue %dn",qid); puts("Please enter the message to queue:"); if ((fgets((&msg)->msg_text, BUFSZ, stdin)) == NULL) //消息内容 { puts("no message"); exit(1); } msg.msg_type = getpid(); //消息类型,可以理解为发信人名字 len = strlen(msg.msg_text); if ((msgsnd(qid, &msg, len, 0)) < 0) //把消息加到队列 { perror("message posted"); exit(1); } if (msgrcv(qid, &msg, BUFSZ, getpid(), 0) < 0) //读发信人为getpid()的消息 { perror("msgrcv"); exit(1); } printf("message is:%sn",(&msg)->msg_text); if ((msgctl(qid, IPC_RMID, NULL)) < 0) { perror("msgctl"); exit(1); } exit(0); }
实例2:一个程序发送消息 ,另一个接收消息,读的是第一条消息不判断是不是自己想要的消息
代码 msgsnd.c#includemsgrcv.c#include #include #include #include #include #include #define BUFFER_SIZE 512 struct message { long msg_type; char msg_text[BUFFER_SIZE]; }; int main() { int qid; key_t key; struct message msg; if ((key = ftok(".", 'a')) == -1) { perror("ftok"); exit(1); } if ((qid = msgget(key, IPC_CREAT|0666)) == -1) { perror("msgget"); exit(1); } printf("Open queue %dn",qid); while(1) { printf("Enter some message to the queue(enter 'quit' to exit):"); if ((fgets(msg.msg_text, BUFFER_SIZE, stdin)) == NULL) { puts("no message"); exit(1); } msg.msg_type = getpid(); if ((msgsnd(qid, &msg, strlen(msg.msg_text), 0)) < 0) { perror("message posted"); exit(1); } if (strncmp(msg.msg_text, "quit", 4) == 0) { break; } } exit(0); }
#include结果 实例3#include #include #include #include #include #include #define BUFFER_SIZE 512 struct message { long msg_type; char msg_text[BUFFER_SIZE]; }; int main() { int qid; key_t key; struct message msg; if ((key = ftok(".", 'a')) == -1) { perror("ftok"); exit(1); } if ((qid = msgget(key, IPC_CREAT|0666)) == -1) { perror("msgget"); exit(1); } printf("Open queue %dn", qid); do { memset(msg.msg_text, 0, BUFFER_SIZE); if (msgrcv(qid, (void*)&msg, BUFFER_SIZE, 0, 0) < 0) //读取消息不管是谁发的 { perror("msgrcv"); exit(1); } printf("The message from process %d : %s", msg.msg_type, msg.msg_text); } while(strncmp(msg.msg_text, "quit", 4)); if ((msgctl(qid, IPC_RMID, NULL)) < 0) { perror("msgctl"); exit(1); } exit(0); }
进程间通信具体实现代码(两个程序间互发信息)
设计两个程序,要求用消息队列实现聊天程序。
输入bye,结束聊天
#ifndef _COMM_H_ #define _COMM_H_ #includecomm.c#include #include #include #include #define SERVER 1 #define CLIENT 2 #define PATHNAME "." #define PROJ_ID 0x6666 struct msgbuf{ long mtype; char mtext[1024]; }; int creat();//创建消息队列 int send(int msgid,int who,char *msg); //发送消息 int recv(int msgid, int type, char out[]);//接收消息 int destory(int msgid);//销毁消息队列 int get();//获得消息队列的标识符 #endif
#include "comm.h"
static int com(int flags){
key_t key=ftok(PATHNAME,PROJ_ID);
if(key<0){
perror("ftok");
return -1;
}
int msgid=msgget(key,flags);
if (msgid<0){
perror("msgget");
return -2;
}
return msgid;
}
int creat(){
return com(IPC_CREAT|IPC_EXCL|0666);
}
int destory(int msgid){
if(msgctl(msgid,IPC_RMID,NULL)<0){
perror("msgctl");
return -1;
}
}
int get(){
return com(IPC_CREAT);
}
int send(int msgid,int who,char*msg){
struct msgbuf buf;
buf.mtype=who;
strcpy(buf.mtext,msg);
if(msgsnd(msgid,(void*)&buf,sizeof(buf.mtext),0)<0) {
perror("msgsnd");
return -1;
}
}
int recv(int msgid,int type,char out[]){
struct msgbuf buf;
if(msgrcv(msgid,(void*)&buf,sizeof(buf.mtext),type,0)<0){
perror("msgrcv");
return -1;
}
strcpy(out,buf.mtext);
return 0;
}
server.c
#include"comm.h"
int main(){
int msgid=creat();
char buf[1024];
while(1){
buf[0]=0;
recv(msgid,CLIENT,buf);
printf("client say #%sn",buf);
if(strcmp(buf,"byen")==0){
printf("通信结束n");
break;
}
printf("please enter#");
fflush(stdout);
ssize_t s=read(0,buf,sizeof(buf)-1);
if(s>0){
buf[s]=0;
send (msgid,SERVER,buf);
}
}
destory(msgid);
return 0;
}
client.c
#include"comm.h"
int main(){
int msgid=get();
char buf[1024];
while(1){
buf[0]=0;
printf("please enter#");
fflush(stdout);
ssize_t s=read(0,buf,sizeof(buf)-1);
if(s>0){
buf[s]=0;
send (msgid,CLIENT,buf);
}
recv(msgid,SERVER,buf);
printf("server say #%sn",buf);
if(strcmp(buf,"byen")==0){
printf("通信结束n");
break;
}
}
destory(msgid);
return 0;
}
Makefile
.PHONY:all all:server client server:server.c comm.c gcc -o $@ $^ client:client.c comm.c gcc -o $@ $^ .PHONY:clean clean: rm -f server client结果



