我正在尝试找到一个命令来获取mqueue
的实际保留内存。
在/proc/self/limits
中存储了mqueue
的总大小限制(在我的情况下为819200)。
这意味着如果我有10个mqueues
,其限制为10 msg且大小为8192,则总大小为10*10*8192=819200
,这是系统限制中的数字。
我知道如何增加此限制,但我不知道如何获取mqueue
的实际已用内存(例如,如果我目前正在使用6 mqueues
)。
我的问题是我可以找到这个尺寸491520
#include <iostream>
#include <vector>
#include <cerrno>
#include <cstring>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include "mqueue.h"
std::string exec(const char* cmd) {
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
class mqueue {
public:
mqueue(std::string n = "/dummy", int maxMsg = 10) : queue(), attr(), name(n) {
/* Set attributes */
this->attr.mq_flags = 0;
this->attr.mq_maxmsg = maxMsg; /* war: MAX_MSG_QUEUE */
this->attr.mq_msgsize = 8192; /* war: MAX_MSG_SIZE */
/* Destroy old message queue */
mq_unlink(this->name.c_str());
/* Open message queue */
queue = mq_open(this->name.c_str(), O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, &this->attr);
std::cout << "\n[info] in constructor class mqueue name: " << this->name << " | errno: " << std::strerror(errno);
}
~ mqueue() {
//std::cout << "\n[info] in destructor class mqueue name: " << this->name << " | close = " << mq_close(this->queue) << " | unlink = " << mq_unlink(this->name.c_str());
}
void getInfo() const {
std::cout << "\nname: " << this->name << " | mqueue: " << this->queue << "\n" ;
}
private:
std::string name;
mqd_t queue;
mq_attr attr;
};
int main(int argc, char **argv)
{
int i;
mqueue testMqueue("/dummy");
testMqueue.getInfo();
mqueue testMqueue1("/dummy1", 1);
mqueue testMqueue2("/dummy2", 6);
mqueue testMqueue3("/dummy3", 7);
mqueue testMqueue4("/dummy4", 10);
mqueue testMqueue5("/dummy5", 2);
mqueue testMqueue6("/dummy6", 3);
mqd_t mqdes;
mq_attr mqstat;
for (i = 0; i < 99; i++) {
mqdes = i;
mq_getattr(mqdes, &mqstat);
std::cout << "\n" << i << "\tmaxmsg: " << mqstat.mq_maxmsg << "\tmsgsize: " << mqstat.mq_msgsize << "\tsize: " << mqstat.mq_maxmsg * mqstat.mq_msgsize;
}
return 0;
}
感谢您的回答。 阿德里安
示例:
编译并执行:
std::vector<mqueue> testMqueue;
for (i = 0; i < 20; i++) {
testMqueue.push_back(mqueue("/dummy" + std::to_string(i)));
testMqueue.at(i).getInfo();
}
std::cout << exec("ipcs -q") ;
正如您所见:
[info] in constructor class mqueue name: /dummy19 | errno: Too many open files
name: /dummy19 | mqueue: -1
------ Message Queues --------
key msqid owner perms used-bytes messages
当我在终端 中执行ipcs -q时,列表也为空