Message Queuing: The operating system provides a buffer that provides a way to send a block of data from one process to another. Message Queuing differs from a pipeline in that Message Queuing is message-based and the pipeline is byte-stream based.
View System Message Queuing command: IPCS-Q
Delete Message Queue command: Ipcrm-q message ID number
Related functions:
Prototype: Generate Message queue:int msgget (key_t key, int msgflg);
Send message: int msgsnd (int msqid, const void *MSGP, size_t msgsz, int msgflg);
Receive message: ssize_t MSGRCV (int msqid, void *msgp, size_t msgsz, Long msgtyp,int MSGFLG);
Set Message Queuing Properties prototype:int msgctl (int msgqid, int cmd, struct msqid_ds *buf);
Parameters: The system defines 3 cmd operations : Ipc_stat, Ipc_set, Ipc_rmid
Ipc_stat:This command is used to obtain a message queue corresponding to theMsqid_dsData structure and save it to the address space defined by BUF.
Ipc_set: This command sets the properties of the message queue, and the properties to be set are stored in buf .
ipc_rmid: removes the msqid identity message queue from the kernel.
//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); //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 &nbSp; perror ("msgsnd"); 36 return -1; 37 } 38 return 0; //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); &nbsP;26 memset (buf, ' _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, ' n ', _block_size_); 15 gets ( BUF); 16 if (Msg_queue_send (msgid,buf,_CLIENT_MSG_ TYPE_) <0) 17 { 18 printf ("send fail\n"); 19 &Nbsp; 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 writing 1 . Phony:all 2 all:server client 3 server:server.c comm.c 4 gcc -o [email protected] $^ 5 client: Client.c comm.c 6 gcc -o [email protected] $^ 7 . phony:clean 8 clean: 9 rm -f server Client
Operation Result:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/D7/wKiom1cKC0-hcvITAAB4DDppdKs102.png "title=" 10.png "alt=" Wkiom1ckc0-hcvitaab4ddppdks102.png "/>
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1762337
interprocess communication: Message Queuing implements two-way communication