Christian
Christian

Reputation: 7852

Sharing setting between processes without using database?

I need to make a user setting available for various processes without using a database such as SqlLite.

The setting can be changed at any time by the user by checking/unchecking a tick box in the GUI while all other processes may only read the setting.

The application which has several processes is built for cross-platform environments (Linux, OSX, Windows) and is using a mix of .NET and C/C++.

To deal with concurrency issues when writing and reading the setting, I'm thinking that the setting can be stored as a file in a platform specific location and when the setting changes the file name is renamed (as this is a atomic operation on most OS:es if I've understood it correctly). The processes reading the setting then only needs to know the location and the file names corresponding to the setting states and wouldn't have to open the file but only read the name.

Would this be a feasible solution? If not, how could it be solved?

Upvotes: 2

Views: 67

Answers (2)

kaptsea
kaptsea

Reputation: 133

In case of a unix environment, you could take a look at shared memory; in essence you can share a segment o memory between multiple processes without the need for complex file I/O. Since this is a multi OS programme, you can take a look at memory-mapped files

Once this shared memory segment is initiated, you can access it within each process or alter it (of course utilizing mutices or semaphores)!

Upvotes: 1

Fareanor
Fareanor

Reputation: 6805

I think your solution is a bit far-fetched because you will need to create one file by setting with the name being the setting value. It can be a lot of empty files for only one feature of an application.

In fact, you don't need a database but a settings file, for example a .ini file.

In order to deal with concurrency, I think you can share a variable which contains the path of the file between your processes (let's call it config_path). Every time a process has to access the file, it will lock the mutex around the shared variable config_path, and release it when closing the file.

I hope this solution does suit your needs.

Upvotes: 2

Related Questions