Linux Message Queuing practices (1)

Source: Internet
Author: User
Tags message queue

Message Queuing Basic Concepts

Message Queuing provides a way to send a piece of data from one process to another (limited to native only)

Each block of data is considered to have a type, and the receiver process receives a data block that can have different type values

Message Queuing also has the same pipeline of shortcomings, that is, the maximum length of each message is capped (Msgmax), the total number of bytes per message queue is capped (MSGMNB), the total number of message queues on the system also has an upper limit (Msgmni)

Pipeline vs. Message Queuing:

Pipeline: Stream Pipeline message: bounded

Advanced first out can be entered later, first out

Message Queue Size Three limitations

Cat/proc/sys/kernel/msgmax #最大消息长度限制

CAT/PROC/SYS/KERNEL/MSGMNB #消息队列总的字节数

Cat/proc/sys/kernel/msgmni #消息条目数

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; / * Effective UID of owner * /   
  5. gid_t GID; / * Effective GID of owner * /   
  6. uid_t cuid; / * Effective UID of Creator * /   
  7. gid_t Cgid; / * Effective GID of Creator * /   
  8. unsigned short mode; / * Permissions * /
  9. unsigned short __seq; / * Sequence number * /
  10. };

Message Queuing-specific structures

[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 Queuing representation in the kernel


Message Queuing Function Example

msgget function

function: Used to create and access a message queue

Prototype:

[CPP]View Plaincopy
    1. int  Msgget (key_t key, int MSGFLG);

Parameters:

Key: The name of a message queue

MSGFLG: Consisting of nine permission flags, such as 0644, whose usage is the same as the mode pattern flag used when creating the file (but Message Queuing does not have X (execute) permission)

return value:

Successfully returns the message queue number, which is the identification code for the message queue; 1 failure return

Programming practices

[CPP]View Plaincopy
  1. //Practice 1:IPC_PRIVATE: Macro with a value of 0   
  2. int Main ()
  3. {
  4.   //ipc_private each time you create a message queue, the descriptor is different!   
  5.   //So, the timing MessageID (key) is transmitted to other processes (unless there is an associated process),   
  6.   //Other processes also cannot use this message queue (except Blood fork)   
  7. int  MsgId = Msgget (ipc_private,0666);
  8. if (MsgId < 0)
  9. {
  10. //If Message Queuing does not exist, an error is turned on, 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 that are related to the current process!
  25. */   


[CPP]View Plaincopy
  1. //Practice 2:ipc_creat   
  2. int Main ()
  3. {
  4. //Specify Ipc_creat, the message queue must be created   
  5. int MsgId = Msgget (0x1235,0666|  Ipc_creat);
  6. if (MsgId < 0)
  7. {
  8. //If Message Queuing does not exist, an error is turned on, 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 it already exists, the report file already exists (error)   
  5. int MsgId = Msgget (0x1235,0666| 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: Low privilege creation, high privilege open   
  2. int Main ()
  3. {
  4. //Low privilege creation   
  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. //High Privilege open   
  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 diagram

Linux Message Queuing practices (1)

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.