C 獲取訊息佇列的屬性實戰

2021-09-16 21:19:01 字數 2675 閱讀 1937

一 **

#include #include #include #include #include "stdio.h"

#include "errno.h"

void msg_stat(int,struct msqid_ds );

main()

msg_sbuf;

struct msgmbuf

msg_rbuf;

struct msqid_ds msg_ginfo;

char msgpath="./test";

key=ftok(msgpath,'b');

gflags=ipc_creat|ipc_excl;

msgid=msgget(key,gflags|00666);

if(msgid==-1)

//建立乙個訊息佇列後,輸出訊息佇列預設屬性

msg_stat(msgid,msg_ginfo);

sflags=ipc_nowait;

msg_sbuf.mtype=10;

msg_sbuf.mtext[0]='a';

reval=msgsnd(msgid,&msg_sbuf,sizeof(msg_sbuf.mtext),sflags);

if(reval==-1)

//傳送乙個訊息後,輸出訊息佇列屬性

msg_stat(msgid,msg_ginfo);

reval=msgctl(msgid,ipc_rmid,null);//刪除訊息佇列

if(reval==-1)

}void msg_stat(int msgid,struct msqid_ds msg_info)

printf("\n");

printf("current number of bytes on queue is %d\n",msg_info.msg_cbytes);

printf("number of messages in queue is %d\n",msg_info.msg_qnum);

printf("max number of bytes on queue is %d\n",msg_info.msg_qbytes);

//每個訊息佇列的容量(位元組數)都有限制msgmnb,值的大小因系統而異。在建立新的訊息佇列時,//msg_qbytes的預設值就是msgmnb

printf("pid of last msgsnd is %d\n",msg_info.msg_lspid);

printf("pid of last msgrcv is %d\n",msg_info.msg_lrpid);

printf("last msgsnd time is %s", ctime(&(msg_info.msg_stime)));

printf("last msgrcv time is %s", ctime(&(msg_info.msg_rtime)));

printf("last change time is %s", ctime(&(msg_info.msg_ctime)));

printf("msg uid is %d\n",msg_info.msg_perm.uid);

printf("msg gid is %d\n",msg_info.msg_perm.gid);

}

二 結果

[root@localhost test]# g++ test.cpp -o test

[root@localhost test]# ./test

current number of bytes on queue is 0

number of messages in queue is 0

max number of bytes on queue is 16384

pid of last msgsnd is 0

pid of last msgrcv is 0

last msgsnd time is thu jan 1 08:00:00 1970

last msgrcv time is thu jan 1 08:00:00 1970

last change time is fri apr 5 19:11:13 2019

msg uid is 0

msg gid is 0

current number of bytes on queue is 1

number of messages in queue is 1

max number of bytes on queue is 16384

pid of last msgsnd is 1055

pid of last msgrcv is 0

last msgsnd time is fri apr 5 19:11:15 2019

last msgrcv time is thu jan 1 08:00:00 1970

last change time is fri apr 5 19:11:13 2019

msg uid is 0

msg gid is 0

三 說明

可以看到,剛開始時,訊息佇列的訊息是0,傳送乙個後,佇列裡面的訊息個數就變成1了。因為我們傳送的訊息是字元「a」,長度是1,所以訊息佇列的長度就是乙個位元組。

從執行緒的佇列中提取訊息的演算法

當乙個執行緒呼叫 getmessage 或 peekmessage 時,系統必須檢查執行緒的佇列狀態的情況,並確定應該處理哪個訊息。1 如果 qs sendmessage 標誌被設定,系統向相應的視窗過程傳送訊息。getmessage 或 peekmessage 函式在內部進行這種處理,並且在視窗過...

IPC通訊 Posix訊息佇列的屬性設定

posix訊息佇列的屬性使用如下結構存放 struct mq attr 佇列可以在建立時由mq open 函式的第四個引數指定mq maxmsg,mq msgsize。如建立時沒有指定則使用預設值,一旦建立,則不可再改變。佇列可以在建立後由mq setattr 函式設定mq flags includ...

mq獲取訊息慢 ActiveMQ的慢消費者

慢消費者 消費訊息過慢的consumer subscriber。慢消費者會對broker產生影響,尤其是non durable的topic和queue,topic尤甚,會導致broker的記憶體爆滿,阻塞publisher。1.non durable topics 受慢消費者影像最大,傳送到non ...