一.消息隊列
成都創(chuàng)新互聯(lián)公司服務(wù)項目包括方正網(wǎng)站建設(shè)、方正網(wǎng)站制作、方正網(wǎng)頁制作以及方正網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,方正網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到方正省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
消息隊列是一個進程向另一個進程發(fā)送一個數(shù)據(jù)塊的方法,所以消息隊列是基于消息的,而管道則是基于字節(jié)流的。消息隊列提供的是進程間的雙向通信。
消息隊列中的幾個原型函數(shù):
1.獲取消息信息:int msgget(key_t key,int msgflag);key 是用ftok()函數(shù)創(chuàng)建的
2.接收消息:ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
3.發(fā)送消息:int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
4.銷毀消息信息:int msfctl(int msgid)
查看key值命令:ipcs -q
刪除key值命令:ipcs -q key值
//comm.h #pragma once #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/types.h> #include<unistd.h> #include<sys/ipc.h> #include<sys/msg.h> #define _PATH_ "." //路徑 #define _PROJ_ID_ 0x7777 #define _BLOCK_SIZE_ 1024 #define _CLIENT_TYPE_ 1 #define _SERVER_TYPE_ 2 struct msgBuf //定義一個消息結(jié)構(gòu)體 { long mtype; //消息類型 char mtext[_BLOCK_SIZE_]; }; static int creat_msg_queue(); int get_msg_queue(); int send_msg_queue(int msg_id,const char*info,long type); int recv_msg_queue(int msg_id,char info[],long type); int destroy_msg_queue(int msg_id); //comm.c #include"comm.h" int get_msg_queue() { return creat_msg_queue(); } static int creat_msg_queue() { key_t key=ftok(_PATH_,_PROJ_ID_); if(key<0) { perror("ftok"); return -1; } int msg_id=msgget(key,IPC_CREAT); if(msg_id<0) { perror("msgget"); return -1; } return msg_id; } int send_msg_queue(int msg_id,const char*info,long type)//將要發(fā)送的消息存入mtext中 { struct msgBuf msg; msg.mtype=type; memset(msg.mtext,'\0',sizeof(msg.mtext)); strcpy(msg.mtext,info); if(msgsnd(msg_id,&msg,sizeof(msg.mtext),0)<0) { perror("msgsnd"); return -1; } return 0; } int recv_msg_queue(int msg_id,char* info,long type)//將mtext中的消息拿出放入info中 { struct msgBuf msg; if(msgrcv(msg_id,&msg,sizeof(msg.mtext),type,0)<0) { perror("msgrcv"); return -1; } strcpy(info,msg.mtext); return 0; } int destroy_msg_queue(int msg_id) { if(msgctl(msg_id,IPC_RMID,NULL)<0) { perror("msgctl"); return -1; } return 0; } //server.c 先發(fā)送后接收 #include"comm.h" int main() { int msgid=get_msg_queue(); if(msgid<0) { exit(1); } char info[_BLOCK_SIZE_]; while(1) { memset(info,'\0',sizeof(info)); printf("please input:"); fflush(stdout); gets(info); if(send_msg_queue(msgid,info,_SERVER_TYPE_)<0) { printf("send information failed\n"); exit(1); } if(recv_msg_queue(msgid,info,_CLIENT_TYPE_)<0) { printf("recieve information failed\n"); exit(1); } printf("client:%s\n",info); } destroy(msgid); return 0; } //client.c 先接收后發(fā)送 #include"comm.h" int main() { int msgid=get_msg_queue(); if(msgid<0) { exit(1); } char info[_BLOCK_SIZE_]; memset(info,'\0',sizeof(info)); printf("when input stop endding...\n"); while(1) { if(recv_msg_queue(msgid,info,_SERVER_TYPE_)<0) { printf("recieve information failed\n"); exit(1); } else { if(strcmp("stop",info)==0) { return 0; } printf("server :%s\n",info); } printf("please input:"); fflush(stdout); gets(info); if(send_msg_queue(msgid,info,_CLIENT_TYPE_)<0) { printf("send information failed\n"); exit(1); } } destroy(msgid); return 0; }
運行結(jié)果:
新聞標題:進程間通信:消息隊列的實現(xiàn)
標題網(wǎng)址:http://sd-ha.com/article46/gjjshg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計公司、關(guān)鍵詞優(yōu)化、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)