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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-18-2008, 03:18 AM
|
#1
|
|
Member
Registered: Apr 2006
Posts: 49
Rep:
|
[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...
|
|
|
|
07-18-2008, 03:41 AM
|
#2
|
|
Member
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417
Rep:
|
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.
|
|
|
|
07-18-2008, 04:03 AM
|
#3
|
|
Member
Registered: Apr 2006
Posts: 49
Original Poster
Rep:
|
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.
|
|
|
|
07-18-2008, 04:07 AM
|
#4
|
|
Member
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417
Rep:
|
My bad. I have no idea how I dreamed up logging in my head. 
|
|
|
|
07-18-2008, 04:21 AM
|
#5
|
|
Senior Member
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732
Rep:
|
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.
|
|
|
|
07-18-2008, 05:28 AM
|
#6
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
07-18-2008, 08:26 AM
|
#7
|
|
Member
Registered: Jun 2008
Posts: 101
Rep:
|
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.
|
|
|
|
07-18-2008, 08:46 AM
|
#8
|
|
Member
Registered: Apr 2006
Posts: 49
Original Poster
Rep:
|
Quote:
Originally Posted by FranDango
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:56 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|