Can two different programs have complete access to single INI file at the same time? - ini

I have two different programs that need to have complete access to a single INI file to read and write into at the same time. Is this possible?
I am using fpc 2.6.2 and Lazarus 1.0.12.

Related

how to store variables persistently as a config

what is the simplest way to store variables persistently?
In my case i just want to store some strings for a config which can be accessed when my program gets restarted.
I have read about writing and reading txt files, but I was wondering if there is not an even more convienient way to do it?
Saving configuration data is handled differently in different operating systems:
on Unix systems, it is customary to use a text file in the home directory of the user with the values of the configuration settings, in a readable format, allowing for comments, and preferably keeping them across updates.
on Windows, albeit the same method works fine, there is a facility called the registry with various APIs to save and restore settings. For portability and usability, it is probably simpler to use a configuration file on legacy systems too.

C - Shared library with management of the same file

I'd like to create shared library in C for linux, some abstract implementation of database management. Shared library whould be responsible for read file containing database and write differences into it. But I have no idea how to handle multiprocessing problems of file handling for this case eg.: App1 try to write differences into database file and App2 has currently opened file with database to read it. In the case of this example I'd like to inform app1 that file is currently open and delay write sequence until App2 will finish database file read.
I was thinking of using some mutual exclusion mechanisms or by using global enum variable to manage current file status, but after I read some of posts I understood that every application that uses shared library create it's own copy in memory and they don't share any memory section during work.
Shared library whould be responsible for read file containing database and write differences into it.
That is possible but quit complicated solution.
While you would need to make sure that multiple processes do not interfere with each other. It's possible to do so with file logs (see man flock), and record locking in man fcntl but you must insure that multiple processes update disjoint file chunks "in place" (without resizing the file).
This is also prone to deadlocks -- if one of the processes takes a lock on a region and then goes into infinite loop, other processes may get stuck as well.
A much simpler solution involves client-server, where the server implements all writes and clients send it read and modify requests.
P.S. There are existing libraries implementing either approach. You will likely save yourself several months of development time using existing solutions.

Can Logrotate be use in C?

I'm a newbie on linux and I was planning to use logrotate so that my log files which generate inside a radio file system, would not getting larger size and cause any memory issue.
but as can see from below, this configuration is set at logrotate.conf file in linux, then I am confused and wonder is it possible to add this thing as a "coding style" in my C files so that it work on my radio which is running Linux system.
The radio don't have this logrotate thing under /etc/logrotate.d path after all. I also unable to install any logrotate or execute the command "man logroate", it tells : no manual entry for 'logrotate'.
/var/logs/*.log {
hourly
missingok
rotate 24
compress
create
}
Can it be called like normal function such as fclose() or fopen() in .C files?
If it can be used in C files, what is the correct way to configure the parameter?
I think you misinterpreted how logrotate work: It only work with files created by service/application on host machine.
For example on your linux machine you are running mailing server postfix (base on config you posted):
Postfix service filling log file /var/log/postfix.log
Logrotate take every hour postfix.log and create compressed file postfix.1
Increment number of already existing archive files
Logrotate keep 24 compressed files (= 1 day)
In you case you have to setup application logging to save log files and then you can apply logrotate to it.
Logrotate man page can be found here

How can I package a configuration file with a C program?

I am currently trying to build a new version of a piece of software I developed. The software takes a simple command line argument and appends the argument to the end of a file. My problem is that I want to alter the program so:
Someone can set up a standard location to save the file to.
The program will remember that location.
It will still work for anyone installing the C program on mac, linux or windows.
So basically I am trying to figure out how to create a C executable that comes with persistent memory that it can read and modify. Alternatively I would take any way to create an installer to make this easy for anyone who wants to use my program.
If this were a java program I would just add it to the jar file but I have never seen this documented for the C language.
I would add platform-specific code to store your settings in whatever area users of that particular platform expect. So:
For Linux: store configuration files in the location specified by $XDG_CONFIG_HOME.
For Mac: Use CFPreferences
For Windows: use the registry

Can we lock file or folder in ubuntu by using open CV and C

I want to create a project to lock file and folders in ubuntu by face detection through opencv using C language. Can you please let me know it is possible and how can i do it.
Can't help you with the opencv part, but "lock file and folders" could mean a few things:
You want to change permissions of files so that a given user/group can/cannot
access them. If this is the case, you want the chmod function.
See man 2 chmod. Seems like this is probably what you're after?
Usually, "file locking" on Linux refers to a means to prevent other processes from accessing a file without changing permissions via either:
Mandatory file locking via lockf (or fcntl).
Advisory file locking via flock.
If file locking is what you're after, here are the "see also" documents referred to by the man pages on lockf and/or flock:
https://www.kernel.org/doc/Documentation/filesystems/mandatory-locking.txt
https://www.kernel.org/doc/Documentation/filesystems/locks.txt
Note: Others have indicated you might want to use the C++ API for opencv. All of these functions should work just fine from C++ too.

Resources