LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   [C++] Append to a file from several instances (http://www.linuxquestions.org/questions/programming-9/%5Bc-%5D-append-to-a-file-from-several-instances-656565/)

sylvaticus 07-18-2008 04:18 AM

[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...

jcookeman 07-18-2008 04:41 AM

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.

sylvaticus 07-18-2008 05:03 AM

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...

jcookeman 07-18-2008 05:07 AM

My bad. I have no idea how I dreamed up logging in my head. :)

pinniped 07-18-2008 05:21 AM

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.

jschiwal 07-18-2008 06:28 AM

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.

FranDango 07-18-2008 09:26 AM

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

sylvaticus 07-18-2008 09:46 AM

Quote:

Originally Posted by FranDango (Post 3218866)
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.

yes.. I think I'll do like that.. I went googling searching for something simple in C++ like the lock() in perl, but I went far too deeply for my knowledge (and I don't want to use too many libraries as this model is multi-platform), so the simplest thing looks like to have one-output directory by instance (scenario).. I'll then post-process them with some simple python script...

thanks all..
Antonello Lobianco


All times are GMT -5. The time now is 01:16 AM.