简体   繁体   中英

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. 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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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