多次运行的持久存储
我想知道在不使用文件系统或外部数据库的输入输出的情况下,拥有一个存储容器在多次执行(运行)后不会丢失其内容的最佳解决方案是什么。
假设我有一个存储整数的 foo() 类。我想从 main() 调用一个添加整数的方法,并且该类不会忘记其以前的内容。
//
// Data storage accross different runs
// This should go into the daemon process
//
#include<iostream>
#include<list>
using namespace std;
class foo {
public:
foo(int add): add(add) {}
void store(int i) {
vec.push_back( i + add);
}
private:
list<int> vec;
int add;
};
main 函数应该检查是否有已经运行的守护进程 - 如果没有启动它。
//
// Main program. Should check whether daemon runs already, if not starts it.
//
void main(int argc, char *argv[]) {
// if (daemon is not running)
// start daemon( some_number )
// call daemon::add( atoi(argv[1]) );
}
如何使用共享库或守护进程来最好地做到这一点?存储和调用程序位于同一台Linux主机上。
I was wondering what would be the best solution for having a storage container that does not loose its contents over several execution times (runs) without using input-output to the filesystem or external database.
Say I have a class foo() which stores integers. From main() I want to call a method that adds an integer and the class does not forget about its former contents.
//
// Data storage accross different runs
// This should go into the daemon process
//
#include<iostream>
#include<list>
using namespace std;
class foo {
public:
foo(int add): add(add) {}
void store(int i) {
vec.push_back( i + add);
}
private:
list<int> vec;
int add;
};
The main function should check for an already running daemon - if not starts it.
//
// Main program. Should check whether daemon runs already, if not starts it.
//
void main(int argc, char *argv[]) {
// if (daemon is not running)
// start daemon( some_number )
// call daemon::add( atoi(argv[1]) );
}
How would one do this best with shared libraries or with a daemon process? Storage and caller program are on the same Linux host.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看用于进程间通信的 Linux Pipes。
http://linux.die.net/man/2/pipe
Look at Linux Pipes for interprocess communication.
http://linux.die.net/man/2/pipe
命名管道是一种方法。如果您想要非阻塞,您可能想尝试消息队列路由。这是系统调用之一的链接 http://linux.die.net/man/2 /msgctl,您可以从那里查看其他调用。
Named pipes is one way. If you want non blocking though you might want to try the message queue route. Here is a link to one of the system calls http://linux.die.net/man/2/msgctl, you can look at the other calls from there.
可以考虑 http://en.wikipedia.org/wiki/Memcached
May consider http://en.wikipedia.org/wiki/Memcached