IPC-メッセージキューの双方向通行
4396 ワード
メッセージキューは、1つのプロセスが別のプロセスにデータブロックを送信する方法を提供し、各データブロックには1つのタイプがあると考えられ、このタイプは以下で定数is_client_sndとis_server_sndで表す
メッセージキューは、パイプよりもブロックを回避する利点があります.
システム呼び出し関数: #include#include
プロトタイプ:key_t ftok(const char* pathname,int proj_id);
パラメータ:pathnameは、既存の情報を取得できるファイルのフルパスです(既存のファイルでなければなりません)
proj_idは任意の下位8ビットが0でない数(下位8ビットを取るため)
ftokアルゴリズム
戻り値:key値の正常な戻りに失敗しました-1
2.#include
#include
#include
プロトタイプ:int msgget(key_t key,int msgflg);
パラメータ:keyはftokから入手可能
flagは3つの値IPC_を取ることができますCREAT IPC_EXCL IPC_CREAT|IPC_EXCL
IPC_CREATはメッセージキューを作成し、キューのIDを返します.キューがすでに存在する場合は、そのIDを返します.
IPC_EXCL自体はあまり意味がなく、通常IPCとCREAT併用
IPC_CREAT|IPC_EXCLはメッセージキューを作成し、キューがすでに存在する場合は-1エラーを直接返し、リソースがオープンではなく新規であることを保証します.
戻り値:キューIDが正常に戻りました_msg_id失敗は-1を返す
3.#include
#include
#include
受信メッセージ:ssize_t msgrcv(int msgid, const void *msgp,size_t msgsz,int msgtyp,int msgflg);
送信メッセージ:int msgd(int msgid,const void*msgp,size_t msgsz,int msgflg);
パラメータ:msg_idはメッセージキューの識別であり、msgpはメッセージバッファへのポインタ(ユーザが独自に作成できる汎用構造)である
msgszはメッセージのサイズであり、msftypはキューからメッセージを取得するタイプであり、0は任意のメッセージを取得することができる.
msgflgは、メッセージキューにメッセージがない場合に行うべき指示を示し、0はブロック、IPC_NOWAITは直接-1に戻る.
使用するコマンド:
ipcs-q//システムに存在するメッセージキューの表示
ipcrm-q key値//指定したkey値を削除するメッセージキュー
メッセージキューは、パイプよりもブロックを回避する利点があります.
システム呼び出し関数:
プロトタイプ:key_t ftok(const char* pathname,int proj_id);
パラメータ:pathnameは、既存の情報を取得できるファイルのフルパスです(既存のファイルでなければなりません)
proj_idは任意の下位8ビットが0でない数(下位8ビットを取るため)
ftokアルゴリズム
戻り値:key値の正常な戻りに失敗しました-1
2.#include
#include
#include
プロトタイプ:int msgget(key_t key,int msgflg);
パラメータ:keyはftokから入手可能
flagは3つの値IPC_を取ることができますCREAT IPC_EXCL IPC_CREAT|IPC_EXCL
IPC_CREATはメッセージキューを作成し、キューのIDを返します.キューがすでに存在する場合は、そのIDを返します.
IPC_EXCL自体はあまり意味がなく、通常IPCとCREAT併用
IPC_CREAT|IPC_EXCLはメッセージキューを作成し、キューがすでに存在する場合は-1エラーを直接返し、リソースがオープンではなく新規であることを保証します.
戻り値:キューIDが正常に戻りました_msg_id失敗は-1を返す
3.#include
#include
#include
受信メッセージ:ssize_t msgrcv(int msgid, const void *msgp,size_t msgsz,int msgtyp,int msgflg);
送信メッセージ:int msgd(int msgid,const void*msgp,size_t msgsz,int msgflg);
パラメータ:msg_idはメッセージキューの識別であり、msgpはメッセージバッファへのポインタ(ユーザが独自に作成できる汎用構造)である
msgszはメッセージのサイズであり、msftypはキューからメッセージを取得するタイプであり、0は任意のメッセージを取得することができる.
msgflgは、メッセージキューにメッセージがない場合に行うべき指示を示し、0はブロック、IPC_NOWAITは直接-1に戻る.
使用するコマンド:
ipcs-q//システムに存在するメッセージキューの表示
ipcrm-q key値//指定したkey値を削除するメッセージキュー
//comm.h
#ifndef _MSG_QUEUE_
#define _MSG_QUEUE_
#include
#include
#include
#include
#include
#include
#define _PATH_ "/tmp/.msg"
#define _PROJ_ID_ 0x55
#define _MSG_SIZE_ 1024
extern const long is_server_snd;
extern const long is_client_snd;
typedef struct _msg_info _msg_info;
struct _msg_info
{
int mtype;
char mtext[_MSG_SIZE_];
};
static int comm_msg(int flag);
int creat_msg_queue();
int get_msg_queue();
int destroy(int msg_id);
#endif
//comm.c
#include"comm.h"
const long is_server_snd=1;
const long is_client_snd=2;
static int comm_msg(int flag)
{
key_t _key=ftok(_PATH_,_PROJ_ID_);
if(_key<0){
perror("ftok");
return -1;
}
int msg_id= msgget(_key,flag);
if(msg_id<0){
perror("msgget");
return -1;
}
else
return msg_id;
}
int creat_msg_queue()
{
return comm_msg(IPC_CREAT|IPC_EXCL);
}
int get_msg_queue()
{
return comm_msg(IPC_CREAT);
}
int destroy(int msg_id)
{
return msgctl(msg_id,IPC_RMID,NULL);
}
//server.c
#include"comm.h"
int main()
{
int _msg_id=creat_msg_queue();
_msg_info msginfo;
while(1){
msginfo.mtype=is_client_snd;
memset(msginfo.mtext,'\0',sizeof(msginfo.mtext));
if(msgrcv(_msg_id,&msginfo,sizeof(msginfo.mtext),is_client_snd,0)<0){
perror("msgrcv");
}
else{
printf("client say# %s",msginfo.mtext);
}
msginfo.mtype=is_server_snd;
memset(msginfo.mtext,'\0',sizeof(msginfo.mtext));
read(0,msginfo.mtext,sizeof(msginfo.mtext));
if(msgsnd(_msg_id,&msginfo,sizeof(msginfo),0)<0){
perror("msgsnd");
}
}
if(destory(_msg_id)!=0){
perror("destory failed!
");
}
else
printf("destory success!
");
return 0;
}
//client.c
#include"comm.h"
int main()
{
int _msg_id= get_msg_queue();
_msg_info msginfo;
while(1){
memset(msginfo.mtext,'\0',sizeof(msginfo.mtext));
msginfo.mtype=is_client_snd;
if(read(0,msginfo.mtext,sizeof(msginfo.mtext))>=0){
if(msgsnd(_msg_id,&msginfo,sizeof(msginfo.mtext),0)<0){
perror("msgsnd");
}
}
memset(msginfo.mtext,'\0',sizeof(msginfo.mtext));
if(msgrcv(_msg_id,&msginfo,sizeof(msginfo),is_server_snd,0)<0){
perror("msgrcv");
}
else{
printf("server say# %s",msginfo.mtext);
}
}
return 0;
}
//Makefile
.PHONY:all
all:server client
server:server.c comm.c
gcc -o server server.c comm.c
client:client.c comm.c
gcc -o client client.c comm.c
.PHONY:clean
clean:
rm -rf server client