Message Queuing for interprocess communication

Source: Internet
Author: User
Tags function prototype message queue

I. Understanding of Message Queuing

Message Queuing provides a way to send a block of data from one process to another. Avoid synchronization and blocking problems for named pipes by sending messages.

Message Queuing differs from a pipeline in that Message Queuing is message-based and the pipeline is byte-stream based, and that Message Queuing reads are not necessarily first-in-first-out.

Message Queuing has the same disadvantage as named pipes: the maximum length of each message is capped (Msgmax), the total number of bytes per message queue is capped (MSGMNB), and the total number of message queues on the system is also capped (Msgmni).

Two. Related functions

(1) Create a new message queue or get an existing message queue.

Function prototype: int msgget (key_t key,int MSGFLG);

Parameters:

Key: Can be considered as a port number, can also be generated by the function Ftok;

MSGFLG:

Ipc_creat: If the IPC does not exist, create an IPC resource, otherwise open the operation. When Ipc_creat is used alone, the Xxxget () function either returns an operator of the shared memory that already exists, or returns an identifier for the newly created shared memory.

IPC_EXCL: The new shared memory is established only if the shared memory does not exist, or an error occurs. The IPC_EXCL flag itself does not make much sense, but working with the IPC_CREAT flag guarantees that the resulting object is new, rather than opening an existing object.

If the ipc_creat and IPC_EXCL flags are used together, the Xxxget () function returns a new IPC identifier and returns 1 if the IPC resource already exists.

(2) Read or write messages to the queue

function prototypes for fetching messages from queues: ssize_t msgrcv (int msqid,void *msgp,size_t MSGSZ,

Long Msgtyp,int MSGFLG);

function prototypes that put data into Message Queuing: int msgsnd (int msgid,const void *msgp,size_t Msgsz,

int MSGFLG);

Parameters:

MSQID: Message Queue identification code

MSGP: A pointer to the message buffer that is used to temporarily store the sent and received messages, is a user-definable generic structure, in the following form:

struct msgstru{

Long Mtype;

Char mtext[user-specified size];

};

MSGSZ: The size of the message.

Magtyp: The message pattern read from the message queue. A value of 0 indicates that all messages in the message queue will be read.

MSGFLG: Used to indicate the action that the core program should take if the queue has no data.

(3) Setting Message Queuing properties

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

Parameters: The MSGCTL system invokes a CMD operation on the message queue of the MSGQID flag, and the system defines three CMD operations:

Ipc_stat,ipc_set,ipc_rmid

Ipc_stat: Used to get the MSQID_DS data structure corresponding to the message queue and save it in the BUF specified address space.

Ipc_set: Used to set the properties of Message Queuing, the properties to be set are stored in BUF.

Ipc_rmid: The message queue that removes the MSQID flag from the kernel.

Three. Code

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_ "."

Ten #define _PROJ_ID_ 0x4444

#define _BLOCK_SIZE_ 1024

#define _SERVER_MSG_TYPE_ 1

#define _CLIENT_MSG_TYPE_ 2

msgbuf struct

15 {

Mtype long;

+ Char Mtext[_block_size_];

18};

static int comm_msg_queue (int flag);

int Set_msg_queue ();

int Get_msg_queue ();

int msg_queue_send (int msg_id,const char* _info,int _mtype);

MSG_QUEUE_RECV int (int msg_id,char* buf,int recv_type);

destroy_msg_queue int (int msg_id);


Comm.c

1 #include "comm.h"

2

3 static int comm_msg_queue (int flag)

4 {

5 key_t _key=ftok (_PATH_,_PROJ_ID_);

6 if (_key<0)

7 {

8 perror ("Ftok");

9 return-1;

10}

one int msg_id=msgget (_key,flag);

if (msg_id<0)

13 {

Perror ("Msgget");

return-1;

16}

msg_id return;

18}

int Set_msg_queue ()

20 {

Umask (0);

Return Comm_msg_queue (ipc_creat| ipc_excl|0666);

23}

Get_msg_queue Int ()

25 {

Comm_msg_queue return (ipc_creat);

27}

msg_queue_send Int (int msg_id,const char *_info,int _mtype)

29 {

the struct msgbuf _msg;

_msg.mtype=_mtype;

memset (_msg.mtext, ' n ', sizeof (_msg.mtext));

strcpy (_msg.mtext,_info);

if (Msgsnd (msg_id,&_msg,sizeof (_msg.mtext), 0) <0)

35 {

Perror ("msgsnd");

Panax Notoginseng return-1;

38}

0;

40}

MSG_QUEUE_RECV int (int msg_id,char* buf,int recv_type)

42 {

Msgbuf _msg of the struct;

if (MSGRCV (msg_id,&_msg,sizeof (_msg.mtext), recv_type,0) <0)

45 {

Perror ("MSGRCV");

return-1;

48}

strcpy (Buf,_msg.mtext);

return 0;

51}

destroy_msg_queue int (int msg_id)

53 {

if (Msgctl (msg_id,ipc_rmid,null) <0)

55 {

Perror ("Msgctl");

return-1;

58}

+ Else

60 {

done...\n printf ("Remove msg queue");

62}

return 0;

64}


My_server.c

1 #include "comm.h"

2 int Main ()

3 {

4 int msgid=set_msg_queue ();

5 if (msgid<0)

6 {

7 printf ("Get MsgId fail\n");

8 exit (1);

9}

Ten char buf[_block_size_];

printf ("Intput quit endding...\n");

while (1)

13 {

if (MSG_QUEUE_RECV (msgid,buf,_client_msg_type_) <0)

15 {

printf ("recv fail\n");

+ exit (1);

18}

Else

20 {

if (strcmp ("Quit", buf) ==0)

22 {

return 0;

24}

printf ("client:%s\n", buf);

26}

printf ("Input:");

Fflush (stdout);

memset (buf, ' _block_size_ ');

(BUF);

if (Msg_queue_send (msgid,buf,_server_msg_type_) <0)

32 {

printf ("Send fail\n");

Exit (1);

35}

36}

Panax Notoginseng//Destroy_msg_queue (MSGID);

return 0;

39

40}


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_];

Ten while (1)

11 {

Fflush (stdout);

printf ("Please input:");

memset (buf, ' _block_size_ ');

(BUF);

if (Msg_queue_send (msgid,buf,_client_msg_type_) <0)

17 {

printf ("Send fail\n");

Exit (1);

20}

if (MSG_QUEUE_RECV (msgid,buf,_server_msg_type_) <0)

22 {

printf ("recv fail\n");

Exit (1);

25}

printf ("sever:%s\n", buf);

27}

Destroy_msg_queue (MsgId);

return 0;

30}


Makefile

1. Phony:all

2 All:my_server Client

3 My_server:my_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 My_Server Client


Four. Running results

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/25/wKiom1cU2nSzXAB3AABaqOYyYWA210.png "title=" Queue.png "alt=" Wkiom1cu2nszxab3aabaqoyyywa210.png "/>



This article is from the "Zwy" blog, make sure to keep this source http://10548195.blog.51cto.com/10538195/1765163

Message Queuing for interprocess communication

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.