LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-18-2008, 03:18 AM   #1
sylvaticus
Member
 
Registered: Apr 2006
Posts: 49

Rep: Reputation: 15
Question [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...
 
Old 07-18-2008, 03:41 AM   #2
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
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.
 
Old 07-18-2008, 04:03 AM   #3
sylvaticus
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
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...

Last edited by sylvaticus; 07-18-2008 at 04:08 AM.
 
Old 07-18-2008, 04:07 AM   #4
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
My bad. I have no idea how I dreamed up logging in my head.
 
Old 07-18-2008, 04:21 AM   #5
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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.
 
Old 07-18-2008, 05:28 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.

Last edited by jschiwal; 07-18-2008 at 05:30 AM.
 
Old 07-18-2008, 08:26 AM   #7
FranDango
Member
 
Registered: Jun 2008
Posts: 101

Rep: Reputation: 15
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

Last edited by FranDango; 09-20-2008 at 05:35 AM.
 
Old 07-18-2008, 08:46 AM   #8
sylvaticus
Member
 
Registered: Apr 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Thumbs up

Quote:
Originally Posted by FranDango View Post
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
 
  


Reply

Tags
c++, io, parallel, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to append a string to a file name? anandkj Linux - Newbie 2 01-10-2008 08:10 PM
how to append columns form a column file in another file adam_blackice Programming 4 09-17-2007 11:33 PM
[Perl] append to new file noir911 Programming 5 02-08-2007 06:38 AM
C/C++ Append to begining of file Richie55 Programming 8 09-04-2006 06:28 AM
append to file d-rez Linux - Newbie 2 06-20-2002 04:55 AM


All times are GMT -5. The time now is 03:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration