Message Queuing - Message Queuing provides a way to send a piece of data from one process to another on this computer - each block of data is considered to have a type, and the receiver process receives a block of data that can have different type values -- Message Queuing also has a 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)
three restrictions on message size /proc/sys/kernel/msgmax--Maximum message length limit (65536/proc/sys/kernel/msgmnb--Total bytes in Message Queuing (65536/proc/sys/kernel/msgmni--Number of entries for Message Queuing (1465)
Delete Message Queue command --ipcrm msg msqid (Note that MSG is not msq)--ipcrm-q msqid
-- Note: When Msqid is greater than 0 o'clock, both commands can release Message Queuing, but when msqid=0 (Msqid=0 is rare), IPCRM msg msqid will error
megget () function int int MSGFLG); -- function: Used to create and access a message queue - parameter key a message queue name MSGFLG consists of nine permission flags, Their usage and the model mode flags used when creating the file are the same : This is actually a character variable, the int type has 32 bits, the preceding 9 bits are used to identify the read and Write permission (rwxr--r--)--a non-negative integer is returned successfully. Failed to return-1, and update errno
//Message Queuing#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>intMainintArgChar*args[]) { /*Open the message queue in a readable and writable manner, the file must exist, and if the message queue does not exist, errno=enoent*/ //int Msgid=msgget (0x1234,0666); /*If Message Queuing exists, Message Queuing is created using an existing message queue, which does not exist for that key value * / //int Msgid=msgget (0x1234,0666| Ipc_creat); /* If there is no Message Queuing for the key value, a message queue is created, and if there is a message queue, the error hint file already exists * is typically used to determine whether a message queue exists (prevents users from overwriting the original content with new content for Message Queuing that uses that key value) * IPC_EXCL alone Must be used in conjunction with Ipc_creat **/ //int msgid = Msgget (0x1234, 0666 | Ipc_creat | IPC_EXCL); /* ipc_private created Message Queuing for parent-child processes only * each use of ipc_private creates a new message queue and does not use the original, * because using ipc_private no longer has the ability to access existing message queues * So ipc_creat and ipc_excl are useless. * But between parent and child processes, because the program is not finished, the parent process is still using Message Queuing, * So the child process can also use the message queue to interact with the parent process information * Ipc_private macro value is 0 **/ intMsgId = Msgget (Ipc_private,0666); if(MsgId = =-1) { if(errno = =ENOENT) {printf ("Custom Error: no this file! \ n"); return-1; } if(errno = =eexist) {printf ("Custom Error: The message queue already exists! \ n"); return-1; } perror ("msgget () Err"); return-1; } printf ("Create message Queue successfully! id=%d \ n", MsgId); return 0;}
//Message Queuing#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>intMainintArgChar*args[]) { //If there is, use existing, no, create a intMsgId = Msgget (0x1234,0444|ipc_creat); if(MsgId = =-1) {perror ("msgget () Err"); return-1; } //access to a read-only, non-writable Message Queuing error with readable writable permissionsMsgId = Msgget (0x1234,0666); if(MsgId = =-1) {perror ("msgget () Err"); return-1; } return 0;}
Linux Inter-process communication two