UNIX Environment Advanced Programming: System V Message Queuing

Source: Internet
Author: User
Tags message queue

It is far from enough that the signal in the early communication mechanism of UNIX can transmit a limited amount of information and the pipeline can only transmit the unformatted byte stream.

Message Queuing (also known as Message Queuing) customer service These disadvantages:

Message Queuing is a linked list of messages.

You can think of a message as a record with a specific format.

Processes can add new messages to message queues according to certain rules, and other processes can read messages from message queues.

Message Queuing is persisted with the kernel and is deleted only if the kernel reboots or is manually deleted.

System V Message Queuing uses the Message Queuing identifier identifier. Any process with sufficient privileges can place a message on a given queue, and any process with sufficient privileges can read a message from a given queue.

Message queue has some FIFO characteristics, but it can realize random query of message, which has more advantages than FIFO. At the same time, these messages exist in the kernel, and the implementation of the message queue identified by the queue ID includes creating or opening message queues, adding messages, reading messages, and controlling Message Queuing four operations.

For each message queue in the system, the kernel maintains an information structure defined in the <sys/msg.h> header file.

struct Msqid_ds {  
    struct ipc_perm msg_perm;  
    struct MSG *msg_first;      /* queue,unused
    /struct msg *msg_last;       /* Last Message in queue,unused * *
    __kernel_time_t msg_stime;  /* Last msgsnd time * *
    __kernel_time_t msg_rtime;  /* Last MSGRCV time * *
    __kernel_time_t msg_ctime;  /* Last change time
    /unsigned long  msg_lcbytes/* Reuse junk fields for bit/
    unsigned long  msg _lqbytes; /* Ditto *
    unsigned short msg_cbytes;  /* Current number of bytes on queue *
    /unsigned short msg_qnum;    /* Number of messages in queue *
    /unsigned short msg_qbytes;  /* max number of bytes on queue *
    /__kernel_ipc_pid_t msg_lspid;   /* PID of last msgsnd * *
    __kernel_ipc_pid_t msg_lrpid;   /* Last receive PID *
/};

We can draw a specific message queue in the kernel as a message list, as shown in a queue with three messages, message lengths of 1 bytes, 2 bytes, and 3 bytes, and these messages are written to the queue in this order. Let's assume that the three messages are of type 100,200,300 respectively.

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.