Linux IPC Practice (6)--system v Message Queuing (3)

Source: Internet
Author: User

Message Queuing synthesis case

Message Queuing implementation back-up client/server

 

Server when the process receives ,  Specify Msgtyp to be 0, keep receiving messages from the head of the team

When the server process is sent, Mtype is specified as the PID of the client process received

When the client process is sent, the mtype is specified as the PID of its own process

When the client process receives, it needs to designate Msgtyp as the PID of its own process, receiving only messages with the message type of its own PID;

Client/server process received/sent data structure const int Msgmax = 8192;struct msgbuf{    long mtype;         Save the PID of the client process (need to cast the PID into long)    char Mtext[msgmax];//Save the actual data sent by the client process};
Client.cppvoid echoserver (int msgid) {    struct msgbuf buf;    int NRCV;    while (true)    {        bzero (&buf, sizeof (BUF));        if (NRCV = MSGRCV (MsgId, &buf, sizeof (Buf.mtext), 0, 0)) = =-1)            err_exit ("MSGRCV error");        cout << "recv:" << buf.mtext;        if (msgsnd (MsgId, &buf, strlen (Buf.mtext), 0) = =-1)            err_exit ("msgsnd error");}    } int main () {    key_t key = Ftok ("/tmp/echoseed", 0x1234);    int msgid = Msgget (key, ipc_creat|0666);    if (MsgId = =-1)        err_exit ("Msgget error");    Echoserver (MsgId);}

Attached-ftok usage

#include <sys/types.h> #include <sys/ipc.h>key_t ftok (const char *pathname, int proj_id);

Description information:

The Ftok () function uses the identity (symbol) of the file named by the given pathname (which must refer

To an existing, accessible file[must be an already existing and accessible file]) and the least significant (valid) 8 bits[valid minimum 8 bits] of proj_i D (which must is nonzero) to generate a key_t type System V IPC key, suitable

For use with Msgget (2), Semget (2), or Shmget (2). The resulting value is the same for all pathnames that name the same file, when the same value of proj_id

is used (if the file name is the same as the valid bits of PROJ_ID, the generated key must be the same). The value returned should is different when

The (simultaneously existing) files or the project IDs differ.

RETURN VALUE  On success, the generated key_t value is returned. On Failure-1 is returned,

With errno indicating the "error as for the stat" (2) system call.

Linux IPC Practice (6)--system v Message Queuing (3)

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.