1 消息隊列簡介
消息隊列提供了一個從一個進程向另外一個進程發送一塊數據的方法(本機);每個數據塊都被認爲是有一個類型,接收者進程接收的數據塊可以有不同的類型值。消息隊列也有管道一樣的不足:
(1)每個消息的最長字節數的上限(MSGMAX);
(2)系統中消息隊列的總條數也有一個上限(MSGMNI);
(3)每個消息隊列所能夠保存的總字節數是有上限的(MSGMNB).
消息隊列與管道的區別:最主要的區別是管道通信是要求兩個進程之間要有親緣關係,只能承載無格式的字節流,而消息隊列當中,通信的兩個進程之間可以是完全無關的進程,它是有格式的(可類比TCP\UDP)。至於它與有名管道的區別,首先FIFO是要存儲在磁盤上的一種通信方式,而消息隊列是在內存中的。
查看系統中的三個限制的上限:
[root@localhost ~]# cat /proc/sys/kernel/msgmax
8192
[root@localhost ~]# cat /proc/sys/kernel/msgmnb
16384
[root@localhost ~]# cat /proc/sys/kernel/msgmni
1942
2 IPC對象數據結構
// 內核爲每個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) 最後一個接受的進程號*/
};
3 消息隊列在內核中的表示
消息在消息隊列中是以鏈表形式組織的, 每個節點的類型類似如下:
struct msq_Node
{
Type msq_type; //類型
Length msg_len; //長度
Data msg_data; //數據
struct msg_Node *next;
};
4 消息隊列的基本API
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
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);
4.1 msgget
4.1.1 函數
int msgget(key_t key, int msgflg);
參數:
key: 某個消息隊列的名字
msgflg:由九個權限標誌構成,如0644,它們的用法和創建文件時使用的mode模式標誌是一樣的(但是消息隊列沒有x(執行)權限)
返回值:
成功返回消息隊列編號,即該消息隊列的標識碼;失敗返回-1
4.1.2 接下來我們舉幾個實例來看一下創建消息隊列的不同操作及其結果
(1) 在msgflg處指定IPC_CREAT, 如果不存在該消息隊列, 則創建之
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
// 指定IPC_CREAT,如果不存在, 則創建消息隊列
int msgid = msgget(1234, 0666|IPC_CREAT);
if (msgid == -1)
{
printf("msgget error!\n");
return -1;
}
printf("msgget success!\n");
return 0;
}
注意:KEY是16進制顯示。我們可以使用命令 ipcrm -q 刪除消息隊列或者 ipcrm -Q [key](此時key!=0); Ipcs查看消息隊列;要指定IPC_CREAT,纔可創建,類似於文件的creat。
ipcs 查看消息隊列
[root@localhost msg]# ipcs
------ Message Queues --------
key msqid owner perms used-bytes messages
0x000004d2 0 root 666 0 0
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
------ Semaphore Arrays --------
key semid owner perms nsems
[root@localhost msg]#
[root@localhost msg]#
(2) IPC_CREAT|IPC_EXCL, 如果該消息隊列已經存在, 則返回出錯
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
// 指定IPC_EXCL, 如果已經存在,則報告文件已經存在(錯誤)
int msgid = msgget(1234, 0666|IPC_CREAT|IPC_EXCL);
if (msgid == -1)
{
printf("msgget error!\n");
return -1;
}
printf("msgget success!\n");
return 0;
}
(3) 將key指定爲IPC_PRIVATE(值爲0)
將key指定爲IPC_PRIVATE之後,則每調用一次msgget會創建一個新的消息隊列。而且每次創建的消息隊列的描述符都是不同的! 因此, 除非將MessageID(key)傳送給其他進程(除非有關聯的進程),否則其他進程也無法使用該消息隊列。但是具有親緣進程的是可以使用的(fork)。因此, IPC_PRIVATE創建的消息隊列,只能用在與當前進程有關係的進程中使用!
這也意味着進程不能共享這個消息隊列,父子兄弟進程是可以的。
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
// 指定IPC_PRIVATE
int msgid = msgget(IPC_PRIVATE, 0666|IPC_CREAT|IPC_EXCL);
if (msgid == -1)
{
printf("msgget error!\n");
return -1;
}
printf("msgget success!\n");
return 0;
}
運行結果:
[root@localhost msg]# ipcs
------ Message Queues --------
key msqid owner perms used-bytes messages
0x00000000 65536 root 666 0 0
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
------ Semaphore Arrays --------
key semid owner perms nsems
[root@localhost msg]#
以上KEY爲0x00000000的都是使用IPC_PRIVATE創建的。
(4) 僅打開消息隊列時, msgflg選項可以直接忽略(填0), 此時是以消息隊列創建時的權限進行打開
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int msgid = msgget(1234, 0);
if (msgid == -1)
{
printf("msgget error!\n");
return -1;
}
printf("msgget success!\n");
printf("msgid = %d", msgid);
return 0;
}
(5) 低權限創建,高權限打開
會發生“Permission denied”的錯誤
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
int main()
{
// 低權限創建
int msgid = msgget(0x255,0444 | IPC_CREAT);
if (msgid < 0)
{
printf("mesget error!\n");
return -1;
}
else
{
printf("Create Mes OK, msgid = %d\n",msgid);
}
// 高權限打開
msgid = msgget(0x255,0644 | IPC_CREAT);
if (msgid < 0)
{
printf("mesget error!\n");
return -1;
}
else
{
printf("Create Mes OK, msgid = %d\n",msgid);
}
return 0;
}
4.2 msgctl函數
功能:獲取/設置消息隊列的信息
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
參數:
msqid: 由msgget函數返回的消息隊列標識碼
(1) IPC_RMID, 刪除消息隊列
注意: 消息隊列並沒有運用"引用計數"的功能
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
int main()
{
int msgid = msgget(0x255, 0444 | IPC_CREAT);
if (msgid == -1)
{
printf("msgget error!\n");
return -1;
}
if (msgctl(msgid, IPC_RMID, NULL) == -1)
{
printf("msgctl IPC_RMID error!\n");
return -1;
}
printf("msgctl IPC_RMID success!\n");
return 0;
}
(2) IPC_STAT,消息隊列相關信息讀入buf
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
int main()
{
int msgid = msgget(0x255, 0666|IPC_CREAT);
if (msgid == -1)
{
printf("msgget error");
return -1;
}
struct msqid_ds buf;
if (msgctl(msgid,IPC_STAT,&buf) == -1)
{
printf("msgctl error");
return -1;
}
printf("buf.msg_perm.mode = %o\n",buf.msg_perm.mode); // %o以八進制打印
printf("buf.__key = %x\n", buf.msg_perm.__key); // %x以十六進制打印
printf("buf.__msg_cbytes = %d\n", buf.__msg_cbytes);
printf("buf.msg_qbytes = %d\n", buf.msg_qbytes);
printf("buf.msg_lspid = %d\n", buf.msg_lspid);
return 0;
}
(3) IPC_SET,一般需要先獲取,然後再設置
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
int main()
{
int msgid = msgget(0x255, 0666|IPC_CREAT);
if (msgid == -1)
{
printf("msgget error!\n");
return -1;
}
// 獲取消息隊列的屬性
struct msqid_ds buf;
if (msgctl(msgid,IPC_STAT,&buf) == -1)
{
printf("msgctl error!\n");
return -1;
}
// 設置消息隊列的屬性
buf.msg_perm.mode = 0600;
if (msgctl(msgid, IPC_SET, &buf) == -1)
{
printf("msgctl error!\n");
return -1;
}
// 獲取並打印
bzero(&buf, sizeof(buf));
if (msgctl(msgid, IPC_STAT, &buf) == -1)
{
printf("msgctl IPC_STAT error!\n");
return -1;
}
printf("mode = %o\n", buf.msg_perm.mode);
return 0;
}
4.2 msgsnd
4.2.1 函數
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
參數
msgid: 由msgget函數返回的消息隊列標識碼, 也可以是通過ipcs命令查詢出來的一個已經存在的消息隊列的ID號
msgp:是一個指針,指針指向準備發送的消息,
msgsz:是msgp指向的消息長度, 注意: 這個長度不含保存消息類型的那個long int長整型
msgflg:控制着當前消息隊列滿或到達系統上限時將要發生的事情,如果msgflg = IPC_NOWAIT表示隊列滿不等待,返回EAGAIN錯誤。
另外:消息結構在兩方面受到制約:
(1)它必須小於系統規定的上限值(MSGMAX);
(2)它必須以一個long int長整數開始,接收者函數將利用這個長整數確定消息的類型;
// 消息結構參考形式如下:
struct msgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data, 可以設定爲更多的字節數 */
};
4.3.2 實例
/**
示例1:
測試1: 發送消息的最大長度爲8192字節, 一旦超過這個值, 則msgsnd出錯, 提示 Invalid argument錯誤;
測試2: 消息隊列所能夠接收的最大字節數16384字節, 一旦超過這個長度, 如果msgflg爲0(阻塞模式), 則進程會一直阻塞下去, 直到有進程來將消息取走; 而如果msgflg爲IPC_NOWAIT模式, 則一個字節也不會寫入消息隊列, 直接出錯返回;
**/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// argv[0]=./main ,argv[1]=type, argv[2]=len
if (argc != 3)
{
printf("Usage: ./main <type> <length>\n");
return -1;
}
int type = atoi(argv[1]);
int len = atoi(argv[2]);
int msgid = msgget(0x255, 0666|IPC_CREAT);
if (msgid == -1)
{
printf("msgget error!\n");
return -1;
}
struct msgbuf *buf = NULL;
buf = (struct msgbuf *)malloc(len + sizeof(msgbuf::mtype));
buf->mtype = type;
if (msgsnd(msgid, buf, len, IPC_NOWAIT) == -1) //非阻塞
{
printf("msgsnd error!\n");
return -1;
}
return 0;
}
4.4 msgrcv
4.4.1 函數
size_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
參數
msgid: 由msgget函數返回的消息隊列標識碼
msgp:是一個指針,指針指向準備接收的消息;
msgsz:是msgp指向的消息長度,這個長度不含保存消息類型的那個long int長整型
msgtype:它可以實現接收優先級的簡單形式(見下圖)
msgflg:控制着隊列中沒有相應類型的消息可供接收時將要發生的事(見下圖)
返回值:
成功->返回實際放到接收緩衝區裏去的字節數(注意: 此處並不包含msgbuf中的mtype的長度[man-page: msgrcv() returns the number of bytes actually copied into the mtext array.]);
失敗->返回-1;
msgtyp參數 |
|
msgtyp=0 |
返回隊列第一條信息 |
msgtyp>0 |
返回隊列第一條類型等於msgtype的消息 |
msgtyp<0 |
返回隊列第一條類型小於等於(<=)msgtype絕對值的消息,並且是滿足條件的消息類型最小的消息(按照類型進行排序的順序進行接收消息) |
msgflg參數 |
|
msgflg=IPC_NOWAIT |
隊列沒有可讀消息不等待,返回ENOMSG錯誤。 |
msgflg=MSG_NOERROR |
消息大小超過msgsz(msgrcv 函數的第三個參數)時被截斷, 並且不會報錯 |
msgtyp>0且msgflg=MSG_EXCEPT |
接收類型不等於msgtype的第一條消息
|
另外,下面的接受程序中,我們會用到一個用於命令行執行程序時的指定不同參數的函數getopt
我們先介紹一下這個函數的基本用法:
./main 1 234 -c -d
程序會根據讀取的參數執行相應的操作,在C語言中,這個功能一般是靠getopt()這個函數,結合switch語句來完成的:
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
// 示例: 解析 ./main -n -t 3 中的參數選項
int main(int argc, char *argv[])
{
while (true)
{
int opt = getopt(argc, argv, "nt:");
if (opt == '?')
{
exit(EXIT_FAILURE);
}
else if (opt == -1)
{
break;
}
switch (opt)
{
case 'n':
{
cout << "-n" << endl;
break;
}
case 't':
{
int n = atoi(optarg);
cout << "-t " << n << endl;
break;
}
}
}
}
例如參數"a:b::cde",表示可以有,-a,-b,-c,-d,-e 這幾個參數。
":" 表示必須該選項帶有額外的參數,全域變量optarg會指向此額外參數,"::"標識該額外的參數可選(有些Uinx可能不支持"::")。
全域變量optind指示下一個要讀取的參數在argv中的位置。
如果getopt()找不到符合的參數則會印出錯信息,並將全域變量 optopt 設爲“?”字符。
如果不希望getopt()印出錯信息,則只要將全域變量 opterr 設爲0即可。
/**
示例2: 消息接收(配合示例1中程序使用)
說明: -t [number], 指定接收消息的類型, 類型爲number的值
-n ,指定以IPC_NOWAIT模式接收消息
**/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/** 解析參數 **/
int type = 0;
int flag = 0;
int opt;
while ((opt = getopt(argc, argv, "nt:")) != -1)
{
switch (opt)
{
case 'n': // 指定IPC_NOWAIT選項
{
flag |= IPC_NOWAIT;
break;
}
case 't': // 指定接收的類型, 如果爲0的話,說明是按照順序接收
{
type = atoi(optarg);
break;
}
default:
{
exit(EXIT_FAILURE);
}
}
}
int msgid = msgget(0x255, 0);
if (msgid == -1)
{
printf("msgget error!\n");
return -1;
}
const int MSGMAX = 8192; //指定一條消息的最大長度
struct msgbuf *buf;
buf = (struct msgbuf *)malloc(MSGMAX + sizeof(buf->mtype));
ssize_t nrcv;
if ((nrcv = msgrcv(msgid, buf, MSGMAX, type, flag)) == -1)
{
printf("msgrcv error!\n");
return -1;
}
cout << "recv " << nrcv << " bytes, type = " << buf->mtype << endl;
return 0;
}
從消息隊列中發送/讀取消息:
/** 綜合示例: msgsnd/msgrcv, 消息發送/接收實踐 **/
// 1. 消息發送
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
struct myMsgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[1024]; /* message data, 可以設定爲更多的字節數 */
};
int main()
{
int msgid = msgget(0x1234,0666|IPC_CREAT);
if (msgid == -1)
{
printf("msgget error!\n");
return -1;
}
struct myMsgbuf myBuffer;
for (int i = 0; i < 10; ++i)
{
myBuffer.mtype = i+1;
sprintf(myBuffer.mtext,"Hello, My number is %d",i+1);
if (msgsnd(msgid,&myBuffer,strlen(myBuffer.mtext), IPC_NOWAIT) == -1)
{
printf("msgsnd error(%d)!\n", errno());
return -1;
}
}
return 0;
}
編譯執行:
[root@localhost thread]# g++ -o send ./send.cpp
[root@localhost thread]#
[root@localhost thread]# ./send
[root@localhost thread]#
[root@localhost thread]#
[root@localhost thread]# ipcs
------ Message Queues --------
key msqid owner perms used-bytes messages
0x00001234 32768 root 666 211 10
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
------ Semaphore Arrays --------
key semid owner perms nsems
[root@localhost thread]#
// 2. 消息接收:從隊首不斷的取數據
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct myMsgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[1024]; /* message data, 可以設定爲更多的字節數 */
};
int main(int argc, char *argv[])
{
int msgid = msgget(0x1234, 0);
if (msgid == -1)
{
printf("msgget error");
return -1;
}
struct myMsgbuf buf;
ssize_t nrcv;
while ((nrcv = msgrcv(msgid, &buf, sizeof(buf.mtext), 0, IPC_NOWAIT)) > 0)
{
cout << "recv " << nrcv << " bytes, type: " << buf.mtype
<< ", message: " << buf.mtext << endl;
}
return 0;
}
編譯執行
[root@localhost thread]# g++ -o rec ./rec.cpp
[root@localhost thread]#
[root@localhost thread]#
[root@localhost thread]# ./rec
recv 21 bytes, type: 1, message: Hello, My number is 1
recv 21 bytes, type: 2, message: Hello, My number is 2
recv 21 bytes, type: 3, message: Hello, My number is 3
recv 21 bytes, type: 4, message: Hello, My number is 4
recv 21 bytes, type: 5, message: Hello, My number is 5
recv 21 bytes, type: 6, message: Hello, My number is 6
recv 21 bytes, type: 7, message: Hello, My number is 7
recv 21 bytes, type: 8, message: Hello, My number is 8
recv 21 bytes, type: 9, message: Hello, My number is 9
recv 22 bytes, type: 10, message: Hello, My number is 10
[root@localhost thread]#
[root@localhost thread]#
[root@localhost thread]# ipcs
------ Message Queues --------
key msqid owner perms used-bytes messages
0x00001234 32768 root 666 0 0
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
------ Semaphore Arrays --------
key semid owner perms nsems
[root@localhost thread]#
本文轉自:
https://blog.csdn.net/NK_test/article/details/49757631