简体   繁体   English

从程序内部复制C程序的整个bss和数据段?

[英]Copy the entire bss and data segments of a C program from within the program?

I'm trying to build a checkpoint-restart library for a specific HPC application. 我正在尝试为特定的HPC应用程序构建检查点重启库。 To perform a checkpoint, I need to be able to make copies of each processing element's global and static variables. 为了执行检查点,我需要能够复制每个处理元素的全局和静态变量。 I want to be able to do this by simply grabbing the contiguous block of memory where all of these variables -- initialized as well as uninitialized -- are stored and writing it to somewhere else. 我希望能够通过简单地获取存储所有这些变量(已初始化和未初始化)的连续内存块并将其写入其他位置来实现此目的。

I've Googled thoroughly and haven't come up with anything. 我已经对Google进行了彻底的搜索,没有提出任何建议。 I would be hugely grateful for any help. 我将非常感谢您的帮助。 Any suggestions of better ways to solve this problem are more than welcome, too. 任何解决此问题的更好方法的建议也都受到欢迎。

Depending on your compiler, these may be either in your heap or the data sections of the binary. 根据您的编译器,它们可能在您的堆中,也可能在二进制文件的数据部分中。 I'm not familiar enough with the C standard to know exactly which aspects of the allocation process are guaranteed by the spec, but trying to dump these parts of memory and restore from them later on sounds like an approach with lots of hidden pitfalls. 我对C标准不太熟悉,无法确切地知道规范可以保证分配过程的哪些方面,但是尝试转储内存的这些部分并在以后听起来像带有许多隐藏陷阱的方法从中恢复。 Not to mention the potential wasted time grabbing uninitialized memory. 更不用说浪费时间浪费未初始化的内存了。

I would recommend you instead explicitly dump what you need to an serial format like JSON, and restore from these binary files on restart. 我建议您改为将所需的内容显式转储为JSON之类的串行格式,并在重新启动时从这些二进制文件中恢复。 It will take more effort to set up and maintain this save and restore process, but I think it will be safer in the long run than dumping the entirety of your heap and data sections. 建立和维护此保存和还原过程将花费更多的精力,但是从长远来看,这比转储整个堆和数据节更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM