欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

進(jìn)程間通信:消息隊(duì)列實(shí)現(xiàn)雙向通信-創(chuàng)新互聯(lián)

消息隊(duì)列:操作系統(tǒng)提供緩沖區(qū),提供了一種從一個(gè)進(jìn)程向另一個(gè)進(jìn)程發(fā)送一個(gè)數(shù)據(jù)塊的方法。消息隊(duì)列與管道不同的是,消息隊(duì)列是基于消息的,而管道是基于字節(jié)流的。

創(chuàng)新互聯(lián)建站專注于比如企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,電子商務(wù)商城網(wǎng)站建設(shè)。比如網(wǎng)站建設(shè)公司,為比如等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)

查看系統(tǒng)消息隊(duì)列命令:ipcs -q

刪除消息隊(duì)列命令:ipcrm -q 消息id號(hào)

相關(guān)函數(shù):

原型: 產(chǎn)生消息隊(duì)列:int msgget(key_t key, int msgflg);

發(fā)送消息: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);

設(shè)置消息隊(duì)列屬性原型:int msgctl ( int msgqid, int cmd, struct msqid_ds *buf );

參數(shù):系統(tǒng)定義了 3 種 cmd 操作: IPC_STAT , IPC_SET , IPC_RMID

IPC_STAT : 該命令用來獲取消息隊(duì)列對應(yīng)的 msqid_ds 數(shù)據(jù)結(jié)構(gòu),并將其保存到 buf 指定的地址空間。
IPC_SET : 該命令來設(shè)置消息隊(duì)列的屬性,要設(shè)置的屬性存儲(chǔ)在buf中。
IPC_RMID : 從內(nèi)核中刪除 msqid 標(biāo)識(shí)的消息隊(duì)列。

  //comm.h
  1 #pragma once
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<string.h>
  5 #include<sys/types.h>
  6 #include<unistd.h>
  7 #include<sys/ipc.h>
  8 #include<sys/msg.h>
  9 #define _PATH_ "."
 10 #define _PROJ_ID_ 0x777
 11 #define _BLOCK_SIZE_ 1024
 12 #define _SERVER_MSG_TYPE_ 1
 13 #define _CLIENT_MSG_TYPE_ 2
 14 struct msgbuf
 15 {
 16     long mtype;
 17     char mtext[_BLOCK_SIZE_];
 18 };
 19 static int comm_msg_queue(int flag);
 20 int set_msg_queue();
 21 int get_msg_queue();
 22 int msg_queue_send(int msg_id,const char* msg,long type);
 23 int msg_queue_recv(int msg_id,char* msg,long type);
 24  int destory_msg_queue(int msgId);
 //comm.c
  1 #include"comm.h"
  2 static int comm_msg_queue(int flag)
  3 {
  4 
  5     key_t _key=ftok(_PATH_,_PROJ_ID_);
  6     if(_key<0)
  7     {
  8         perror("ftok");
  9         return -1;
 10     }   
 11     int msg_id=msgget(_key,flag);
 12     if(msg_id<0)
 13     {
 14         perror("msgget");
 15         return -1;
 16     }   
 17     return msg_id;
 18 }   
 19 int set_msg_queue()
 20 {
 21     umask(0);
 22     return comm_msg_queue(IPC_CREAT|IPC_EXCL|0666);
 23 }   
 24 int get_msg_queue()
 25 {
 26     return comm_msg_queue(IPC_CREAT);
 27 }   
 28 int msg_queue_send(int msg_id,const char* message,long type)
 29 {
 30     struct msgbuf msg;
 31     msg.mtype=type;
 32     strcpy(msg.mtext,message);
 33     if(msgsnd(msg_id,&msg,strlen(msg.mtext),0)<0)
 34     {
 35         perror("msgsnd");
 36         return -1;
 37     }   
 38     return 0;
 39  }
 40 int msg_queue_recv(int msg_id,char* msg,long type)
 41 {
 42     struct msgbuf ret;
 43     memset(ret.mtext,'\0',_BLOCK_SIZE_);
 44     if(msgrcv(msg_id,&ret,_BLOCK_SIZE_-1,type,0)<0)
 45     {
 46         perror("msgrcv");
 47         return -1;
 48     }
 49     strcpy(msg,ret.mtext);
 50     return 0;
 51 }
 52 int destory_msg_queue(int msg_id)
 53 {
 54     if(msgctl(msg_id,IPC_RMID,NULL)<0)
 55     {
 56         perror("msgctl");
 57         return -1;
 58     }
 59     else
 60     {
 61         printf("remove msg_queue\n");
 62         return 0;
 63     }
 64 }
 //server.c
  1 #include"comm.h"
  2 int main()
  3 {
  4     int msgid=set_msg_queue();
  5     if(msgid<0)
  6     {
  7         exit(1);
  8     }
  9     char buf[_BLOCK_SIZE_];
 10     printf("input quit endding..\n");
 11     while(1)
 12     {
 13         if(msg_queue_recv(msgid,buf,_CLIENT_MSG_TYPE_)<0)
 14         {
 15             printf("recv fail\n");
 16             exit(1);
 17         }
 18         else
 19         {
 20             if(strcmp("quit",buf)==0)
 21                 return 0;
 22             printf("client:%s\n",buf);
 23         }
 24         printf("input:");
 25         fflush(stdout);
 26         memset(buf,'\0',_BLOCK_SIZE_);
 27         gets(buf);
 28         if(msg_queue_send(msgid,buf,_SERVER_MSG_TYPE_)<0)
 29         {
 30             printf("send fail\n");
 31             exit(1);
 32         }
 33     }
 34     destroy(msgid);
 35     return 0;
 36 }
 //client.c
  1 #include"comm.h"
  2 int main()
  3 {
  4     int msgid=get_msg_queue();
  5     if(msgid<0)
  6     {
  7         exit(1);
  8     }
  9     char buf[_BLOCK_SIZE_];
 10     while(1)
 11     {
 12         fflush(stdout);
 13         printf("please input:");
 14         memset(buf,'\0',_BLOCK_SIZE_);
 15         gets(buf);
 16         if(msg_queue_send(msgid,buf,_CLIENT_MSG_TYPE_)<0)
 17         {
 18             printf("send fail\n");
 19             exit(1);
 20         }
 21         if(msg_queue_recv(msgid,buf,_SERVER_MSG_TYPE_)<0)
 22         {
 23             printf("recv fail\n");
 24             exit(1);
 25         }
 26         printf("server:%s\n",buf);
 27     }
 28     return 0;
 29 }
 //Makefile的編寫
  1 .PHONY:all
  2 all:server client
  3 server:server.c comm.c
  4     gcc -o $@ $^
  5 client:client.c comm.c
  6     gcc -o $@ $^
  7 .PHONY:clean
  8 clean:
  9     rm -f server client

運(yùn)行結(jié)果:

進(jìn)程間通信:消息隊(duì)列實(shí)現(xiàn)雙向通信

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。

網(wǎng)站欄目:進(jìn)程間通信:消息隊(duì)列實(shí)現(xiàn)雙向通信-創(chuàng)新互聯(lián)
文章URL:http://www.chinadenli.net/article48/dhcphp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站網(wǎng)站排名動(dòng)態(tài)網(wǎng)站網(wǎng)頁設(shè)計(jì)公司靜態(tài)網(wǎng)站關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名