C++ 中消息隊(duì)列函數(shù)實(shí)例詳解
C++ 中消息隊(duì)列函數(shù)實(shí)例詳解
1.消息隊(duì)列結(jié)構(gòu)體的定義
typedef struct{ uid_t uid; /* owner`s user id */ gid_t gid; /* owner`s group id */ udi_t cuid; /* creator`s user id */ gid_t cgid; /* creator`s group id */ mode_t mode; /* read-write permissions 0400 MSG_R 0200 MSG_W*/ ulong_t seq; /* slot usage sequence number*/ }ipc_perm; typedef stuct{ struct ipc_perm msg_perm; /* read_write perms */ struct msg *msg_first; /* ptr to first message on queue */ struct msg *msg_last; /* ptr to last message on queue */ msglen_t msg_cbytes; /* used bytes current on queue */ msgqnum_t msg_qnum; /* current num of message on queue */ msglen_t msg_qbytes; /* max # of bytes allowed on queue */ pid_t msg_lspid; /* pid of last msgsnd() */ pid_t msg_lrpid; /* pid of last msgrcv() */ time_t msg_stime; /* time of last msgsnd() */ time_t msg_rtime; /* time of last msgrcv() */ time_t msg_ctime; /* time of last msgctl() */ }msqid_ds; typedef struct { long mtype; char mbuf[MSGLEN]; }Message;
2.創(chuàng)建消息隊(duì)列:
/*************************************************** Function: int msgget(ket_t key,int oflag); Explain: create or view a message queue Return : a int indetify Include: sys/msg.h introduction: oflag: 0400 msg_r 0200 msg_w 0600 msg_wr ipc_creat: NO exist and then creat a queue exist : reference a queue ipc_creat|ipc_excl: NO exist and then creat a queue exist : return error ****************************************************/ #include<stdio.h> #include<sys/msg.h> #include<stdlib.h> int MsgGet(int key) { int ret; ret=msgget(key,0600|IPC_CREAT); // ret=msgget(key,0600|IPC_CREAT|IPC_EXCL); if(ret<0) perror("creat msgid error"); printf("msgid=%d/n",ret); system("ipcs -q -i ret"); return ret; } int main(int argc,char *agrv[]) { int key; printf("pleasse input msgkey:"); scanf("%d",&key); MsgGet(key); return 0; }
3.向消息隊(duì)列中發(fā)送消息msgsnd
/*********************************************************************************** Function: int msgsnd(int msqid,const void *ptr,size_t length,int flag) Explain: send a message to a queue Return: len: send message len; Include: sys/msg.h Introduction: flag: 0 : if queue full wait:1>具備存放新消息的空間 2>由msqid標(biāo)識的消息隊(duì)列從系統(tǒng)中刪除(返回EIDRM錯誤) 3>調(diào)用線程被某個捕獲的信號所中斷(返回EINTR錯誤) IPC_NOWAIT:如果沒有存放新消息的空間,函數(shù)馬上返回 1>指定的隊(duì)列中有太多的字節(jié) 2>在系統(tǒng)范圍存在太多的消息 *****************************************************************************************/ #include "typemsg.h" int MsgSnd(int msqid,char *buf,int len,int flag) { int ret; ret=msgsnd(msqid,buf,len,flag); if(ret<0) perror("msgsnd error"); system("ipcs -q"); return ret; } int main() { int msqid,len,stype; Message msgb; memset(&msgb,0,sizeof(Message)); printf("msgsnd:please input msqid:"); scanf("%d",&msqid); printf("please input msgtype:"); scanf("%d",&stype); msgb.mtype=stype; strcpy(msgb.mbuf,"zhangweia"); MsgSnd(msqid,(char *)&msgb,sizeof(Message),0); return 0; }
4.從隊(duì)列中獲取消息 msgrcv
/********************************************************************* Function: int msgrcv(int msqid,const void *ptr,size_t msglen,long type,int flag) Explain: recv message order by type msgrcv error: Argument list too long --> msglen的長度小于消息體中消息的長度 Para : ptr: point to message struct msglen: 由ptr指向的緩沖區(qū)中數(shù)據(jù)部分的大小,這個是該函數(shù)能夠返回的最大數(shù)據(jù)量 type: message type; 1> 0:返回隊(duì)列中最早的消息 2> 大于0:返回消息隊(duì)列中類型為type的第一個消息 3> 小于0:返回消息隊(duì)列中類型小于或者等于type的絕對值的消息類型中最小的第一個消息 flag: 0<wait> 沒有消息或者消息類型不符合的時候,線程等待 響應(yīng): 1>有一個所請求類型的消息可以獲取 2>msqid的消息隊(duì)列被系統(tǒng)刪除,返回一個EIDRM 3>調(diào)用線程被某個捕獲的信號所中斷 IPC_NOWAIT:在沒有數(shù)據(jù)的情況下,立即返回一個ENOMSG錯誤 MSGNOERROR:當(dāng)所接受的消息數(shù)據(jù)部分大于msglen長度時,獲取截短的數(shù)據(jù)部分,否則返回E2BIG錯誤 Return: message len *********************************************************************/ #include "typemsg.h" int MsgRcv(int msqid,char *buf,int msglen,long type,int flag) { int ret; ret=msgrcv(msqid,buf,msglen,type,flag); if(ret<0) perror("msgrcv error"); system("ipcs -q"); return ret; } int main() { int msqid,len; long ttype; Message mbuf; printf("msgrcv:please input recv msqid:"); scanf("%d",&msqid); MsgRcv(msqid,(char *)&mbuf,8900,0,IPC_NOWAIT); printf("recv message=%s/n",mbuf.mbuf); Put_String((unsigned char *)&mbuf,sizeof(Message)); return 0; }
6.消息隊(duì)列的控制msgctl
/********************************************************** Function: int msgctl(int msqid,int cmd,struct msqid_ds *buff) Explain: cdm: IPC_RMID; delete msqid IPC_SET: IPC_STAT: return msqid stat *********************************************************/ #include "typemsg.h" int MsgCtl(int msqid,int cmd,struct msqid_ds *buff) { int ret; ret=msgctl(msqid,cmd,buff); if(ret<0) { perror("msgctl error"); return -1; } return 0; } int main() { int msqid,type; struct msqid_ds info; printf("please input msqid /nand type(1:icp_rmid;2:ipc_stat)"); scanf("%d%d",&msqid,&type); if(type==1) { MsgCtl(msqid,IPC_RMID,NULL); printf("delete queue success:%d/n",msqid); }else if(type==2) { MsgCtl(msqid,IPC_STAT,&info); printf("get queue stat:%d/n",msqid); } return 0; }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:C++ 中快排的遞歸和非遞歸實(shí)現(xiàn)
欄 目:C語言
下一篇:IOS 開發(fā)UITextView回收或關(guān)閉鍵盤
本文標(biāo)題:C++ 中消息隊(duì)列函數(shù)實(shí)例詳解
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1471.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子