linux程序間通訊 訊息佇列

2021-08-02 21:20:08 字數 1437 閱讀 7111

訊息佇列屬於ipc

兩個程序間要通過訊息佇列進行通訊,比如a通過訊息佇列給b傳送乙個訊息。首先a要建立乙個訊息佇列,然後a往該訊息佇列裡面傳送訊息(由乙個有特殊形式的結構體構成,包括資料型別和資料內容),當不需要使用這個訊息佇列的時候刪除訊息佇列。b要做的事情是開啟訊息佇列,開啟方式是用和a裡面一樣的鍵值開啟對應的訊息佇列,然後接收訊息佇列的訊息(即結構體中某個型別的資料),結束。

相關函式

建立/

開啟訊息佇列  

msgget

傳送資料  

msgsnd

從訊息佇列取訊息  

msgrcv

刪除訊息佇列  

msgctl

例如a程序**

#include

#include

#include

#include

#include

#include

struct msgt

long msgtype;

char msgtext[1024];

void main()

int msqid;

int msg_type;

char str[256];

struct msgt msgs;

//建立訊息佇列

msqid = msgget(1025, ipc_creat);

//迴圈

while(1)

//刪除訊息佇列

msgctl(msqid, ipc_rmid, 0);

b程序**

#include

#include

#include

#include

#include

#include

int msqid;

struct msgt

long msgtype;

char msgtext[1024];

void myprocess()

struct msgt ms;

int x;

x = msgrcv(msqid, &ms, sizeof(struct msgt), 0, 0);

printf("the receive text: %s  

%d\n", ms.msgtext, x);

void main()

int i;

int cpid;

//開啟訊息佇列

msqid = msgget(1025, ipc_excl);

printf("the msqid is %d\n", msqid);

myprocess();

Linux程序間通訊 訊息佇列

linux和類linux系統下程序間通訊 inter process communication,ipc 有很多種方式,包括套接字 socket 共享記憶體 shared memory 管道 pipe 訊息佇列 message queue 等,各自有各自的一些應用場景和用途,這次就來聊一聊訊息佇列這...

linux程序間通訊 訊息佇列

訊息佇列由id 唯一標識 訊息佇列就是乙個訊息的列表,使用者可在佇列中新增,讀取訊息等 可按照型別來收發訊息 int msgget key t key,int flag int msgsnd int msqid,const void msgp,size t size,int flag msqid 訊...

Linux程序間通訊 訊息佇列

首先上篇文章我們說到了linux下進行程序間通訊的一種方法或機制匿名管道和命名管道,那麼這裡要說的是另外一種與之不同的通訊方法,即訊息佇列,兩者之間有相同也有不同的地方,具體的下面就一一介紹。一 什麼是訊息佇列?首先它也是一種進行程序間通訊的方式,通過乙個程序向另外乙個程序傳送資料塊的方式,每個資料...