当前位置:网站首页>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
边栏推荐
- Slf4j + Logback日志框架
- 洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
- S12. Verify multi host SSH mutual access script based on key
- Slf4j + logback logging framework
- Digital twin visualization solution digital twin visualization 3D platform
- 英文论文有具体的格式吗?
- What is the standard format of a 2000-3000 word essay for college students' classroom homework?
- Markdown使用教程
- Array de duplication
- 国外的论文在那找?
猜你喜欢

Digital twin smart factory develops digital twin factory solutions

Angled detection frame | calibrated depth feature for target detection (with implementation source code)
![[shutter] shutter open source project reference](/img/3f/b1d4edd8f8e8fd8e6b39548448270d.jpg)
[shutter] shutter open source project reference

Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?

Xcode real machine debugging

The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north

In February 2022, the ranking list of domestic databases: oceanbase regained its popularity with "three consecutive increases", and gaussdb is expected to achieve the largest increase this month

TypeError: Cannot read properties of undefined (reading ***)
![[shutter] open the third-party shutter project](/img/1a/e35d0180612d7e79b55e7818193740.jpg)
[shutter] open the third-party shutter project

Shell脚本基本使用
随机推荐
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
Judge whether the binary tree is full binary tree
Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
Using tensorflow to realize voiceprint recognition
Should you study kubernetes?
CMake基本使用
Xcode real machine debugging
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
国外的论文在那找?
JVM foundation review
Architecture: database architecture design
MFC file operation
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
在线预览Word文档
FRP reverse proxy +msf get shell
请问大家在什么网站上能查到英文文献?
Explain in detail the process of realizing Chinese text classification by CNN
sysdig分析容器系统调用
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Install docker and use docker to install MySQL