如何在基于Linux的系统上的ac程序中使用mqueue(消息队列)?
我正在寻找一些好的代码示例,可以以正确和正确的方式显示这是如何完成的,也许是一个howto.
我正在使用POSIX IPC并根据文档 - http://man7.org/linux/man-pages/man3/mq_send.3.html
mq_send()方法只发送char*数据,而mq_recv()只接收字符数据.但是,我想将自定义结构发送到我的msg队列,在接收端,我想获取结构.
示例结构:
struc Req
{
pid_t pid;
char data[4096];
}
Run Code Online (Sandbox Code Playgroud)
那么,有谁知道如何在C lang中实现这一目标?
我正在使用一个包含posix实时扩展的python模块来获取MessageQueues.
这是python代码
#!/usr/bin env python
import uuid
import posix_ipc
import time
def spawn():
return posix_ipc.MessageQueue("/%s" % uuid.uuid4(), flags=posix_ipc.O_CREAT)
i = 0
while True:
i += 1
spawn()
print(i)
Run Code Online (Sandbox Code Playgroud)
这将在报告之前创建大约10 mq OSError: This process already has the maximum number of files open
我调查了mq限制和rlimit并检查它们都设置得非常高.例如
fs.file-max = 2097152
fs.mqueue.msg_max = 1000
fs.mqueue.queues_max = 1000
Run Code Online (Sandbox Code Playgroud)
即使对于特权用户,它仍然只能创建大约10个队列.
直接使用实时扩展的等效C如下
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <mqueue.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在节点 js 和 python 3应用程序之间编写 IPC 的“hello world” 。目前,当新消息到达 Python 应用程序时,我遇到了解析错误。代码是:
Python:
from ipcqueue import posixmq
from time import sleep
q1 = posixmq.Queue('/fila1')
while True:
while q1.qsize() > 0:
print('p1: Recebi na minha fila: ' + str(q1.get()))
sleep(0.5)
Run Code Online (Sandbox Code Playgroud)
节点:
const PosixMQ = require('posix-mq')
var mq = new PosixMQ();
mq.open({
name: '/fila1',
create: true,
mode: '0777',
maxmsgs: 10,
msgsize: 11
});
mq.push("hello world")
mq.close();
Run Code Online (Sandbox Code Playgroud)
当第二个应用程序发送消息时,python 应用程序失败并显示:
文件“../test-fila1.py”,第 9 行,在
Run Code Online (Sandbox Code Playgroud)print('p1: Recebi na minha fila: ' + str(q1.get())) File "/usr/lib/python3.7/site-packages/ipcqueue/posixmq.py", line …
我无法在 C 中创建和打开消息队列。
我尝试了各种函数参数mq_open(),但没有任何结果。
我检查过一些类似的问题,但未能找到解决方案。
我不确定我做错了什么。
我收到mq_open: Invalid argument错误。
// Create the message queue
mqd_t mq;
// Open the message queue
mq = mq_open(argv[1], O_RDONLY | O_CREAT, 0666, NULL);
if (mq == (mqd_t)-1) {
perror("mq_open");
exit(EXIT_FAILURE);
}
Run Code Online (Sandbox Code Playgroud)
我随意给它命名为“a”。