如何使用c编程重启后保留值[关闭]

问题描述 投票:-3回答:2

任何人都可以提供帮助,要求是在重启服务器后保留变量的值。

假设我在服务器中有多个C编程文件用于单个进程,我想在重新启动服务器之前保留一些值,一旦重新启动我想根据最后重新启动的值执行操作(即..,States) 。

我不确定它是否会起作用但是渴望知道全局静态是否会保留早期状态的值......?

ex:static int early_status [10]

注意:我的要求是在Linux系统上使其更简单,我总结为单个进程

c embedded-linux
2个回答
1
投票

将您的数据存储在memory mapped file中。然后它将作为常规内存在程序中运行并被访问,但操作系统将在映射文件中保留它。重新启动时,重新映射现有文件,它将包含最后一个状态。

但是需要注意一些事项 - 如果重新启动或终止中断映射数据访问,状态可能会不一致。可能需要对数据进行某种验证。


0
投票
for retaining the updated values of variables after rebooting , it is necessory to store the values of variables in permanent memory as the temporary memory get erased when power off happens .
So for this you need to create the Configuration file( NOTE : Create in R+ mode because other modes will erase the data while creating the file if it is already exist) .  
- Create a file at starting of the execution in R+ mode .
- Write the variable in file which you need to retain , here you need to write the variable again and again where your program is modifying that variable , and here you need to take care of position of variables in the file .
- Use the fflush(file ptr); function to flush the file stream into file ( This is necessory to put data into hard drive for each variable modification , it will allows you to kill the process abnormally) .
- After reboot run the process(if your requirement is like , you need to start process with bootup then make it zombie ) and read and initialize the variables form the file and use in the program for further processing .
© www.soinside.com 2019 - 2024. All rights reserved.