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;
}