文章链接:https://codemouse.online/archives/2020-06-06202119
common.h
#pragma once
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<errno.h>
#include<string.h>
#define _PATH_ "."
#define _PROJ_ID_ 0x6666
#define _SIZE_ 4096
#define CLIENT_TYPE 1
#define SERVER_TYPE 2
struct msgbuf
{
long mtype;
char mtext[_SIZE_];
};
int creat_queue();
int get_queue();
int send_msg(int msgqueue_id,int who,char *msg);
int recv_msg(int msgqueue_id,int want,char out[],int out_len);
int delete_msgqueue(int msgqueue_id);
common.c
#include"common.h"
static int com_creat_queue(int flags)
{
key_t key = ftok(_PATH_,_PROJ_ID_);
if(key < 0){
printf("%d:%sn",errno,strerror(errno));
return -1;
}
int msg_id = msgget(key,flags);
if(msg_id < 0){
printf("%d:%sn",errno,strerror(errno));
return -2;
}
return msg_id;
}
int creat_queue()
{
int flags = IPC_CREAT|0666;
return com_creat_queue(flags);
}
int get_queue()
{
int flags = IPC_CREAT;
return com_creat_queue(flags);
}
int send_msg(int msg_id,int who,char *msg)
{
struct msgbuf _buf;
memset(&_buf,'