本文给出了一个demo,说明Linux应用编程中mq_open/mq_send/mq_receive几个API的基本使用方法。
#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <stdio.h>
#include <unistd.h>
#define TEST_MQ_FILE_NAME "/test_mq"
#define TEST_MESSAGE_SIZE 100
int main(int argc, char **argv)
{
mqd_t mqd;
struct mq_attr attr= {
.mq_flags = 0,
.mq_maxmsg = 1,
.mq_msgsize = TEST_MESSAGE_SIZE,
.mq_curmsgs = 0
};
char msg[TEST_MESSAGE_SIZE];
int count;
int ret;
if((argc != 2) || (argv[1][0] != '0' && argv[1][0] != '1')) {
printf("usage: %s 0|1\n", argv[0]);
return -1;
}
mqd = mq_open(TEST_MQ_FILE_NAME, O_RDWR | O_CREAT, 0777, &attr);
if(mqd < 0) {
perror("mq_open");
return -1;
}
if(argv[1][0] == '0') {
/* send */
while(1) {
sprintf(msg, "test msg %d", count++);
ret = mq_send(mqd, msg, s

最低0.47元/天 解锁文章
955

被折叠的 条评论
为什么被折叠?



