能帮上什么忙,要求是在重新启动服务器后保留变量的值。
假设我在一个服务器中的一个进程中有多个C编程文件,我想在重新启动服务器之前保留一些值,一旦重新启动,我想根据上次重新启动的值执行操作(即,状态)。
我不确定它是否会工作,但渴望知道是否有一个全局静态变量 将保存早期状态的值....?
ex:静态int previous_status [10]
注意:我的要求是在Linux系统上,以简化我对单个进程的总结
答案 0 :(得分:1)
将数据存储在memory mapped file中。然后它将表现出来并在程序中作为常规内存进行访问,但是将由OS保留在映射文件中。重新启动后,重新映射现有文件,它将包含最后一个状态。
但是,需要采取一些谨慎措施-如果重新启动或终止中断了映射数据访问,则状态可能不一致。可能需要某种形式的数据验证。
答案 1 :(得分: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 .