Foreword:
A message queue is a linked list of messages. You can think of a message as a record with a specific format and a specific priority. A process that has write access to the message queue can add new messages to it according to certain rules; a process that has read permission to the message queue can read the message from the message queue.
Function:
1. Create a new message queue or get an existing message queue
Prototype:
int msgget(key_t key, int msgflg);
Parameter:
key: can be considered as a The port number can also be generated by the function ftok.
msgflg: IPC_CREAT value, if there is no such queue, create one and return a new identifier; if it already exists, return the original identifier.
IPC_EXCL value, -1 if there is no such queue, or 0 if it already exists.
2.Read/write messages to the queue
Prototype:
msgrcv fetch messages from the queue:
ssize_t msgrcv(int msqid, void *msgp, size_t Msgsz, long msgtyp, int msgflg);
msgsnd puts the data in the message queue:
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
Parameter:
msqid: ID of the message queue
msgp: Pointer to the message buffer. This location is used to temporarily store the messages sent and received. It is a user-definable general structure, in the following form: Br>struct msgstru{ long mtype; //greater than 0 char mtext[512];};
msgsz: the size of the message.
msgtyp: The form of the message read from the message queue. If the value is zero, it means that all messages in the message queue will be read.
msgflg: Used to indicate the actions that the core program should take if there is no data in the queue. If msgflg and the constant IPC_NOWAIT are used together, if the message queue is full during msgsnd() execution, msgsnd() will not block, but will return -1 immediately. If msgrcv() is executed, the message queue is empty. When you do not wait, immediately return -1 and set the error code to ENOMSG. When msgflg is 0, msgsnd() and msgrcv() take the blocking wait mode when the queue is full or empty.
3.Set Message Queue Properties
Prototype:
int msgctl ( int msgqid, int cmd, struct msqid_ds *buf );
Parameter: msgctl system call identified by msgqid The message queue performs the cmd operation. The system defines three cmd operations: IPC_STAT, IPC_SET, IPC_RMIDIPC_STAT: This command is used to obtain the msqid_ds data structure corresponding to the message queue and save it to the address space specified by buf. IPC_SET : This command is used to set the properties of the message queue. The attributes to be set are stored in buf.
IPC_RMID : Remove the message queue identified by msqid from the kernel.
Instance:
Message sender: send.c
/*send.c*/#include <stdio.h>#include <sys/types.h># Include <sys/ipc.h>#include <sys/msg.h>#include <errno.h>#define MSGKEY 1024struct msgstru{long msgtype;char msgtext[2048];};main(){struct msgstru Msgs;int msg_type;char str[256];int ret_value;int msqid;msqid=msgget(MSGKEY,IPC_EXCL); /*Check if the message queue exists */if(msqid < 0){msqid = msgget(MSGKEY,IPC_CREAT
for Windows 7 computers Microsoft will allow its computer OEMs and their system co-developers to d
In RHEL/CentOS, it is very convenient to use the setup configuration tool to configure many key conf
With Windows for a long time, it will inevitably start to lack freshness, so some people will try to
The use of X capacitors and Y capacitors, and their differences: 1. DC blocking 2. Bypass 3. Coupl
The best solution for hard disk partitioning under Linux
Minicom can't input the problem solution
Reset MySQL root password under Linux
How to avoid shell scripts being run multiple times at the same time
Modify ubuntu default editor to vim
Linux Squid build transparent proxy gateway server
Linux file directory X execution permission meaning
The difference between several Linux kernel files
How to delete Grub boot after the Linux uninstallation
Linking and loading of programs and implementation of dynamic linking under Linux
Windows7 system how to check the computer boot history
Win7 wireless network card sharing Internet settings graphic tutorial
How to set the relevant input method for multiple programs in win8 system
Win10 Creator Update 15002 leaked version installation: "Seal" to lift Dafa
Flash 10.1 exposed high-risk vulnerability Android is also affected
Password Detector under Win2000/XP/2003