当前位置:网站首页>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
边栏推荐
- What are the recommended thesis translation software?
- 35 pages dangerous chemicals safety management platform solution 2022 Edition
- 洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
- Sysdig analysis container system call
- JS interviewer wants to know how much you understand call, apply, bind no regrets series
- 请问大家在什么网站上能查到英文文献?
- LeedCode1480.一维数组的动态和
- Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
- Digital twin smart factory develops digital twin factory solutions
- Feature Engineering: summary of common feature transformation methods
猜你喜欢

TypeError: Cannot read properties of undefined (reading ***)

接口差异测试——Diffy工具

Xcode real machine debugging

写论文可以去哪些网站搜索参考文献?

Angled detection frame | calibrated depth feature for target detection (with implementation source code)

How QT exports data to PDF files (qpdfwriter User Guide)

Explain in detail the process of realizing Chinese text classification by CNN

Seckill system design

Custom throttling function six steps to deal with complex requirements

Mutual exclusion and synchronization of threads
随机推荐
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
MFC gets the current time
CMake基本使用
接口自动化覆盖率统计——Jacoco使用
[shutter] Introduction to the official example of shutter Gallery (learning example | email application | retail application | wealth management application | travel application | news application | a
Digital twin visualization solution digital twin visualization 3D platform
67 page overall planning and construction plan for a new smart city (download attached)
Seckill system design
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
Digital twin smart factory develops digital twin factory solutions
Maya fishing house modeling
Digital collection trading website domestic digital collection trading platform
Analyze ad654: Marketing Analytics
SQL query statement parameters are written successfully
Hit the industry directly! The propeller launched the industry's first model selection tool
监控容器运行时工具Falco
NC50965 Largest Rectangle in a Histogram
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
布隆过滤器
MySQL 23道经典面试吊打面试官