Detailed description of Linux interprocess communication-Message Queuing

Source: Internet
Author: User
Tags message queue

Preamble: The process of communication between signals and pipelines is discussed, and Message Queuing is discussed next.

First, the System V IPC

Three systems V IPC: There are many similarities between Message Queuing, semaphores, and shared memory (shared memory).

  The I P C structure (message queue, semaphore, or shared bucket) in each core uses an identifier of a nonnegative integer
(i d e n t i f i e R) be referenced.

  Whenever you create an I P-C structure (call m S G G e t, s E m g e t or s h M g e t) , you should specify a keyword (k e y ), off

The above is a brief introduction to the IPC, which is useful for understanding the message queues, semaphores, and shared memory that are described next.

second, Message Queuing1. Introduction

  Message Queuing is a linked table of messages , stored in the kernel and identified by message queue identifiers. We will say that the news team is listed as
"Queue" with the identifier "queue I D". m S G G e T is used to create a new queue or open an existing queue. m S G S n d
Used to add new messages to the end of the queue. Each message contains a positive long integer type field, a non-negative length, and an actual
Data bytes (corresponding to length), all of which are passed to m s G S n d when the message is added to the queue. the M S G R C v is used to
The queue for fetching messages. We do not have to get messages in FIFO order or in the Type field of the message.

2. Function Introduction
    • Ftok function

#include <sys/types.h>
#include <sys/ipc.h>

key_t Ftok (const char *pathname, int proj_id);//"/home/linux", ' a '
Function: Generate a key (value)

    • Msgget function

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

int Msgget (key_t key, int msgflg);

Function: Create or get a Message Queuing object
Returns: The ID of the message queue object the same key gets the same object
Format: Msgget (Key,flag|mode);
Flag: can be 0 or ipc_creat (created if not present)
Mode: Same as file permissions

    • msgsnd function

int msgsnd (int msqid, const void *MSGP, size_t msgsz, int msgflg);
Function: Writes MSGP message to message queue identified as MsgId
MSGP:
struct MSGBUF {
Long Mtype; /* Message type, must be > 0 * * The type of message must be >0
Char mtext[1]; /* Message Data */length arbitrary
};

MSGSZ: The size of the message to be sent does not include the 4 bytes of the message type
MSGFLG: If it is 0 when the message team is listed as full MSGSND will block
If ipc_nowait is not blocked when the message queue is full, return immediately

Return value: Successful return ID failure return-1

    • MSGRCV function

ssize_t MSGRCV (int msqid, void *msgp, size_t msgsz, Long Msgtyp,
int MSGFLG);

Function: Receives a specified type of message from the message queue with identifier MsgId and stores it in MSGP to delete the message from the message queue
Msgtyp: 0 means that no matter what type you can receive
MSGP: The structure that holds the message
MSGSZ: The size of the message to receive does not contain the 4 bytes that the message type occupies
MSGFLG: If the 0 ID is waiting for a message without a specified type
If it's ipc_nowait, it means no waiting.

    • Msgctl function

int msgctl (int msqid, int cmd, struct msqid_ds *buf);
Msgctl (msgid,ipc_rmid,null);//Delete Message Queuing objects

Program 2-2 will simply demonstrate Message Queuing:

---snd.c---

#include"My.h"typedefstruct{    Longtype; Charname[ -]; intAge ;} MSG;intMain () {key_t key= Ftok ("/HOME/LIUDW",'6'); printf ("key:%x\n", key); intMsgId = Msgget (key,ipc_creat| o_wronly|0777); if(msgid<0) {perror ("Msgget error!"); Exit (-1);    } MSG m; Puts ("Please input your type name:"); scanf ("%ld%s%d",&m.type,m.name,&m.age); Msgsnd (MsgId,&m,sizeof(m)-sizeof(M.type),0); return 0;}

---rcv.c---

#include"My.h"typedefstruct{    Longtype; Charname[ -]; intAge ;} MSG;intMain () {key_t key= Ftok ("/HOME/LIUDW",'6'); printf ("key:%x\n", key); intMsgId =Msgget (key,o_rdonly); if(msgid<0) {perror ("Msgget error!"); Exit (-1);    } MSG Rcv; Longtype; Puts ("Please input type want!"); scanf ("%ld",&type); MSGRCV (MsgId,&AMP;RCV,sizeof(RCV)-sizeof(type), type,0); printf ("rcv--name:%s age:%d\n", Rcv.name,rcv.age);    Msgctl (Msgid,ipc_rmid,null); return 0;}

Run the Demo:

Third, detailed Ftok function
    • Ftok extracts the file information according to the path name, and then synthesizes the key based on the file information and project ID, which can be arbitrarily set.
    • The path must exist, and Ftok only takes a value based on the uniqueness of the file inode within the system, regardless of the permissions of the file.
    • PROJ_ID can be arbitrarily set according to their own conventions. This number, some call it project ID; On UNIX systems, it has a value of 1 to 255;

To verify the above view, modify the program 2-2 slightly and modify the path and proj_id as follows:

Program 3-1 is as follows:

---snd.c---

#include"My.h"typedefstruct{    Longtype; Charname[ -]; intAge ;} MSG;intMain () {key_t key= Ftok (" /Home",'a'); printf ("key:%x\n", key); intMsgId = Msgget (key,ipc_creat| o_wronly|0777); if(msgid<0) {perror ("Msgget error!"); Exit (-1);    } MSG m; Puts ("Please input your type name:"); scanf ("%ld%s%d",&m.type,m.name,&m.age); Msgsnd (MsgId,&m,sizeof(m)-sizeof(M.type),0); return 0;}

---rcv.c---

#include"My.h"typedefstruct{    Longtype; Charname[ -]; intAge ;} MSG;intMain () {key_t key= Ftok (" /Home",'a'); printf ("key:%x\n", key); intMsgId =Msgget (key,o_rdonly); if(msgid<0) {perror ("Msgget error!"); Exit (-1);    } MSG Rcv; Longtype; Puts ("Please input type want!"); scanf ("%ld",&type); MSGRCV (MsgId,&AMP;RCV,sizeof(RCV)-sizeof(type), type,0); printf ("rcv--name:%s age:%d\n", Rcv.name,rcv.age);    Msgctl (Msgid,ipc_rmid,null); return 0;}

Run a demo such as:

 

  Summary: mainly describes the process of communication between the message queue, there are questions can leave a message, must be answered immediately.

Detailed description of 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.