Posix mq和SystemV mq区别

本文介绍 Posix 和 System V 消息队列的内核参数配置建议,并提供 C 语言实现的消息发送与接收代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、 Posix mq

内核参数影响

fs.mqueue.msg_max        消息队列中消息个数最大限制
fs.mqueue.msgsize_max 消息队列中每个消息长度最大限制
fs.mqueue.queues_max  消息队列个数最大限制

建议值

fs.mqueue.msgsize_max = 8192

fs.mqueue.msg_max = 10000

fs.mqueue.queues_max = 16

 

2、SystemVmq

内核参数影响

kernel.msgmnb                  每个消息队列存放消息的总字节数

kernel.msgmni                   消息队列个数最大限制

kernel.msgmax                  消息队列中每个消息长度最大限制

建议值

kernel.msgmnb = 10485760

kernel.msgmni = 16

kernel.msgmax = 65536

 

3、代码示例

Posix mq发送
#include <mqueue.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#define MQ_FILE "/mq_test"
#define BUF_LEN 512

int main()
{
        mqd_t mqd;
        char buf[BUF_LEN];
        int ret, count;
        struct timespec ts;
        struct mq_attr attr;

        attr.mq_flags = 0;
        attr.mq_maxmsg = 10000;
        attr.mq_msgsize = BUF_LEN;
        attr.mq_curmsgs= 0;
        mqd = mq_open(MQ_FILE, O_RDWR | O_CREAT, 0644, &attr);
        if (-1 == mqd) {
                perror("mq_open error.\n");
                return -1;
        }
        printf("mqd:%d\n", mqd);
        count = 0;
        ts.tv_sec = 3;
        ts.tv_nsec = 0;
        while(1){
                ret = mq_timedsend(mqd, buf, BUF_LEN, 0, &ts);
                if (ret != 0) {
                        perror("mq_send error.\n");
                        break;
                }
                ++count;
        }
        printf("send:%d\n", count);
        mq_close(mqd);
        return 0;
}

Posix mq接收
#include <mqueue.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#define MQ_FILE "/mq_test"
#define BUF_LEN 512

int main()
{
        mqd_t mqd;
        char buf[BUF_LEN];
        int ret, count;
        struct timespec ts;

        mqd = mq_open(MQ_FILE, O_RDONLY, 0644, 0);
        if (-1 == mqd) {
                perror("mq_open error.\n");
                return -1;
        }
        printf("mqd:%d\n", mqd);
        count = 0;
        ts.tv_sec = 3;
        ts.tv_nsec = 0;
        printf("start:%ld\n", time(NULL));
        while(1){
                ret = mq_timedreceive(mqd, buf, BUF_LEN, 0, &ts);
                if (ret < 0) {
                        perror("mq_receive error.\n");
                        break;
                }
                ++count;
        }
        printf("end:%ld\n", time(NULL));
        printf("send:%d\n", count);
        mq_close(mqd);
        //mq_unlink(MQ_FILE);
        return 0;
}

SystemV mq发送
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define DATASIZE        300
#define MQKEY           0xFFF0

typedef struct msgbuf_t msgbuf_t;
struct msgbuf_t {
        long mtype;
        char mtext[DATASIZE];
};

int main(int argc, char *argv[])
{
        int             ret, id, msgid, count;
        msgbuf_t        msg;

        if(argc < 2){
                printf("usage:%s xxx\n", argv[0]);
                return 0;
        }
        id = atoi(argv[1]);
        msgid = msgget(id+MQKEY, IPC_CREAT | 0600);
        if(msgid < 0){
                perror("msgget");
                return errno;
        }
        printf("msgid:%d\n", msgid);
        count = 0;
        msg.mtype = 1;
        printf("start:%ld\n", time(NULL));
        while(1){
                ret = msgsnd(msgid, &msg, DATASIZE, IPC_NOWAIT);
                if(ret != 0){
                        perror("msgsnd");
                        break;
                }
                ++count;
        }
        printf("end:%ld\n", time(NULL));
        printf("[msgsnd]:%d\n", count);
        return 0;
}
SystemV mq接收
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define DATASIZE        300
#define MQKEY           0xFFF0

typedef struct msgbuf_t msgbuf_t;
struct msgbuf_t {
        long mtype;
        char mtext[DATASIZE];
};

int main(int argc, char *argv[])
{
        int             id, msgid, count;
        msgbuf_t        msg;
        ssize_t len;

        if(argc < 2){
                printf("usage:%s xxx\n", argv[0]);
                return 0;
        }
        id = atoi(argv[1]);
        msgid = msgget(id+MQKEY, 0600);
        if(msgid < 0){
                perror("msgget\n");
                return errno;
        }
        printf("msgid:%d\n", msgid);
        count = 0;
        printf("start:%ld\n", time(NULL));
        while(1){
                len = msgrcv(msgid, &msg, DATASIZE, 0, IPC_NOWAIT);
                if(len < 0){
                        perror("msgrcv\n");
                        break;
                }
                ++count;
        }
        printf("end:%ld\n", time(NULL));
        printf("[msgrcv]:%d\n", count);
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值