Linux --處理序間通訊--訊息佇列

來源:互聯網
上載者:User
一、訊息佇列的定義
訊息佇列能夠彌補管道的不足,實現雙向互動資料,是一個進程向另一進程發送進程塊的方法。與管道不同的是,管道是基於位元組流的,訊息佇列是基於訊息的,且訊息佇列的讀取不一定是先進先出。

二、訊息佇列的建立
通過函數int messget(key_t key,int msgflg);建立

key:連接埠號碼,可以有 ftok產生。
msgflg:
IPC_CRTAT 若果 IPC不存在,則建立一個IPC資源,
IPC_EXCL:一般和 IPC_CREAT一起使用可以保證所得的對象是建立的,而不是開啟已有的。
三、向訊息佇列讀資料
msgrcv從隊列中取的訊息

ssize_t msgrcv(int msqid,void *msgp,size_t msgsz,long msgtyp,int msgflg);

msgsnd將訊息放進隊列中

int msgsnd(int msqid,const void *msgp,size_t msgsz,int msgflg);

四、設定訊息佇列屬性
int msgctl(int msgqid,int cmd,struct msqid_ds *buf);

*** 可以用man 函數名 查看函數***

程式碼:

coom.h

1 #pragma once    2 #include<stdio.h>    3 #include<stdlib.h>    4 #include<string.h>    5 #include<sys/types.h>    6 #include<sys/ipc.h>    7 #include<sys/msg.h>        8 #define _PATH_ "."    9 #define _PROCID_ 0x776   10 #define _SIZE 1024   11 #define SERVE_MSGTYPE 1   12 #define CLIECT_MSGTYPE 2      13 typedef struct message   14 {   15   long mtype;   16   char mtext[_SIZE];   17 }message;   18 int create_msg(int msg_num);   19 int drstroy_msg(int msg_id);   20 int set_msg();   21 int get_msg();   22 int msg_send(int msg_id,const char* msg,long type);   23 int msg_rec(int msg_id ,char buf[],long type);

comm .c

1 #include"comm.h"    2  int create_msg(int msg_num)    3 {    4   key_t _key=ftok(_PATH_,_PROCID_);    5   if(_key < 0)    6   {    7     perror("ftok");    8     return -1;    9   }   10   int ret= msgget(_key,msg_num);   11    if(ret < 0)   12    {   13     perror("msgget");   14     return -1;   15    }   16  return ret;   17 }   18 int drstroy_msg(int msg_id)   19 {   20   if(msgctl(msg_id,IPC_RMID,NULL) < 0)   21   {   22     perror("msgctl");   23     return -1;   24   }else{   25     printf("remove message_queue\n");   26     return 0;   27   }   28 }   29 int set_msg()   30 {   31   umask(0);   32   return create_msg(IPC_CREAT |IPC_EXCL |0666);   33 }   34 int get_msg()   35 {   36     return create_msg(IPC_CREAT);   37 }   38 int msg_send(int msg_id,const char* msg,long type)   39 {   40   struct message _msg;   41   _msg.mtype=type;   42   strcpy(_msg.mtext,msg);   43   if(msgsnd(msg_id,&_msg,strlen(_msg.mtext),0)< 0)   44   {   45     perror("msg_send error");   46     return -1;   47   }   48   return 0;   49 }   50 int msg_rec(int msg_id,char buf[],long type)   51 {   52   struct message _msg;   53   memset(_msg.mtext,'\0',_SIZE);   54   if(msgrcv(msg_id,&_msg,_SIZE,type,0)< 0)   55   {   56     perror("msg_receve error");   57     return -1;   58   }   59   strcpy(buf,_msg.mtext);   60   return 0;   61 }   62

client.c

1 #include"comm.h"    2 int main()    3 {    4  int msg_id=get_msg();    5  if(msg_id<0)    6  {    7   perror("error");    8   exit(1);    9  }   10   char buf[_SIZE];   11  while(1)   12  {   13   fflush(stdout);   14   printf("please client input: ");   15   memset(buf,'\0',_SIZE);   16   fgets (buf,sizeof(buf),stdin);   17   if(msg_send(msg_id,buf,CLIECT_MSGTYPE)< 0)   18     {   19       perror("send fail");   20       exit(1);   21     }   22    23   if(msg_rec(msg_id,buf,SERVE_MSGTYPE)<0)   24     {   25       perror("recve fail");   26       exit(1);   27     }   28   printf("serve:%s" ,buf);   29  }   30   return 0;   31 }

server.c

1 #include "comm.h"    2 int main()    3 {    4   int msg_id=set_msg();    5   if(msg_id<0)    6   {    7     perror("mig_id");    8     exit(1);    9   }   10   printf("%d\n",msg_id);   11   char buf[_SIZE];   12   while(1)   13   {   14      if(msg_rec(msg_id,buf,CLIECT_MSGTYPE)< 0)   15     {   16       perror("recve fail");   17       exit(1);   18     }   19     else   20     {   21       printf("client :%s",buf);   22     }   23     printf("server please input: ");   24     fflush(stdout);   25     memset(buf,'\0',_SIZE);   26     fgets(buf,_SIZE,stdin);   27     if(msg_send(msg_id,buf,SERVE_MSGTYPE)<0)   28     {   29       perror("send fail");   30       exit(1);   31     }   32   }   33   drstroy_msg(msg_id);   34   return 0;   35 }

運行結果:

以上就是Linux --處理序間通訊--訊息佇列的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.