当前位置:网站首页>Multi process programming (III): message queue
Multi process programming (III): message queue
2022-07-03 00:23:00 【HDD615】
Message queue
** Message queue , Is a linked list of messages , Is a list of messages stored in the kernel .** User processes can add messages... To message queues , You can also read messages to the message queue .
Message queuing overcomes the shortcomings that the pipeline can only carry unformatted byte streams and the buffer size is limited .
The advantage is to specify a specific message type for each message , When receiving, you can receive specific types of messages according to custom conditions , It is not necessary to receive data in a first in first out manner like the pipeline .
Message data consists of structures
struct msgbuf {
long msg_type; // Information types
char msg[size_t]; // An array that holds information
};
function :
1、 Required header file
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
2、 Create a message queue
int msgget(key_t key, int msgflg);
- key It's a key value , It is used to identify a globally unique message queue , Generally, hexadecimal numbers are used to represent
- msgflg Indicates the required operations and permissions , It can be used to control and create a message queue
IPC_CREAT
IPC_EXCL
IPC_CREAT | IPC_EXCL
- Return value :
Successful call : Return the identification number of the message queue
Call failed : return -1
3、 Write a message to the message queue
int msgsnd(int msgid, const void *ptr, size_t nbytes, int flag);
- msgid Indicates the identification number of the message queue
- ptr Pointer to the message structure ( Pointer to the structure storing data )
- nbytes Represents the size of the character array in the message structure
- flag Indicates the mode selected by the message queue
0 -- When the message queue is full ,msgsnd It's blocking the process , Until the message can be written into the message queue or the message queue is deleted
IPC_NOWAIT -- Does not block the process , Will return immediately EAGAIN
- Return value :
Successful call : return 0
Call failed : return -1
4、 Read information from the message queue
size_t msgrcv(int msgid, void *ptr, size_t nbytes, long type, int flag);
- msgid Indicates the identification number of the message queue
- ptr Pointer to the message structure ( Pointer to the structure storing data )
- nbytes Represents the size of the character array in the message structure
- type It means reading the data in the message queue according to the type
0 -- Return the first message in the queue
>0 -- The message type in the return queue is type The first message
<0 -- The message type value in the return queue is less than or equal to type Absolute news , If there are many such messages , Then take the message with the smallest type
- flag:
0 -- Blocking process , Until the information is successfully read
IPC_NOWAIT -- If you can't read the information, return immediately , The error code is ENOMSG
- Return value :
Successful call : Returns the length of the message data
Call failed : return -1
5、 Delete message queue
int msgctl(int msgid, int cmd, struct msqid_ds *buf);
- msgid Indicates the identification number of the message queue
- cmd:
IPC_RMID
- buf Set to NULL
give an example
// write_msg.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
struct msgbuf {
long int mymsg_type;
char msg[100];
} msg;
int main() {
// Create a message queue
int msgid = msgget((key_t)0x5005, IPC_CREAT | 0666);
if (msgid == -1) {
perror("msgget!");
}
// Prepare data in advance , And copy it to the message queue structure
msg.mymsg_type = 1;
char buf[100] = "Hello world!\n This is my show!";
memcpy(msg.msg, buf, strlen(buf));
// Send a message to a message queue
int res = msgsnd(msgid, &msg, 100, 0);
if (res == -1) {
perror("msgsnd!");
}
return 0;
}
// read_msg.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
struct msgbuf {
long int mymsg_type;
char msg[100];
} msg;
int main() {
// Create a message queue
int msgid = msgget((key_t)0x5005, IPC_CREAT | 0666);
if (msgid == -1) {
perror("msgget!");
}
// Read the data in the message queue
int res = msgrcv(msgid, &msg, 100, 1, IPC_NOWAIT); // Non blocking read
if (res == -1) {
perror("msgsnd!");
}
// Print data
printf("%s\n", msg.msg);
return 0;
}
View message queues
ipcs -q # View existing message queues
ipcrm -q msgid # According to the message queue identification number , Delete message queue
边栏推荐
- 多进程编程(一):基本概念
- [shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
- sysdig分析容器系统调用
- How do educators find foreign language references?
- 洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
- Where can I find the English literature of the thesis (except HowNet)?
- 1380. Lucky numbers in the matrix
- Returns the size of the largest binary search subtree in a binary tree
- Bypass AV with golang
- 在线预览Word文档
猜你喜欢

Markdown使用教程

【单片机项目实训】八路抢答器

Understanding and application of least square method

Chapter 3 of getting started with MySQL: database creation and operation

Custom throttling function six steps to deal with complex requirements

多进程编程(一):基本概念

Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs

接口差异测试——Diffy工具

35 pages dangerous chemicals safety management platform solution 2022 Edition

Should you study kubernetes?
随机推荐
多进程编程(五):信号量
Several methods of the minimum value in the maximum value of group query
JVM foundation review
Missing number
Develop knowledge points
Explain in detail the process of realizing Chinese text classification by CNN
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
Program analysis and Optimization - 9 appendix XLA buffer assignment
接口差异测试——Diffy工具
论文的英文文献在哪找(除了知网)?
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
What are the projects of metauniverse and what are the companies of metauniverse
Which software can translate an English paper in its entirety?
国外的论文在那找?
Confluence的PDF导出中文文档异常显示问题解决
Slf4j + logback logging framework
Go custom sort
JS interviewer wants to know how much you understand call, apply, bind no regrets series
1380. Lucky numbers in the matrix
NC24840 [USACO 2009 Mar S]Look Up