linux消息队列服务,Linux进程间通信-消息队列(mqueue)深入理解

前面两篇文章分解介绍了匿名管道和命名管道方式的进程间通信,本文将介绍Linux消息队列(posix)的通信机制和特点。

1、消息队列

消息队列的实现分为两种,一种为System V的消息队列,一种是Posix消息队列;这篇文章将主要围绕Posix消息队列介绍;

消息队列可以认为是一个消息链表,某个进程往一个消息队列中写入消息之前,不需要另外某个进程在该队列上等待消息的达到,这一点与管道和FIFO相反。Posix消息队列与System V消息队列的区别如下:

(1) 对Posix消息队列的读总是返回最高优先级的最早消息,对System V消息队列的读则可以返回任意指定优先级的消息。

(2)当往一个空队列放置一个消息时,Posix消息队列允许产生一个信号或启动一个线程,System V消息队列则不提供类似的机制。

2、消息队列的基本操作

2.1 打开一个消息队列

#include   

typedef int mqd_t;

mqd_t mq_open(const char *name, int oflag, ... /* mode_t mode, struct mq_attr *attr */);

返回: 成功时为消息队列描述字,出错时为-1。

功能: 创建一个新的消息队列或打开一个已存在的消息的队列。

2.2 关闭一个消息队列

#include   

int mq_close(mqd_t mqdes);

返回: 成功时为0,出错时为-1。

功能: 关闭已打开的消息队列。

2.3 删除一个消息队列

#include   

int mq_unlink(const char *name)

返回: 成功时为0,出错时为-1

功能: 从系统中删除消息队列。

这三个函数操作的代码如下:

#include

#include

#include

#include

#include

#include

int main(int argc, char* argv[])

{

int flag = O_RDWR | O_CREAT | O_EXCL;

int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

mqd_t mqid = mq_open("/mq_test", flag, mode,NULL);

if (mqid == -1)

{

printf("mqueue create failed!\n");

return 1;

}

else

{

printf("mqueue create success!\n");

}

mq_close(mqid);  return 0;

}

#include

#include

int main(int argc, char* argv[])

{

mq_unlink("/mq_test");

return 0;

}

注意:编译posix mqueue时,要连接运行时库(runtime library),既-l

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 POSIX 消息队列实现线程间通信的例子: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <mqueue.h> #define MSG_SIZE 256 #define MAX_MSG 10 mqd_t mqd; pthread_t tid[2]; pthread_attr_t attr; // 线程1:发送消息 void *send_func(void *arg) { char msg[MSG_SIZE]; int i; for (i = 0; i < MAX_MSG; i++) { memset(msg, 0, MSG_SIZE); sprintf(msg, "Message %d from thread 1", i); if (mq_send(mqd, msg, strlen(msg) + 1, 0) == -1) { perror("mq_send"); exit(1); } printf("Thread 1 sent: %s\n", msg); sleep(1); } } // 线程2:接收消息 void *recv_func(void *arg) { char msg[MSG_SIZE]; unsigned int prio; int i; for (i = 0; i < MAX_MSG; i++) { memset(msg, 0, MSG_SIZE); if (mq_receive(mqd, msg, MSG_SIZE, &prio) == -1) { perror("mq_receive"); exit(1); } printf("Thread 2 received: %s\n", msg); sleep(1); } } int main() { struct mq_attr attr; attr.mq_flags = 0; attr.mq_maxmsg = 10; attr.mq_msgsize = MSG_SIZE; attr.mq_curmsgs = 0; if ((mqd = mq_open("/test_mq", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr)) == -1) { perror("mq_open"); exit(1); } if (pthread_attr_init(&attr) != 0) { perror("pthread_attr_init"); exit(1); } if (pthread_create(&tid[0], &attr, send_func, NULL) != 0) { perror("pthread_create"); exit(1); } if (pthread_create(&tid[1], &attr, recv_func, NULL) != 0) { perror("pthread_create"); exit(1); } if (pthread_join(tid[0], NULL) != 0) { perror("pthread_join"); exit(1); } if (pthread_join(tid[1], NULL) != 0) { perror("pthread_join"); exit(1); } if (mq_close(mqd) == -1) { perror("mq_close"); exit(1); } if (mq_unlink("/test_mq") == -1) { perror("mq_unlink"); exit(1); } return 0; } ``` 该程序创建了一个 POSIX 消息队列 `/test_mq`,其中维护了最大消息数为 10,每条消息为 256 字节。程序启动两个线程,一个用于发送消息,一个用于接收消息,它们都可以同时操作消息队列。发送线程每秒钟向队列中发送一条消息,接收线程每秒钟从队列中接收一条消息并打印出来。程序使用 `pthread_create()` 创建线程,使用 `pthread_join()` 以等待线程完成,使用 `mq_send()` 发送消息,使用 `mq_receive()` 接收消息。最后程序清理了 POSIX 消息队列
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值