消息隊(duì)列是消息的鏈接列表,包括POSIX消息隊(duì)列SystemV消息隊(duì)列,具有足夠權(quán)限的進(jìn)程可以向隊(duì)列中添加消息,具有讀取權(quán)限的進(jìn)程可以從隊(duì)列中讀取消息,下文是詳解進(jìn)程間通信之深入消息隊(duì)列的特點(diǎn),武林技術(shù)頻道小編帶你細(xì)細(xì)品味。
詳解進(jìn)程間通信之深入消息隊(duì)列的特點(diǎn)
一、消息隊(duì)列的特點(diǎn)
? ??1.消息隊(duì)列是消息的鏈表,具有特定的格式,存放在內(nèi)存中并由消息隊(duì)列標(biāo)識(shí)符標(biāo)識(shí).
? ??2.消息隊(duì)列允許一個(gè)或多個(gè)進(jìn)程向它寫(xiě)入與讀取消息.
? ??3.管道和命名管道都是通信數(shù)據(jù)都是先進(jìn)先出的原則。
? ??4.消息隊(duì)列可以實(shí)現(xiàn)消息的隨機(jī)查詢,消息不一定要以先進(jìn)先出的次序讀取,也可以按消息的類(lèi)型讀取.比FIFO更有優(yōu)勢(shì)。
? ??目前主要有兩種類(lèi)型的消息隊(duì)列:POSIX消息隊(duì)列以及系統(tǒng)V消息隊(duì)列,系統(tǒng)V消息隊(duì)列目前被大量使用。系統(tǒng)V消息隊(duì)列是隨內(nèi)核持續(xù)的,只有在內(nèi)核重起或者人工刪除時(shí),該消息隊(duì)列才會(huì)被刪除。
二、相關(guān)函數(shù)
1. 獲得key值
? ? key_t ftok(char *pathname, int projid)
#include <sys/types.h>
#include <sys/ipc.h>
參數(shù):
? ? pathname:文件名(含路徑),通常設(shè)置為當(dāng)前目錄“.” 比如projid為'a',則為"./a"文件
? ? projid:項(xiàng)目ID,必須為非0整數(shù)(0-255).
2. 創(chuàng)建消息隊(duì)列
? ? int msgget(key_t key, int msgflag)
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
功能:
? ? 用于創(chuàng)建一個(gè)新的或打開(kāi)一個(gè)已經(jīng)存在的消息隊(duì)列,此消息隊(duì)列與key相對(duì)應(yīng)。
參數(shù):
? ??key:函數(shù)ftok的返回值或IPC_PRIVATE。
? ??msgflag:
? ??? ??IPC_CREAT:創(chuàng)建新的消息隊(duì)列。
? ??? ??IPC_EXCL:與IPC_CREAT一同使用,表示如果要?jiǎng)?chuàng)建的消息隊(duì)列已經(jīng)存在,則返回錯(cuò)誤。
? ??? ??IPC_NOWAIT:讀寫(xiě)消息隊(duì)列要求無(wú)法滿足時(shí),不阻塞。
返回值:
? ??調(diào)用成功返回隊(duì)列標(biāo)識(shí)符,否則返回-1.
在以下兩種情況下,將創(chuàng)建一個(gè)新的消息隊(duì)列:
? ??1、如果沒(méi)有與鍵值key相對(duì)應(yīng)的消息隊(duì)列,并且msgflag中包含了IPC_CREAT標(biāo)志位。
? ??2、key參數(shù)為IPC_PRIVATE。
3. 消息隊(duì)列屬性控制
? ? int msgctl(int msqid,? int cmd,? struct msqid_ds *buf)
功能:
?對(duì)消息隊(duì)列進(jìn)行各種控制操作,操作的動(dòng)作由cmd控制。
參數(shù):
? ??msqid:消息隊(duì)列ID,消息隊(duì)列標(biāo)識(shí)符,該值為msgget創(chuàng)建消息隊(duì)列的返回值。
? ??cmd:
? ??? ??IPC_STAT:將msqid相關(guān)的數(shù)據(jù)結(jié)構(gòu)中各個(gè)元素的當(dāng)前值存入到由buf指向的結(jié)構(gòu)中.
? ??? ??IPC_SET:將msqid相關(guān)的數(shù)據(jù)結(jié)構(gòu)中的元素設(shè)置為由buf指向的結(jié)構(gòu)中的對(duì)應(yīng)值.
? ??? ??IPC_RMID:刪除由msqid指示的消息隊(duì)列,將它從系統(tǒng)中刪除并破壞相關(guān)數(shù)據(jù)結(jié)構(gòu).
buf:消息隊(duì)列緩沖區(qū)
?? ??struct msqid_ds {
?????????????? struct ipc_perm msg_perm; ? ? ? ? ?/* Ownership and permissions*/
?????????????? time_t???????? msg_stime; ? ? ? ? ? ? ? ? /* Time of last msgsnd() */
?????????????? time_t???????? msg_rtime; ? ? ? ? ? ? ? ? ?/* Time of last msgrcv() */
?????????????? time_t???????? msg_ctime; ? ? ? ? ? ? ? ? /* Time of last change */
?????????????? unsigned long? __msg_cbytes; ? ?/* Current number of bytes in? queue (non-standard) */
?????????????? msgqnum_t????? msg_qnum; ? ? ? ? ?/* Current number of messages? in queue */
?????????????? msglen_t?????? msg_qbytes; ? ? ? ? ? /* Maximum number of bytesallowed in queue */
?????????????? pid_t????????? msg_lspid; ? ? ? ? ? ? ? ? ?/* PID of last msgsnd() */
?????????????? pid_t????????? msg_lrpid; ? ? ? ? ? ? ? ? ?/* PID of last msgrcv() */
?????????? ????????? };
?? ??struct ipc_perm {
?????????????? key_t key;???????? ?????????????? /* Key supplied to msgget() */
?????????????? 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 */
?????????? ??????? };
4.發(fā)送信息到消息隊(duì)列
? ? int msgsnd(int msqid,? struct msgbuf *msgp,? size_t msgsz,? int msgflag)
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
功能:
? ? 將新消息添加到隊(duì)列尾端,即向消息隊(duì)列中發(fā)送一條消息。
參數(shù):
? ??msqid:已打開(kāi)的消息隊(duì)列id
? ??msgp:存放消息的結(jié)構(gòu)體指針。
? ??msgflag:函數(shù)的控制屬性。
? ??消息結(jié)構(gòu)msgbuf為:
? ??struct msgbuf
? ??{
?? ??? ??long mtype;//消息類(lèi)型
?? ??? ??char mtext[1];//消息正文,消息數(shù)據(jù)的首地址,這個(gè)數(shù)據(jù)的最大長(zhǎng)度為8012吧,又可把他看成是一個(gè)結(jié)構(gòu),也有類(lèi)型和數(shù)據(jù),recv時(shí)解析即可。
? ??}
? ??msgsz:消息數(shù)據(jù)的長(zhǎng)度。
? ??msgflag:
? ??? ???IPC_NOWAIT: 指明在消息隊(duì)列沒(méi)有足夠空間容納要發(fā)送的消息時(shí),msgsnd立即返回。
? ??? ???0:msgsnd調(diào)用阻塞直到條件滿足為止.(一般選這個(gè))
5. 從消息隊(duì)列接收信息
? ? ssize_t msgrcv(int msqid,? struct msgbuf *msgp,? size_t msgsz,? long msgtype,? int msgflag)
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
功能:
? ? 從隊(duì)列中接收消息
參數(shù):
? ??msqid:已打開(kāi)的消息隊(duì)列id
? ??msgp:存放消息的結(jié)構(gòu)體指針。msgp->mtype與第四個(gè)參數(shù)是相同的。
? ??msgsz:消息的字節(jié)數(shù),指定mtext的大小。
? ??msgtype:消息類(lèi)型,消息類(lèi)型 mtype的值。如果為0,則接受該隊(duì)列中的第一條信息,如果小于0,則接受小于該值的絕對(duì)值的消息類(lèi)型,如果大于0,接受指定類(lèi)型的消息,即該值消息。
? ??msgflag:函數(shù)的控制屬性。
? ??msgflag:
? ??? ??MSG_NOERROR:若返回的消息比nbytes字節(jié)多,則消息就會(huì)截短到nbytes字節(jié),且不通知消息發(fā)送進(jìn)程.
? ??? ??IPC_NOWAIT:調(diào)用進(jìn)程會(huì)立即返回.若沒(méi)有收到消息則返回-1.
? ??? ??0:msgrcv調(diào)用阻塞直到條件滿足為止.
在成功地讀取了一條消息以后,隊(duì)列中的這條消息將被刪除。
三、相關(guān)例子
例1:消息隊(duì)列之簡(jiǎn)單收發(fā)測(cè)試
#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
struct msg_buf{
? ? ?int mtype;//消息類(lèi)型
? ???char data[255];//數(shù)據(jù)
};
int main(int argc,? char *argv[])
{
? ????key_t key;
? ????int msgid;
? ????int ret;
? ????struct msg_buf msgbuf;
?? ???//獲取key值
? ????key = ftok(".", ?'a');
?? ???printf("key = [%x]/n",? key);
? ????//創(chuàng)建消息隊(duì)列
? ????msgid = msgget(key,? IPC_CREAT|0666);/*通過(guò)文件對(duì)應(yīng)*/
?? ???if(msgid == -1)
?? ???{
?? ???? ????printf("creat error/n");
?? ???? ????return -1;
? ????}
?? ???//以當(dāng)前進(jìn)程類(lèi)型,非阻塞方式發(fā)送"test data"到消息隊(duì)列
? ????msgbuf.mtype = getpid();
?? ???strcpy(msgbuf.data,? "test data");
?? ???ret = msgsnd(msgid,? &msgbuf,? sizeof(msgbuf.data),? IPC_NOWAIT);
? ????if(ret == -1)
? ????{
?? ???? ????printf("send message err/n");
? ???? ?????return -1;
? ????}
?? ???//以非阻塞方式接收數(shù)據(jù)
? ????memset(&msgbuf,? 0,? sizeof(msgbuf));
? ????ret = msgrcv(msgid,? &msgbuf,? sizeof(msgbuf.data),?getpid(),? IPC_NOWAIT);
? ????if(ret == -1)
? ????{
? ???? ?????printf("receive message err/n");
?? ???? ????return -1;
? ????}
? ????printf("receive msg = [%s]/n",? msgbuf.data);
? ????return 0;
}
例2:進(jìn)程間消息隊(duì)列通信
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <string.h>
#include <signal.h>
struct msgbuf{
? ? ??int mtype;
? ????char mtext[100];
};
int main(void)
{
? ????key_t key;
? ????pid_t pid;
?? ???int msgid;
?? ???struct msgbuf msg;
?? ???key=ftok(".", 0x01);
?? ???if ( (msgid = msgget(key, ?IPC_CREAT|0666)) <0 )
?? ???{
?? ???? ????perror("msgget error");
?? ???? ????exit(1);
? ????}
?? ???//創(chuàng)建一個(gè)進(jìn)程
? ????if ( (pid = fork()) < 0 )
? ????{
? ???? ?????perror("fork error");
? ???? ?????exit(1);
? ????}
?? ???//子進(jìn)程收信息
? ????else if (pid==0)
? ????{
?? ???? ????while(1)
? ???? ?????{
?? ???? ???? ?????memset(msg.mtext, 0, 100);
? ???? ???? ??????msgrcv(msgid, &msg, 100,?2, 0);? //receive the msg from 2
?? ???? ???? ?????printf("/receive:%s/n:", msg.mtext);
??? ???? ???? ????fflush(stdout);
??? ???? ???}
??? ???? ???exit(0);
?? ???}
? ????//父進(jìn)程發(fā)信息
? ????else
? ????{
?? ???? ????while(1)
? ???? ?????{
??? ???? ???? ????memset(msg.mtext, 0, 100);
??? ???? ???? ????printf("father:");
??? ???? ???? ????fgets(msg.mtext, 100, stdin);
??? ???? ???? ????if (strncmp("bye", msg.mtext, 3)==0)//如果前3個(gè)字符為bye,則退出
??? ???? ???? ????{
??? ???? ???? ???? ?????kill(pid, SIGSTOP);
???? ???? ???? ???? ????exit(1);
??? ???? ???? ????}
?? ???? ???? ?????msg.mtype=1;//send to 1
??? ???? ???? ????msg.mtext[strlen(msg.mtext)-1]='/0';
??? ???? ???? ????msgsnd(msgid, &msg, strlen(msg.mtext)+1, 0);
?? ???? ????}
? ????}
?? ???return 0;
}
???這個(gè)程序的缺點(diǎn)為:有發(fā)無(wú)收,有收無(wú)發(fā)。可在這2個(gè)進(jìn)程中分別創(chuàng)建2個(gè)線程,分別負(fù)責(zé)收和發(fā),就完成了進(jìn)程間的通信。
以上就是關(guān)于詳解進(jìn)程間通信之深入消息隊(duì)列的特點(diǎn),如果你還想了解更多這方面的信息,你可以關(guān)注武林技術(shù)頻道,這里有著最專業(yè)的信息和服務(wù)。
新聞熱點(diǎn)
疑難解答
圖片精選