Linux 開發 程序間通訊 訊息佇列學習筆記

2021-10-12 14:00:14 字數 2280 閱讀 4785

訊息佇列讀完結點就被刪除

步驟總結 msgget 在核心中建立訊息佇列 鏈式佇列

mmgctl 刪除 訊息佇列 修改核心空間屬性

mmgsnd 使用者空間資料寫入訊息佇列

mmgrcv 使用者空間可以讀取到核心空間的訊息佇列

阻塞方式 非阻塞方式

a b 無親緣關係的兩個程序 通過訊息佇列進行通訊 兩個訊息佇列 全雙工通訊

管道通訊 共享記憶體通訊的區別和缺點

訊息佇列雙向通訊**如下

伺服器 :

#include

"sys/types.h"

#include

#include

#include

#include

#include

#include

#include

#include

struct msgbuf

;int

main()

msgid=

msgget

(key,ipc_creat |

0777);

if(msgid<0)

printf

("create message success msgid:%d\n"

,msgid)

;system

("ipcs -q");

buf.type=

100;

pid=

fork()

;if(pid <0)

printf

("fork failure\n");

if(pid >0)

}if(pid ==0)

}msgctl

(msgid,ipc_rmid,

null);

system

("ipcs -q");

}

客戶端

#include

"sys/types.h"

#include

#include

#include

#include

#include

#include

#include

#include

struct msgbuf

;int

main()

msgid=

msgget

(key,

0777);

if(msgid<0)

printf

("create message success msgid:%d\n"

,msgid)

; pid=

fork()

; buf.type=

200;

if(pid <0)

printf

("fork failure\n");

if(pid>0)

}if(pid ==0)

}msgctl

(msgid,ipc_rmid,

null);

system

("ipcs -q");

}

LINUX學習筆記15 程序間通訊4訊息佇列

a 本質 訊息鍊錶 b 分類 1.posix訊息佇列 可移植作業系統介面 2.系統v 隨核心持續的,只有在核心重啟和人工刪除,才會被刪除。每個訊息佇列對應唯一的鍵值 c 獲取鍵值 key t ftok char pathname,char proj 1.作用 返回檔名對應的鍵值,本質是個檔案 2.i...

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 訊...