Linux-interprocess communication-Message Queuing

Source: Internet
Author: User
Tags message queue stdin

I. Definition of message queue

Message Queuing can compensate for the lack of pipelines and implement bidirectional interaction data, which is a way for a process to send a process block to another process. Unlike pipelines, pipelines are based on byte streams, Message Queuing is message-based, and Message Queuing reads are not necessarily FIFO.

Second, the creation of message queue

Through the function int messget (key_t key,int MSGFLG);

Key: Port number, can have ftok generation.

MSGFLG:

Ipc_crtat If the IPC does not exist, create an IPC resource,

IPC_EXCL: It is common to use with Ipc_creat to guarantee that the resulting object is new, not open.

Third, read the data to the message queue

MSGRCV messages taken from the queue

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

MSGSND putting messages in the queue

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

Iv. Setting Message Queuing properties

int msgctl (int msgqid,int cmd,struct msqid_ds *buf);

You can view functions with the man function name * * *

Program code:

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"); &nbsP;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, ' ', _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, ' _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 , ' _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 }

Operation Result:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/7E/ED/wKiom1cM4yyiutEVAAG4zXdr9gc424.jpg "title=" Message Queuing. JPG "alt=" Wkiom1cm4yyiutevaag4zxdr9gc424.jpg "/>

Linux-interprocess communication-Message Queuing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.