訊息佇列(一)

2021-07-22 17:24:44 字數 1529 閱讀 7000

訊息佇列與pipe類似,但是存在兩個區別:

1. message boundaries are preserved, so that readers and writes communicate in units of messages, rather than via an undelimited byte stream.

2.each message includes an integer type field, and it's possible to select messages by type.

1.msqid_ds 訊息佇列資料結構: 用於標示整個訊息佇列的基本情況

2.msg 訊息佇列資料結構:整個訊息佇列的主體

#include #include #include #include #include #include int main(int argc, char *argv)

//建立訊息佇列,param1:key值,param2:許可權

if((msgid = msgget(key, 0666|ipc_creat)) == -1 )

/**訊息佇列屬性控制

* extern int msgctl(int __msqid, int __cmd, struct msqid_ds *__buf);

__cmd:

#define ipc_rmid 0 // remove resource  這種刪除是立即生效的,只有相應的許可權才能操作

#define ipc_set 1 // set ipc_perm options 

//設定訊息佇列的屬性,按照buf指向的結構體中的值:msg_perm.uid\msg_perm.gid\msg_perm\mode\msg_qbytes

//有效使用者id 等於msg_perm.cuid/msg_perm.uid或root才能執行

#define ipc_stat 2 // get ipc_perm options 取得此佇列的msqid_ds結構,並存在buf指向的結構體中

#define ipc_info 3 // see ipcs

*/msgctl(msgid, ipc_stat, &buf);

printf("the key: %d,\nthe uid:%d,\nthe gid:%d,\nthe cuid:%d,\nthe cgid:%d,\nthe mode:%o,\n\

the squence:%d\n",buf.msg_perm.__key, buf.msg_perm.uid,

buf.msg_perm.gid, buf.msg_perm.cuid, buf.msg_perm.cgid,

buf.msg_perm.mode, buf.msg_perm.__seq);

printf("the max bytes is: %lu\n", buf.msg_qbytes);

//刪除訊息佇列

msgctl(msgid, ipc_rmid, (struct msqid_ds *)0);

return 0;

}

訊息佇列(一)

在不使用訊息佇列伺服器的時候,使用者的請求資料直接寫入資料庫,在高併發的情況下資料庫壓力劇增,使得響應速度變慢。但是在使用訊息佇列之後,使用者的請求資料傳送給訊息佇列之後立即返回,再由訊息佇列的消費者程序從訊息佇列中獲取資料,非同步寫入資料庫。由於訊息佇列伺服器處理速度快於資料庫 訊息佇列也比資料庫...

一 訊息佇列

訊息佇列 message queue,簡稱為mq 是一種應用間的通訊方式,訊息傳送後可以立即返回,由訊息系統來確保訊息的可靠傳遞。訊息發布者只管把訊息發布到 mq 中而不用管誰來取,訊息使用者只管從 mq 中取訊息而不管是誰發布的。這樣發布者和使用者都不用知道對方的存在。訊息佇列中介軟體是分布式系統...

訊息佇列 訊息佇列

輪詢排程 一次性分發所有訊息,保證訊息平均分配,不管消費者是否能正常消費 訊息應答 保證消費端能確實消費,不丟失 公平 乙個乙個分發所有訊息,在保證分發到的執行緒確認回覆後,才分發下個訊息給下個空閒的消費者,訊息持久化 保證佇列中的訊息不丟失,包括3要素 交換器 訊息佇列 訊息都必須宣告持久化 發布...