程序間訊息佇列通訊

2021-06-06 15:20:29 字數 2005 閱讀 3088

要保證server能夠接收client的訊息,就必須保證server的生成的msg的識別符號是一樣的,也就是兩個用的key是必須一樣的。

msglucy.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define projid 0xff

#define lucy 1

#define peter 2

#define seed 'g'

int mqid;

void terminate_handler(int );

void terminate_handler(int signo)

int main()

msg;

int ret;

mqkey = ftok(".",seed);/*get the same key*/

if(mqkey==-1)

mqid = msgget(mqkey,ipc_creat|0666);/*if not creat or open it*/

if(mqid==-1)

signal(sigint,terminate_handler);

signal(sigterm,terminate_handler);

while(1)

//printf("ipc_nowait=%d\n",ipc_nowait);

msg.mtext[strlen(msg.mtext)-1] = '\0';

msg.mtype = lucy;

msgsnd(mqid,&msg,strlen(msg.mtext)+1,0);/*block method*/

msgrcv(mqid,&msg,sizeof(struct msgbuf),peter,0);/*can recieve the address*/

printf("peter:%s\n",msg.mtext);

printf("the ponit m:%p\n",msg.m);}}

msgpeter.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define projid 0xff

#define lucy 1

#define peter 2

#define seed 'g'

void terminate_handler(int );

int mqid;

void terminate_handler(int signo)

int main()

msg;

int ret;

mqkey = ftok(".",seed);

if(mqkey==-1)

if((mqid = msgget(mqkey,0))==-1)

//mqid = msgget(mqkey,0);

if(mqid==-1)

n=4;

msg.m=&n;

printf("the ponit m:%p\n",msg.m);

printf("msg.m's value is %d\n",*(msg.m));

signal(sigint,terminate_handler);

signal(sigterm,terminate_handler);

while(1)

msg.mtext[strlen(msg.mtext)-1] = '\0';

msg.mtype = peter;

msgsnd(mqid,&msg,sizeof(struct msgbuf),0);/*can send the address to the server*/}}

程序間通訊(訊息佇列)

在嵌入式linux應用開發中,linux程序通訊的方式有6種,分別是管道 pipe 及有名管道 named pipe 訊號 signal 訊息佇列 msg 共享記憶體 shm 訊號量 和套接字 socket 在這我就簡單的描述一下程序通訊中的資訊佇列 msg 首先,訊息佇列的實現有重要的幾步 1 建...

程序間通訊 訊息佇列

有三種稱作xsi ipc的ipc 訊息佇列 訊號量以及 共享記憶體。它們只見有很多的相似之處。訊息佇列是訊息的鏈結表,儲存在核心中,由訊息佇列識別符號表示。它不同於管道,其生命週期是隨核心的。訊息佇列提供了 一種從 乙個程序向另 乙個程序傳送 乙個資料塊的 方法。每個資料塊都被認為是有 乙個型別,接...

程序間通訊 訊息佇列

訊息佇列 訊息佇列提供了一種從乙個程序向另乙個程序傳送資料塊的方法。每個資料塊都被認為是有乙個型別,接收者程序接收的資料塊可以有不同的型別值。我們可以通過傳送訊息來避免命名管道的同步和阻塞問題。訊息佇列與管道不同的是,訊息佇列是基於訊息的,而管道是基於位元組流的,且訊息佇列的讀取不一定是先入先出。訊...