[C++] Append to a file from several instances
Hi there..
is there a way to output (a simply append) to the same file from several instances of a program using C++ ?? My doubt is that without a sort of locking instanceY could start appending while instanceX is writing in the middle of the row... |
log4cxx
I dont know how serious you are about this application and logging, but you really should look at something like log4cxx.
A proper logging facility is not exactly a trivial amount of work, and since you'd rather focus on your application, a mature solution like this is exactly what you need. We use its cousin log4j, and it works quite well. |
hi.. thanks for the link...
Really I do not need it for "logging", rather for outputing the results of an economic model, and the output is already quite personalised (some outputs are ASCII GIS maps, some are simple lists, some are data tables...). My problem is that this model is moving form a "desktop" approach to a cluster one, where several instances are runned simultaniously. I ran some tests and with my surprice everything was working fine, but I don't know if I was just lucky that there were no simultaneous access to the same file or (hopefully) the cluster OS already provides a sort of file locking... |
My bad. I have no idea how I dreamed up logging in my head. :)
|
If several instances of a program work on the same file, you need to make those programs aware of eachother. You can write a small program to see if discretionary file locking is enforced, and if it were, you might get away with programs being oblivious of eachother and simply using IOCTLs to gain sole write access (and release access) to a file. Writing interspersed data to a single file from multiple sources seems weird though.
|
I think the logging approach mentioned would be best. Another way of thinking of it is that the instances are clients. Let a logger (server) do the writing to the file. It's not so much that you are creating a log file as you are letting a single process write to the file for the instances. I'm thinking of a client/server writer, not necessarily a particular logging program.
|
Wouldn't it be easier to have each instance write it's output into a single location, but under a different unique filename, and also creating an ID-file at startup to indicate running instances. Each instance would delete it's ID-file when finished, and the last one (determined by being the only one with an ID-file left) will merge all the created output in a single file and delete the other files.
Isn't as complicated as it sounds, and it doesn't need locking or external applications. Linux Archive |
Quote:
thanks all.. Antonello Lobianco |
All times are GMT -5. The time now is 02:54 AM. |