(1)每個消息的最長字節數的上限(MSGMAX);
(2)系統中消息隊列的總條數也有一個上限(MSGMNI);
(3)每個消息隊列所能夠保存的總字節數是有上限的(MSGMNB) .
消息隊列與管道的區別:最主要的區別是管道通信是要求兩個進程之間要有親緣關系,只能承載無格式的字節流,而消息隊列當中,通信的兩個進程之間可以是完全無關的進程,它是有格式的(可類比TCP\UDP)。至於它與有名管道的區別,首先FIFO是要存儲在磁盤上的一種通信方式,而消息隊列是在內存中的。
查看系統中的三個限制的上限:

//內核為每個IPC對象維護一個數據結構
struct ipc_perm
{
key_t __key; /* Key supplied to msgget(2) */
uid_t uid; /* Effective UID of owner */
gid_t gid; /* Effective GID of owner */
uid_t cuid; /* Effective UID of creator */
gid_t cgid; /* Effective GID of creator */
unsigned short mode; /* Permissions */
unsigned short __seq; /* Sequence number */
};
//消息隊列特有的結構
struct msqid_ds
{
struct ipc_perm msg_perm; /* Ownership and permissions 各類IPC對象所共有的數據結構*/
time_t msg_stime; /* Time of last msgsnd(2) */
time_t msg_rtime; /* Time of last msgrcv(2) */
time_t msg_ctime; /* Time of last change */
unsigned long __msg_cbytes; /* Current number of bytes in queue (nonstandard) 消息隊列中當前所保存的字節數 */
msgqnum_t msg_qnum; /* Current number of messages in queue 消息隊列中當前所保存的消息數 */
msglen_t msg_qbytes; /* Maximum number of bytes allowed in queue 消息隊列所允許的最大字節數 */
pid_t msg_lspid; /* PID of last msgsnd(2) 最後一個發送數據的進程號*/
pid_t msg_lrpid; /* PID of last msgrcv(2) 最後一個接受的進程號*/
};
消息隊列在內核中的表示

消息在消息隊列中是以鏈表形式組織的, 每個節點的類型類似如下:
struct msq_Node
{
Type msq_type; //類型
Length msg_len; //長度
Data msg_data; //數據
struct msg_Node *next;
};
消息隊列的基本API
#include本節先介紹前2個API,發送和接受消息的API請參考下篇博客。#include #include int msgget(key_t key, int msgflg); int msgctl(int msqid, int cmd, struct msqid_ds *buf); int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
msgget
int msgget(key_t key, int msgflg);
參數:
key: 某個消息隊列的名字
msgflg:由九個權限標志構成,如0644,它們的用法和創建文件時使用的mode模式標志是一樣的(但是消息隊列沒有x(執行)權限)
返回值:
成功返回消息隊列編號,即該消息隊列的標識碼;失敗返回-1

/** 示例1: 在msgflg處指定IPC_CREAT, 如果不存在該消息隊列, 則創建之**/
int main(int argc, char *argv[])
{
//指定IPC_CREAT,如果不存在, 則創建消息隊列
int msgid = msgget(1234, 0666|IPC_CREAT);
if (msgid == -1)
err_exit("msgget error");
cout << "msgget success" << endl;
}
注意:KEY是16進制顯示。我們可以使用命令 ipcrm -q 刪除消息隊列或者 ipcrm -Q [key](此時key!=0); Ipcs查看消息隊列;要指定IPC_CREAT,才可創建,類似於文件的creat

/** 示例2:IPC_CREAT|IPC_EXCL, 如果該消息隊列已經存在, 則返回出錯 **/
int main(int argc, char *argv[])
{
//指定IPC_EXCL, 如果已經存在,則報告文件已經存在(錯誤)
int msgid = msgget(1234, 0666|IPC_CREAT|IPC_EXCL);
if (msgid == -1)
err_exit("msgget error");
cout << "msgget success" << endl;
}
/**示例3:將key指定為IPC_PRIVATE(值為0)
將key指定為IPC_PRIVATE之後,則每調用一次msgget會創建一個新的消息隊列
而且每次創建的消息隊列的描述符都是不同的! 因此, 除非將MessageID(key)傳送給其他進程(除非有關聯的進程),否則其他進程也無法使用該消息隊列
但是具有親緣進程的是可以使用的(fork)
因此, IPC_PRIVATE創建的消息隊列,只能用在與當前進程有關系的進程中使用!
**/
這也意味著進程不能共享這個消息隊列,父子兄弟進程是可以的
int main(int argc, char *argv[])
{
//指定IPC_PRIVATE
int msgid = msgget(IPC_PRIVATE, 0666|IPC_CREAT|IPC_EXCL);
if (msgid == -1)
err_exit("msgget error");
cout << "msgget success" << endl;
}

/** 示例4: 僅打開消息隊列時, msgflg選項可以直接忽略(填0), 此時是以消息隊列創建時的權限進行打開
**/
int main(int argc, char *argv[])
{
int msgid = msgget(1234, 0);
if (msgid == -1)
err_exit("msgget error");
cout << "msgget success" << endl;
cout << "msgid = " << msgid << endl;
}
//示例5:低權限創建,高權限打開
int main()
{
//低權限創建
int msgid = msgget(0x255,0444 | IPC_CREAT);
if (msgid < 0)
err_exit("mesget error");
else
cout << "Create Mes OK, msgid = " << msgid << endl;
//高權限打開
msgid = msgget(0x255,0644 | IPC_CREAT);
if (msgid < 0)
err_exit("mesget error");
else
cout << "Create Mes OK, msgid = " << msgid << endl;
}
會發生“Permission denied”的錯誤。
msgctl函數
功能:獲取/設置消息隊列的信息
[cpp] view plaincopy參數:
msqid: 由msgget函數返回的消息隊列標識碼

/** 示例1: IPC_RMID, 刪除消息隊列
注意: 消息隊列並沒有運用”引用計數”的功能
**/
int main()
{
int msgid = msgget(1234, 0);
if (msgid == -1)
err_exit("msgget error");
if (msgctl(msgid, IPC_RMID, NULL) == -1)
err_exit("msgctl IPC_RMID error");
cout << "msgctl IPC_RMID success" << endl;
}
/** 示例2: IPC_STAT,消息隊列相關信息讀入buf
**/
int main()
{
int msgid = msgget(0x255, 0666|IPC_CREAT);
if (msgid == -1)
err_exit("msgget error");
struct msqid_ds buf;
if (msgctl(msgid,IPC_STAT,&buf) == -1)
err_exit("msgctl error");
printf("buf.msg_perm.mode = %o\n",buf.msg_perm.mode); //%o以八進制打印
printf("buf.__key = %x\n", buf.msg_perm.__key); //%x以十六進制打印
cout << "buf.__msg_cbytes = " << buf.__msg_cbytes << endl;
cout << "buf.msg_qbytes = " << buf.msg_qbytes << endl;
cout << "buf.msg_lspid = " << buf.msg_lspid << endl;
}
** 實踐:IPC_SET,一般需要先獲取,然後再設置
**/
int main()
{
int msgid = msgget(0x255, 0);
if (msgid == -1)
err_exit("msgget error");
//獲取消息隊列的屬性
struct msqid_ds buf;
if (msgctl(msgid,IPC_STAT,&buf) == -1)
err_exit("msgctl error");
//設置消息隊列的屬性
buf.msg_perm.mode = 0600;
if (msgctl(msgid, IPC_SET, &buf) == -1)
err_exit("msgctl error");
//獲取並打印
bzero(&buf, sizeof(buf));
if (msgctl(msgid, IPC_STAT, &buf) == -1)
err_exit("msgctl IPC_STAT error");
printf("mode = %o\n", buf.msg_perm.mode);
}