如何在基于 Linux 的系统上的 ac 程序中使用 mqueue?
如何在基于 Linux 的系统上的 ac 程序中使用 mqueue(消息队列)?
我正在寻找一些好的代码示例,可以展示如何以正确且正确的方式完成此操作,也许是一个操作指南。
How do I use mqueue (message queue) in a c program on a Linux based system?
I'm looking for some good code examples that can show how this is done in a correct and proper way, maybe a howto.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
代码如下供大家参考:
编译每个源文件,以获得编写器可执行文件和读取器可执行文件。如下::
执行每个二进制文件,您可以发送消息并从消息队列读取消息。另外,尝试通过运行命令(在队列的不同状态)来查看消息队列状态:
对于您的 Linux 系统,您可以使用以下命令了解 IPC 机制和可用队列等的所有详细信息:
参考博客
Code as below for your reference:
Compile each of the source files, to get a writer-executable and reader-executable. As below::
Executing each of the binaries, you can send the message and read the message from the message queue. Also, try to see the message queue state, by running command (at different states of queue):
For your linux system, you can know all the details of the IPC mechanisms and available queues etc, by using:
Reference Blog
下面是一个服务器的简单示例,该服务器接收来自客户端的消息,直到收到告诉其停止的“退出”消息。
服务器的代码:
客户端的代码:
通用标头:
编译:
The following is a simple example of a server that receives messages from clients until it receives an "exit" message telling it to stop.
The code for the server:
The code for the client:
The common header:
Compiling:
mq_send(mq, (char *)(&a), sizeof(int), 1)
从缓冲区&a< 复制
sizeof(int)
个字节/code> 在这种情况下,它不携带变量a
的指针,而是将变量a
的值从一个进程携带到另一个进程。实施是正确的。mq_send(mq, (char *)(&a), sizeof(int), 1)
copiessizeof(int)
bytes from buffer&a
in this case, it does not carry the pointer of variablea
, but carries the value of variablea
from one process to another process. Implementation is right.