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
- struct Ipc_perm
- {
- key_t __key; /* Key supplied to Msgget (2) */
- uid_t uid; / * Effective UID of owner * /
- gid_t GID; / * Effective GID of owner * /
- uid_t cuid; / * Effective UID of Creator * /
- gid_t Cgid; / * Effective GID of Creator * /
- unsigned short mode; / * Permissions * /
- unsigned short __seq; / * Sequence number * /
- };
Message Queuing-specific structures
[CPP]View Plaincopy
- struct Msqid_ds
- {
- struct Ipc_perm msg_perm; / * Ownership and Permissions * /
- time_t Msg_stime; /* Time of Last msgsnd (2) */
- time_t Msg_rtime; /* Time of Last MSGRCV (2) */
- time_t Msg_ctime; /* Time of last change * /
- Unsigned long __msg_cbytes; /* Current number of bytes in
- Queue (nonstandard) */
- Msgqnum_t Msg_qnum; /* Current number of messages
- In queue * /
- Msglen_t msg_qbytes; / * Maximum number of bytes
- allowed in queue * /
- pid_t Msg_lspid; /* PID of Last MSGSND (2) */
- pid_t Msg_lrpid; /* PID of Last MSGRCV (2) */
- };
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
- 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
- //Practice 1:IPC_PRIVATE: Macro with a value of 0
- int Main ()
- {
- //ipc_private each time you create a message queue, the descriptor is different!
- //So, the timing MessageID (key) is transmitted to other processes (unless there is an associated process),
- //Other processes also cannot use this message queue (except Blood fork)
- int MsgId = Msgget (ipc_private,0666);
- if (MsgId < 0)
- {
- //If Message Queuing does not exist, an error is turned on, Errno=enoent
- if (errno = = ENOENT)
- {
- cout << "ENOENT" << Endl;
- }
- Err_exit ("Mesget error");
- }
- Else
- {
- cout << "MsgId =" << msgid << Endl;
- }
- return 0;
- }
- /**
- The message queue created by Ipc_private can only be used in processes that are related to the current process!
- */
[CPP]View Plaincopy
- //Practice 2:ipc_creat
- int Main ()
- {
- //Specify Ipc_creat, the message queue must be created
- int MsgId = Msgget (0x1235,0666| Ipc_creat);
- if (MsgId < 0)
- {
- //If Message Queuing does not exist, an error is turned on, Errno=enoent
- if (errno = = ENOENT)
- {
- cout << "ENOENT" << Endl;
- }
- Err_exit ("Mesget error");
- }
- Else
- {
- cout << "MsgId =" << msgid << Endl;
- }
- return 0;
- }
[CPP]View Plaincopy
- //Practice 3:ipc_creat| IPC_EXCL
- int Main ()
- {
- //Specify IPC_EXCL, if it already exists, the report file already exists (error)
- int MsgId = Msgget (0x1235,0666| Ipc_creat| IPC_EXCL);
- if (MsgId < 0)
- {
- Err_exit ("Mesget error");
- }
- Else
- {
- cout << "MsgId =" << msgid << Endl;
- }
- return 0;
- }
[CPP]View Plaincopy
- //Practice 4: Low privilege creation, high privilege open
- int Main ()
- {
- //Low privilege creation
- int MsgId = msgget (0x255,0444 | Ipc_creat);
- if (MsgId < 0)
- {
- Err_exit ("Mesget error");
- }
- Else
- {
- cout << "Create Mes OK, MsgId =" << msgid << Endl;
- }
- //High Privilege open
- MsgId = Msgget (0x255,0644 | Ipc_creat);
- if (MsgId < 0)
- {
- Err_exit ("Mesget error");
- }
- Else
- {
- cout << "Create Mes OK, MsgId =" << msgid << Endl;
- }
- return 0;
- }
Msgget function parameter diagram
Linux Message Queuing practices (1)