进程间通信之消息队列

消息队列由操作系统提供,一个进程向另一个进程发送有类型数据块。

生命周期:随内核。

消息队列的实现:

 
 
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值