Linux 訊息佇列相關函式使用

2021-10-07 07:31:21 字數 1828 閱讀 9399

訊息佇列(message queue):不在磁碟上,沒有檔名,有乙個關鍵字key可以開啟訊息佇列,訊息佇列中有很多通道,專門用來寫入/讀取資訊的。

#include #include #include int msgget(key_t key, int msg***);  //建立或者開啟訊息佇列

//引數一:自己指定的關鍵字key的編號

//引數二:標誌位,如果是建立訊息佇列則 o_creat | 許可權

//例如:msgget(1234, ipc_creat|0644);

//如果是開啟某乙個訊息佇列,則第二個引數為0

//建立成功返回值是訊息佇列的id,-1代表出錯

示例程式:

#include

#include

#include

#include

#include

#include

intmain

(void

)

ipcs -q   //檢視系統中的訊息佇列

ipcrm -q key //手動刪除指定的訊息佇列

int msgsnd(int msqid, const void *msgp, size_t msgsz, int msg***);

//引數一,要寫入的訊息佇列的id

//引數二,訊息內容,需要自己定義乙個指定的結構體

//引數三,訊息的大小,只算結構體的訊息資料的大小

//引數四,標誌位,一般寫0,如果想要非阻塞的話寫 ipc_nowait

//這個就是引數二的結構體了

struct msgbuf ;

示例程式:

#include

#include

#include

#include

#include

#include

struct msgbuf

;int

main

(void

)

ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, 

long msgtyp, int msg***);

//引數一:指定訊息佇列id

//引數二:裝獲取到的資料的結構體

//引數三:引數二裝資料的大小

//引數四:指定從哪個訊息通道獲取

//引數五:標誌位,一般寫0,如果想要非阻塞的話寫 ipc_nowait

示例程式:

#include

#include

#include

#include

#include

#include

struct msgbuf

;int

main

(void

)

手動關閉:

ipcrm -q key
也可以使用函式進行關閉

msgctl(id, ipc_rmid, 0);
cat /proc/sys/kernel/msgmax   

//得到訊息佇列的某乙個通道的乙個訊息的最大位元組

cat /proc/sys/kernel/msgmnb

//得到乙個訊息佇列的最大位元組

cat /proc/sys/kernel/msgmni

//得到系統中對多容納多少訊息佇列

linux 訊息佇列相關

ipcs u ipcs l 原型 int msgget key t key,int msg 引數 key 可以認為是乙個埠號,也可以由函式ftok生成。msg ipc creat值,若沒有該佇列,則建立乙個並返回新識別符號 若已存在,則返回原識別符號。ipc excl值,若沒有該佇列,則返回 1 若...

linux訊息佇列 Linux訊息佇列

訊息佇列,unix的通訊機制之一,可以理解為是乙個存放訊息 資料 容器。將訊息寫入訊息佇列,然後再從訊息佇列中取訊息,一般來說是先進先出的順序。可以解決兩個程序的讀寫速度不同 處理資料速度不同 系統耦合等問題,而且訊息佇列裡的訊息哪怕程序崩潰了也不會消失。最簡單的訊息記憶體的使用流程 ftok函式生...

vxworks訊息佇列相關函式和案例

標頭檔案 include msgqcreate 建立乙個訊息佇列 msg q id msgqcreate int maxmsgs,佇列中儲存的最大訊息數目 int maxmsglength,每個訊息的最大位元組數 int options 訊息在訊息佇列中的排列方式 options一般有兩個選項 ms...