请为我提供一段linux信息队列接收的代码示例
时间: 2024-05-02 07:20:40 浏览: 17
以下是可以用于接收Linux信息队列的代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
#define MAX_MSGS 10
#define MSG_SIZE 256
int main(int argc, char **argv) {
mqd_t mq;
struct mq_attr attr;
char buffer[MSG_SIZE + 1];
int msg_count, i;
//打开信息队列
mq = mq_open("/test_queue", O_CREAT | O_RDONLY, 0644, &attr);
if (mq == -1) {
perror("mq_open");
exit(1);
}
//接收信息
msg_count = mq_receive(mq, buffer, MSG_SIZE, NULL);
if (msg_count == -1) {
perror("mq_receive");
exit(1);
}
//添加字符串结束标志
buffer[msg_count] = '\0';
printf("Received: %s\n", buffer);
//关闭信息队列
if (mq_close(mq) == -1) {
perror("mq_close");
exit(1);
}
return 0;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)