当前位置:网站首页>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
边栏推荐
- Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
- MySQL advanced learning notes (III)
- NC24840 [USACO 2009 Mar S]Look Up
- Where can I check the foreign literature of economics?
- Codeforces Round #771 (Div. 2)---A-D
- Returns the size of the largest binary search subtree in a binary tree
- TypeError: Cannot read properties of undefined (reading ***)
- [shutter] open the third-party shutter project
- 秒杀系统设计
- NC24840 [USACO 2009 Mar S]Look Up
猜你喜欢
Which software can translate an English paper in its entirety?
Pytorch里面多任务Loss是加起来还是分别backward?
[shutter] open the third-party shutter project
67 page overall planning and construction plan for a new smart city (download attached)
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
【单片机项目实训】八路抢答器
Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
TypeError: Cannot read properties of undefined (reading ***)
Should you study kubernetes?
随机推荐
论文的设计方案咋写?
Bloom filter
Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
写论文可以去哪些网站搜索参考文献?
多进程编程(五):信号量
1380. Lucky numbers in the matrix
Blue decides red - burst CS teamserver password
Bypass AV with golang
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
大学生课堂作业2000~3000字的小论文,标准格式是什么?
Cmake basic use
Where can I find foreign papers?
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
What are the recommended thesis translation software?
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
Realization of mask recognition based on OpenCV
yolov5train. py