想要在内存中创建存储数据的应用程序。但我不希望数据丢失,即使我的应用程序崩溃 [英] Wants to create an application storing data in memory. But i dont want the data to be lost even if my app crashes

查看:163
本文介绍了想要在内存中创建存储数据的应用程序。但我不希望数据丢失,即使我的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要在内存中创建存储数据的应用程序。但我不希望数据丢失,即使我的应用程序崩溃。
我应该使用什么概念?

Wants to create an application storing data in memory. But i dont want the data to be lost even if my app crashes. What concept should i use? Should I use a shared memory, or is there some other concept that suits my requirement better.

推荐答案

你应该使用共享内存,请求持久性(或甚至正交持久性)和/或应用程序检查点

You are asking for persistence (or even orthogonal persistence) and/or for application checkpointing.

这是不可能的通过便携式C ++代码)在一般情况下为某些任意现有的C ++代码,例如因为 ASLR ,因为指针或指向本地调用堆栈,因为多个线程,并且因为外部资源(套接字,打开的文件,...),因为当前

This is not possible (at least thru portable C++ code) in the general case for some arbitrary existing C++ code, e.g. because of ASLR, because of pointers on -or to- the local call stack, because of multi-threading, and because of external resources (sockets, opened files, ...), because the current continuation cannot be accessed, restored and handled in standard C++.

但是,您可以设计 >您的应用程序与持久性。这是一个强大的架构要求。例如,你可以让每个类都包含一些转储方法和它的加载工厂函数。注意共享指针,并考虑到可以有循环引用。研究垃圾回收算法(例如,在 Gc HandBook ),它们类似于持久化所需的那些(复制GC与检查点算法非常相似)。

However, you might design your application with persistence in mind. This is a strong architectural requirement. You could for instance have every class contain some dumping method and its load factory function. Beware of shared pointers, and take into account that you could have cyclic references. Study garbage collection algorithms (e.g. in the Gc HandBook) which are similar to those needed for persistence (a copying GC is quite similar to a checkpointing algorithm).

查看序列化库(如 libs11n )。您还可以考虑使用文字格式(例如 JSON ),也许可以在某些 Sqlite 数据库(或某些真实数据库,例如 PostGreSQL MongoDb ....)。我在我的 monimelt 软件中执行此操作(在C中)。

Look also in serialization libraries (like libs11n). You might also consider persisting into textual format (e.g. JSON), perhaps inside some Sqlite database (or some real database like PostGreSQL or MongoDb....). I am doing this (in C) in my monimelt software.

您还可以考虑使用检查点库,如 BLCR

You might also consider checkpointing libraries like BLCR

重要的是要思考持久性&在设计时早点检查点。将您的应用程序当作一些专门的字节码解释器或VM可能会有帮助(特别是如果您想要保留继续,或某种形式的调用堆栈)。

The important thing is to think about persistence & checkpointing very early at design time. Thinking of your application as some specialized bytecode interpreter or VM might help (notably if you want to persist continuations, or some form of "call stack").

您可以在持久化之前分叉您的进程(假设您在Linux或Posix上)。因此,持续时间并不重要(例如,如果你持续每小时或每十分钟)。

You could fork your process (assuming you are on Linux or Posix) before persistence. Hence, persistence time does not matter that much (e.g. if you persist every hour or every ten minutes).

一些语言实现能够持续它们的整个状态堆) SBCL (良好的Common Lisp实现)及其 save-lisp-and-die Poly / ML - ML方言及其 SaveState Squeak (a Smalltalk 实现)。

Some language implementations are able to persist their entire state (notably their heap), e.g. SBCL (a good Common Lisp implementation) with its save-lisp-and-die, or Poly/ML -an ML dialect- with its SaveState, or Squeak (a Smalltalk implementation).

另请参见此答案& 那一个。 J.Pitrat的博客有一个相关的条目: CAIA as a sleeping beauty a>。

See also this answer & that one. J.Pitrat's blog has a related entry: CAIA as a sleeping beauty.

具有代码的数据持久性(例如 vtables 对象,函数指针)在技术上可能很困难。 dladdr(3) - with dlsym 内容) -pages / man3 / getcontext.3.htmlrel =nofollow> getcontext(3),但我不推荐)。避免名称修改 dlsym )声明 externC所有与持久性相关的代码。如果你想保留一些数据,并能够从一个稍微修改的程序(例如一个小错误修复)重新启动。事情是更复杂。

Persistency of data with code (e.g. vtables of objects, function pointers) might be technically difficult. dladdr(3) -with dlsym- might help (and, if you are able to code machine-specific things, consider the old getcontext(3), but I don't recommend that). Avoid name mangling (for dlsym) by declaring extern "C" all code related to persistence. If you want to persist some data and be able to restart from it with a slightly modified program (e.g. a small bugfix) things are much more complex.

更务实,你可以有一个类表示你的整个持久化状态,并实现方法来持久化(并重新加载它)。然后,您只会坚持在算法的某些步骤(例如,如果您有主循环或事件循环< a>,在该循环的开始)。你可能不想太频繁地持久化(例如,因为持久化所需的时间和磁盘空间)。也许每十分钟。如果适用于应用程序的整体情况,您可能还会考虑一些事务日志

More pragmatically, you could have a class representing your entire persistable state, and implement methods to persist (and reload it). You would then persist only at certain steps of your algorithm (e.g. if you have a main loop or an event loop, at start of that loop). You probably don't want to persist too often (e.g. because of the time and disk space required to persist), e.g. perhaps every ten minutes. You might perhaps consider some transaction log if it fits in the overall picture of your application.

这篇关于想要在内存中创建存储数据的应用程序。但我不希望数据丢失,即使我的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆