There is a program written in C and running in linux box. It has a configuration file given to it at starting point. I came to know that it can sometime support updating the configuration file without the need to stop the program.
As the configuration update means eventually updating some of the objects(member variables) inside the running program. I want to understand the idea behind it. How we can update the object inside a running program?
I am a beginner and want to understand the concept behind it? Or is it something trivial that only I am missing the point?
CodePudding user response:
Scope the configuration so that it is reread when necessary.
Here's a very simple example
void do_work()
{
Configuration config(path_to_config_file);
while (not_changed(path_to_config_file))
{
// do one unit of work
}
}
int main()
{
while (true)
{
do_work();
}
}
Program starts, enters the loop and calls do_work
. do_work
constructs a Configuration
object that reads the configuration file and stores the settings. Then it enters a loop that checks for a change to the configuration file and does work if the file has not changed.
If the file has changed, the loop exits, do_work
exits, the Configuration
object is destroyed and the loop in main
calls do_work
to load the new configuration and get back to work.
Precisely how you would implement not_changed
depends on the system. It could be as simple as looking for a change in the modification timestamp on the configuration file.
In a real program you'll want to add some extra smarts to allow the user to exit the program.