systerm v
訊息佇列:
建立system v訊息佇列
id =
msgget
(key, ipc_creat | s_irusr | s_iwusr);if
(id ==-1
)errexit
("msgget"
);
msgget
完成下列某個操作之一:
msgget呼叫將乙個key(整數值),轉化為相應的整數ipc識別符號。
產生唯一的ipc key
的方法:
svmsgcreate.c
:
#include
#include
#include
#include
#include
"tlpi_hdr.h"
static
void
usageerror
(const
char
*progname,
const
char
*msg)
""[octal-perms]\n"
, progname)
;fprintf
(stderr
," -c use ipc_creat flag\n");
fprintf
(stderr
," -x use ipc_excl flag\n");
fprintf
(stderr
," -f pathname generate key use ftk\n");
fprintf
(stderr
," -k key use 'key' as key\n");
fprintf
(stderr
," -p use ipc_private key\n");
exit
(exit_failure);}
intmain
(int argc,
char
*ar**)
}if(numkeyflags !=1)
usageerr
(ar**[0]
,"exactly one of the options -f, -k,"
"or -p must be supplied\n");
perms =
(optind == argc)
?(s_irusr | s_iwusr)
:getint
(ar**[optind]
, gn_base_8,
"octal-perms");
msqid =
msgget
(key, flags | perms);if
(msqid ==-1
)errexit
("msgget");
printf
("%d\n"
, msqid)
;exit
(exit_failure)
;}
傳送訊息:
#include
#include
intmsgsnd
(int msqid,
const
void
*msgp, size_t msgsz,
int msg***)
;
msgp是指向有程式設計師定義的用於收發訊息的結構體的void
指標,通常具有以下形式:
strut mymsg
;
傳送的訊息中,mtype
的值必須大於零,mtext
可以為零;
svmsgsend.c
:
#include
#include
#include
"tlpi_hdr.h"
#define max_mtext 1024
struct mbuf
;static
void
usageerror
(const
char
*progname,
const
char
*msg)
intmain
(int argc,
char
*ar**)
}if(argc < optind +
2|| argc > optind +3)
usageerror
(ar**[0]
,"wrong number of arguments\n");
msqid =
getint
(ar**[optind],0
,"msqid");
msg.mtype =
getint
(ar**[optind +1]
,0,"msg-type");
if(argc > optind +2)
elseif(
msgsnd
(msqid,
&msg, msglen, flags)==-
1)errexit
("msgsnd");
exit
(exit_success)
;}
接收訊息:
#include
#include
intmsgrcv
(int msqid,
const
void
*msgp, size_t maxmsgsz,
long msgtyp,
int msg***)
;
svmsgreceive.c
:
#include
#include
#include
"tlpi_hdr.h"
#define max_mtext 1024
struct mbuf
;static
void
usageerror
(const
char
*progname,
const
char
*msg)
intmain
(int argc,
char
*ar**)
}if(argc < optind +
1|| argc > optind +2)
usageerror
(ar**[0]
,"wrong number of arguments\n");
msqid =
getint
(ar**[optind],0
,"msqid");
maxbytes =
(argc > optind +1)
?getint
(ar**[optind +1]
,0,"max-bytes"
): max_mtext;
msglen =
msgrcv
(msqid,
&msg, maxbytes, type, flags);if
(msglen ==-1
)errexit
("msgrcv");
printf
("received: type = %ld; length = %ld;"
, msg.mtype,
(long
)msglen);if
(msglen >0)
printf
(" body = %s\n"
, msg.mtext)
;exit
(exit_success)
;}
#include
#include
intmsgctl
(int msqid,
int cmd,
struct msqid_ds *buf)
;
struct msqid_ds *buf
為每個訊息佇列關聯的資料結構;
if
(msgctl
(clientid, ipc_rmid,
null)==
-1)errexit
("msgctl"
);
msgctl
的ipc_rmid
選項用於刪除乙個物件;
共享記憶體物件的刪除,只有所有使用該共享記憶體段的程序與之分離後刪除操作才會生效;
msvsgremove.c
:
#include
#include
#include
"tlpi_hdr.h"
intmain
(int argc,
char
*ar**)
exit
(exit_success)
;}
訊息佇列 訊息佇列
輪詢排程 一次性分發所有訊息,保證訊息平均分配,不管消費者是否能正常消費 訊息應答 保證消費端能確實消費,不丟失 公平 乙個乙個分發所有訊息,在保證分發到的執行緒確認回覆後,才分發下個訊息給下個空閒的消費者,訊息持久化 保證佇列中的訊息不丟失,包括3要素 交換器 訊息佇列 訊息都必須宣告持久化 發布...
訊息佇列 訊息佇列 kafka
kafka是乙個分布式的基於發布 訂閱模式的訊息佇列,主要用於大資料實時處理領域。要理解kafka首先要有分布式的概念,要有訊息佇列的概念。分布式系統最大的優勢就是解耦和削峰,這種情況下,a系統生成了乙個訊息,b系統非同步獲取,那麼就需要乙個存放訊息的訊息佇列 mq 相比較傳統的訊息佇列,訊息被消費...
linux訊息佇列 Linux訊息佇列
訊息佇列,unix的通訊機制之一,可以理解為是乙個存放訊息 資料 容器。將訊息寫入訊息佇列,然後再從訊息佇列中取訊息,一般來說是先進先出的順序。可以解決兩個程序的讀寫速度不同 處理資料速度不同 系統耦合等問題,而且訊息佇列裡的訊息哪怕程序崩潰了也不會消失。最簡單的訊息記憶體的使用流程 ftok函式生...