进程间通信之消息队列

来源:互联网 发布:我是皇弓箭进阶数据 编辑:程序博客网 时间: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 128
struct 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);
#endif
comm.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_=client
server_=server
cc=gcc
cliSrc = client.c comm.c
serSrc = server.c comm.c
.PHONY:all
all:$(client_) $(server_)
$(client_):$(cliSrc)
$(cc) -o $@ $^
$(server_):$(serSrc)
$(cc) -o $@ $^
.PHONY:clean
clean:
rm -f $(client_) $(server_)

0 0
原创粉丝点击