C程序的类似于Hibernate的功能 [英] Hibernate-like functionality for C program

查看:106
本文介绍了C程序的类似于Hibernate的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个跨数十个文件的C程序.我的目的是编写一个函数,该函数在调用时将所有全局变量保存到某个文件中并退出到OS(Windows中的命令行).稍后,我需要程序从文件中加载全局变量并继续运行.

程序是单线程exe,不会加载任何DLL.

解决此问题的直接方法是列出所有全局变量(假设我将静态变量转换为全局变量)并将其转储到文件中.但这是一项繁琐的工作,因为可能有数百个(即使不是数千个)变量.

还有其他方法,例如转储整个数据段并在以后加载吗?即使我保存了数据段并设法将其加载回RAM,也很难将程序变量与数据段的内存块相关联.

解决方案

现在,我从未尝试过这样做,而且我不知道它是否可以工作,但是... setjmp/longjmp保存了环境并恢复了它./p>

我认为可以使用setjmp将struct(jmp_buf)存储在文件中.然后,当应用程序再次加载时,将jmp_bug加载回RAM并运行longjmp.

你怎么看?!

I have a C program spanning across dozens of files. My aim is to write a function that when called will save all the global variables to some file and exit to the OS (command line in windows). Later, I need the program to load the global variables from the file and continue running.

The program is a single threaded exe which does not load any DLLs.

The straightforward way to solve this is to make a list of all the global variables (assuming I'll convert statics to globals) and just dump them to a file. But it is a tedious work, since there are probably hundreds, if not thousands of variables.

Is there any other way around, say dumping the whole data segment and loading it later? Even if I saved the data segment and managed to load it back to RAM, I'll have trouble to associate the program variables to the memory chunks of the data segment.

解决方案

Now i've never tried this, and I don't know if it would work but...setjmp / longjmp save the environment and restore it.

I think it would be possible to use setjmp, store the struct(jmp_buf) in a file. Then when the application loads again, load jmp_bug back into RAM and run longjmp.

What do you think?!

这篇关于C程序的类似于Hibernate的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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