Message Queuing for interprocess communication

Source: Internet
Author: User
Tags message queue strcmp

I. Overview:

Message Queuing is a linked list of messages, stored in the kernel and flagged by message queue identifiers, providing a way to send chunks of data from one process to another (data block has type). We can avoid the synchronization and blocking problems of named pipes by sending messages.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/E0/wKiom1cLWVCDSIIoAAD0PkVwLqw447.png "title=" 2016-04-11 15:58:27 screen. png "alt=" Wkiom1clwvcdsiioaad0pkvwlqw447.png "/>

Note: (1). The most important are the first three and the last two.

(2). Message Queuing has the same insufficiency as a named pipe, that is, 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 has one by one caps (Msgmni).


Two. Related functions:

    1. int Msgget (ket_t ket, int msgflg): Creates a new message queue or opens a message queue that already exists.

Key: Can be thought of as a port number, or it can be created with the Ftok function (below).

MSGFLG:

Ipc_creat: If the IPC does not exist, create an IPC resource, or open it directly for operation.

IPC_EXCL: Shared memory is created only if shared memory does not exist, and an error occurs if shared memory exists.

Return value: If successful, the message queue identifier is returned, otherwise, 1 is returned.

Note: (1). If Ipc_creat and ipc_excl are used together: if the IPC resource does not exist, a new IPC resource is returned, or 1 if the IPC is already present.

(2). The IPC_EXCL alone is not very significant, it is generally used in conjunction with ipc_creat, to ensure that a new IPC resource is returned, rather than an existing one.

2. ssize_t MSGRCV (int msqid, void *msgp, size_t msgsz, long Msgtyp, int msgflg): Fetching messages from the message queue;

int msgsnd (int msqid, const void *MSGP, size_t msgsz, int msgflg): Sends data to the message queue.

MSQID: The identifier code for the message queue.

MSGP: A pointer to the message buffer, used to temporarily store the received and sent messages, is a user-definable data structure. The pattern is as follows:

struct MSGP

{

Long mytype;//greater than 0

Char mytest[user-specified size];

}

MSGSZ: The size of the message.

Msgtyp: The message pattern read from within the message queue. A value of 0 identifies all messages in the message queue to be read.

MSGFLG: Used to indicate the line action that the core program should take if the queue has no data. If MSGFLG and ipc_nowait are pooled, then if the message queue is full when the msgsnd () is executed, the MSGSND () will not block, and will immediately return 1 if MSGRCV () is executed, and if the message queue is empty, do not wait for the immediate return-1, and set the error code to enomsg. When MSGFLG is 0 o'clock, msgsnd () and MSGRCV () take a blocking wait processing mode when the queue is full or empty.

Return value: On failure both functions return-1 with errno indicating the error,otherwise msgsnd () returns 0 and MSGRCV () Retu RNs the number of bytes actually copied into the Mtext array.

3. int msgctl (int msgqid, int cmd, struct msqid_ds *buf): Sets the information queue property.

The MSGCTL system call performs a line-cmd operation on the message queue identified by the msgqid, and the system defines 3 cmd operations:
Ipc_stat: This command is used to get the MSQID_DS data structure corresponding to the message queue and save it to the BUF reference
Address space is fixed.
Ipc_set: This command is used to set the properties of Message Queuing, and the properties to be set are stored in BUF.
Ipc_rmid: Removes the MSQID identity message queue from the kernel.

4. key_t Ftok (const char* path, int proj_id): Converts an already existing pathname and an integer to a key_t value, called the IPC key. Successfully returns a key_t value, otherwise returns-1;


Three. Code that implements two-way communication with Message Queuing:


Com.h:

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define PATH "."
#define BUF_SIZE 1024
#define PROJ_ID 0x777//...???
#define SERVER_TYPE 1
#define CLIENT_TYPE 2

struct MSGBUF
{
Long MyType;
Char Mytest[buf_size];
};
static int msg_queue (int flag);//Get a message queue for this file
int Set_msg_queue ();//Set the interface for Message Queuing
int msg_queue_send (int msg_id, const char* MSG, long type);//interface for sending messages
int msg_queue_recv (int msg_id, char* buf, long type);//interface that accepts messages

Com.c

#include "com.h"

static int msg_queue (int flag)
{
key_t key = Ftok (path,proj_id);
if (Key < 0)
{
printf ("Ftok is error\n");
return-1;
}

int msg_id = Msgget (Key,flag);
if (msg_id < 0)
{
printf ("Msgget is error\n");
return-1;
}

return msg_id;
}

int Get_msg_queue ()
{
Return Msg_queue (ipc_creat);
}

int Set_msg_queue ()//set Read and write execution permissions for Message Queuing
{
Umask (0);
Return Msg_queue (Ipc_creat | Ipc_excl | 0666);
}

int msg_queue_send (int msg_id, const char* MSG, long type)
{
struct MSGBUF buf;
Buf.mytype = type;
strcpy (Buf.mytest, msg);
if (msgsnd (msg_id, &buf, strlen (buf.mytest), 0) < 0)
{
Perror ("msgsnd");
return-1;
}
return 0;
}

int msg_queue_recv (int msg_id, char *msg, long type)
{
int ret = 0;
struct MSGBUF buf;
Buf.mytype = type;
if (MSGRCV (msg_id, &buf, sizeof (Buf.mytest), 0, 0) < 0)
{
Perror ("MSGRCV");
return-1;
}

strcpy (msg, buf.mytest);//Copy the contents of the message into the MSG
return strlen (msg);
}

SERVER.C:

#include "com.h"

int main ()
{
int msg_id = Get_msg_queue ();
if (msg_id < 0)
{
printf ("Message Queue create failed\n");
return-1;
}

Char Buf[buf_size];
while (1)//server-side accepts messages before sending messages
{

memset (buf, ' n ', sizeof (BUF)-1);
int ret = MSG_QUEUE_RECV (msg_id, buf, Client_type);
if (Ret < 0)
{
printf ("Message recv failed\n");
return-1;
}
Else
{
if (strcmp (buf, "quit") = = = 0)
{
printf ("Client closed\n");
return 0;
}

Buf[ret] = ' + ';
printf ("Client say:%s\n", buf);
}

printf ("Please write yous words:");
Fflush (stdout);

memset (buf, ' n ', sizeof (BUF)-1);
Gets (BUF);
if (Msg_queue_send (msg_id, buf, Server_type) < 0)
{
printf ("Message send failed\n");
return-1;
}
}
Destory (msg_id);
return 0;
}


Client.c

#include "com.h"

int main ()
{
int msg_id = Get_msg_queue ();
if (msg_id < 0)
{
printf ("Message queue creat failed\n");
return-1;
}

Char Buf[buf_size];
while (1)
{
printf ("Please write your words:");
Fflush (stdout);

memset (buf, ' n ', sizeof (BUF)-1);
Gets (BUF);
if (Msg_queue_send (msg_id, buf, Client_type) < 0)
{
printf ("Message send failed\n");
return-1;
}

memset (buf, ' n ', sizeof (BUF)-1);
int ret = MSG_QUEUE_RECV (msg_id, buf, Server_type);
if (Ret < 0)
{
printf ("Message recv failed\n");
return-1;
}
Else
{
if (strcmp (buf, "quit") = = = 0)
{
printf ("Server closed!\n");
return 0;
}

Buf[ret] = ' + ';
printf ("Server say:%s\n", buf);
}
}
Destory (msg_id);
return 0;
}


Makefile:

. Phony:all
All:server Client
SERVER:SERVER.C COM.C
Gcc-o [email protected] $^
Client:client.c COM.C
Gcc-o [email protected] $^
. Phony:clean
Clean
Rm-f Server Client



Execution Result:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/E3/wKiom1cLsBmyphLJAABkkSZBQik314.png "title=" 2016-04-11 22:04:09 screen. png "alt=" Wkiom1clsbmyphljaabkkszbqik314.png "/>


Problem: When you enter quit, the cache buf will appear before the previous content, do not know why? (I memset before send, recv also memset).
















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.