当前位置:网站首页>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
边栏推荐
- sysdig分析容器系统调用
- MySQL advanced learning notes (III)
- AcWing_188. 武士风度的牛_bfs
- NC20806 区区区间间间
- yolov5test. Py comment
- 写论文可以去哪些网站搜索参考文献?
- [shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
- 哪些软件可以整篇翻译英文论文?
- Top Devops tool chain inventory
- Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
猜你喜欢

Mutual exclusion and synchronization of threads

Bigder:32/100 测试发现的bug开发认为不是bug怎么处理

CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)

Confluence的PDF导出中文文档异常显示问题解决

67 page overall planning and construction plan for a new smart city (download attached)

redis21道经典面试题,极限拉扯面试官

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

请问大家在什么网站上能查到英文文献?

How do educators find foreign language references?

Which websites can I search for references when writing a thesis?
随机推荐
NC50528 滑动窗口
LeedCode1480.一维数组的动态和
Monitor container runtime tool Falco
Nc50528 sliding window
form表单实例化
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Using tensorflow to realize voiceprint recognition
Additional: token; (don't read until you finish writing...)
Go custom sort
Develop knowledge points
TypeError: Cannot read properties of undefined (reading ***)
Hit the industry directly! The propeller launched the industry's first model selection tool
Go自定义排序
[shutter] Introduction to the official example of shutter Gallery (learning example | email application | retail application | wealth management application | travel application | news application | a
Missing number
Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
有哪些比较推荐的论文翻译软件?
Chapter 3 of getting started with MySQL: database creation and operation
Define MySQL function to realize multi module call
Explain in detail the process of realizing Chinese text classification by CNN