Linux Message Queue practices (1) and linux Message Queue practices

Source: Internet
Author: User

Linux Message Queue practices (1) and linux Message Queue practices
Basic concepts of Message Queue

Message Queue provides a method to send a piece of data from one process to another (limited to local)

Each data block is considered to be of a type. The data block received by the recipient process may have different types of values.

Message queues have the same limitations as queues, that is, the maximum length of each message is limited (MSGMAX), and the total number of bytes of each message queue is limited (MSGMNB ), the total number of message queues on the system also has an upper limit (MSGMNI)

 

MPs queue vs. Message Queue:

MPs queue message: boundary

First-in-first-out

 

Three restrictions on Message Queue size

Cat/proc/sys/kernel/msgmax # maximum message length limit

Cat/proc/sys/kernel/msgmnb # Total Message Queue bytes

Cat/proc/sys/kernel/msgmni # Number of message entries

 

 

IPC Object Data Structure

The kernel maintains a data structure for each IPC object.

[Cpp]View plaincopy
  1. Struct ipc_perm
  2. {
  3. Key_t _ key;/* Key supplied to msgget (2 )*/
  4. Uid_t uid;/* Valid tive UID of owner */
  5. Gid_t gid;/* Valid tive GID of owner */
  6. Uid_t cuid;/* Valid valid UID of creator */
  7. Gid_t cgid;/* Valid tive GID of creator */
  8. Unsigned short mode;/* Permissions */
  9. Unsigned short _ seq;/* Sequence number */
  10. };

 

Message Queue-specific structure

[Cpp]View plaincopy
  1. Struct msqid_ds
  2. {
  3. Struct ipc_perm msg_perm;/* Ownership and permissions */
  4. Time_t msg_stime;/* Time of last msgsnd (2 )*/
  5. Time_t msg_rtime;/* Time of last msgrcv (2 )*/
  6. Time_t msg_ctime;/* Time of last change */
  7. Unsigned long _ msg_cbytes;/* Current number of bytes in
  8. Queue (nonstandard )*/
  9. Msgqnum_t msg_qnum;/* Current number of messages
  10. In queue */
  11. Msglen_t msg_qbytes;/* Maximum number of bytes
  12. Allowed in queue */
  13. Pid_t msg_lspid;/* PID of last msgsnd (2 )*/
  14. Pid_t msg_lrpid;/* PID of last msgrcv (2 )*/
  15. };

 

Message Queue representation in the kernel


Message Queue function example

Msgget Function 

Function: Creates and accesses a message queue.

Prototype:

[Cpp]View plaincopy
  1. Int msgget (key_t key, int msgflg );

Parameters:

Key: The name of a message queue.

Msgflg: consists of nine permission tags, such as 0644. Their usage is the same as the mode flag used when creating a file (but the message queue does not have the x (execution) Permission)

 

Return Value:

A Message Queue ID is returned, that is, the ID code of the message queue.

 

Programming practices

[Cpp]View plaincopy
  1. // Practice 1: IPC_PRIVATE: Macro; Value: 0
  2. Int main ()
  3. {
  4. // IPC_PRIVATE the Message Queue descriptor created each time is different!
  5. // Therefore, the timestamp MessageID (key) is sent to other processes (unless there is an associated process ),
  6. // Other processes cannot use this message queue (except for lineage fork)
  7. Int msgid = msgget (IPC_PRIVATE, 0666 );
  8. If (msgid <0)
  9. {
  10. // If the message queue does not exist, an error is displayed. errno = ENOENT
  11. If (errno = ENOENT)
  12. {
  13. Cout <"ENOENT" <endl;
  14. }
  15. Err_exit ("mesget error ");
  16. }
  17. Else
  18. {
  19. Cout <"msgid =" <msgid <endl;
  20. }
  21. Return 0;
  22. }
  23. /**
  24. The message queue created by IPC_PRIVATE can only be used in processes related to the current process!
  25. */


[Cpp]View plaincopy
  1. // Practice 2: IPC_CREAT
  2. Int main ()
  3. {
  4. // If IPC_CREAT is specified, a message queue is created.
  5. Int msgid = msgget (0x1235,066 6 | IPC_CREAT );
  6. If (msgid <0)
  7. {
  8. // If the message queue does not exist, an error is displayed. errno = ENOENT
  9. If (errno = ENOENT)
  10. {
  11. Cout <"ENOENT" <endl;
  12. }
  13. Err_exit ("mesget error ");
  14. }
  15. Else
  16. {
  17. Cout <"msgid =" <msgid <endl;
  18. }
  19. Return 0;
  20. }


[Cpp]View plaincopy
  1. // Practice 3: IPC_CREAT | IPC_EXCL
  2. Int main ()
  3. {
  4. // Specify IPC_EXCL. If IPC_EXCL already exists, the report file already exists (error)
  5. Int msgid = msgget (0x1235,066 6 | IPC_CREAT | IPC_EXCL );
  6. If (msgid <0)
  7. {
  8. Err_exit ("mesget error ");
  9. }
  10. Else
  11. {
  12. Cout <"msgid =" <msgid <endl;
  13. }
  14. Return 0;
  15. }




[Cpp]View plaincopy
  1. // Practice 4: Create with low permissions and enable with high Permissions
  2. Int main ()
  3. {
  4. // Create with low Permissions
  5. Int msgid = msgget (0x255, 0444 | IPC_CREAT );
  6. If (msgid <0)
  7. {
  8. Err_exit ("mesget error ");
  9. }
  10. Else
  11. {
  12. Cout <"Create Mes OK, msgid =" <msgid <endl;
  13. }
  14. // Open with high Permissions
  15. Msgid = msgget (0x255, 0644 | IPC_CREAT );
  16. If (msgid <0)
  17. {
  18. Err_exit ("mesget error ");
  19. }
  20. Else
  21. {
  22. Cout <"Create Mes OK, msgid =" <msgid <endl;
  23. }
  24. Return 0;
  25. }


Msgget function parameter Relationship Diagram

 

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.