472,347 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

using fstream objects in a multiuser environ.


At my current job I've been tasked with maintaining a very old C++
application written largely in ANSI c++ in what I believe to be with an old
DOS based Borland compiler. I'm trying to add a simple event logging
component to the application. Initially I began with a simple:

[...]
fstream dumpfile
dumpfile.open(fname,std::ios_base::out | std::ios_base::app);
[...]
//Event log occurs
dumpfile << dumptext << std::endl;
[...]

But I realized that I would (and did when I started testing) have a problem
because this DOS app is used in a multiuser environment. Competing users
will overwrite eachothers entries if they occur at the same time.
Previously, my pure ANSI c++ experience has been in primarily single
computer/user environs- and any multiuser experience usually got into OS
specific extentions so this never was an issue.

My question is, what methods would someone suggest to me to allow multiuser
access (using any object available in an ANSI environment) which would avoid
such multiuser issues as described above.

Having some experience with race conditions in other os/languages unrelated
to C++, I came up with one idea-- which seemed kludgey. That is to first
check to see if a 'locking' control file exists in a common directory. If
it sees a file there, assume someone else has control of the log file, wait,
repeat step one. If it doesn't find a locking file, create it and write
some unique set of bytes determined by the individual instance (user) of the
app. Wait a 'reasonable' time, then re-read the bytes-- if said bytes
remain as the unique identifier- I can conclude that I have control and can
then open the real log file, write, then delete the 'locking' file when the
write and buffer flush(close) on the log file are complete.

Again, that's the only thing I've come up with. Any advice/criticism (or
especially new ideas) would be appreciated. Thanks in advance,

Paul
Jul 23 '05 #1
4 2279
1) Give each user his own error file with a name like
filename_processid_random_number.txt
2) If you want just one global error file for all users... you can make
sure each user app process logs temporally to memory. Periodically
they could open the global error file with exclusive access flush their
log and close the file

Raj

Jul 23 '05 #2

<ra******@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
1) Give each user his own error file with a name like
filename_processid_random_number.txt
2) If you want just one global error file for all users... you can make
sure each user app process logs temporally to memory. Periodically
they could open the global error file with exclusive access flush their
log and close the file

1: Unfortunately I have a specific application need which disallows a
localized or 'many' log files situation. It must be a centralized log file.

2: But my understanding of ANSI c++ is that there is no exclusive open for a
file. Am I incorrect? Is there an ANSI specification for exclusive access
with an FSTREAM object? Or any other file stream object? I'll research
while I wait for a reply.

Paul
Jul 23 '05 #3

"Pablo" <jn**********@comcast.net> wrote in message news:K8********************@comcast.com...
Having some experience with race conditions in other os/languages unrelated
to C++, I came up with one idea-- which seemed kludgey. That is to first
check to see if a 'locking' control file exists in a common directory. If
it sees a file there, assume someone else has control of the log file, wait,
repeat step one. If it doesn't find a locking file, create it and write
some unique set of bytes determined by the individual instance (user) of the
app. Wait a 'reasonable' time, then re-read the bytes-- if said bytes
remain as the unique identifier- I can conclude that I have control and can
then open the real log file, write, then delete the 'locking' file when the
write and buffer flush(close) on the log file are complete.

Again, that's the only thing I've come up with. Any advice/criticism (or
especially new ideas) would be appreciated. Thanks in advance,


The problem with the lock file is that if one of the users crashes
or has some problems (nic disconnect etc.) the file may not
get cleaned up and you'll be stuck. You probably need some
sort of static instance, maybe a singleton with mutex locks
and a queue mechanism to guarantee that all users work.

Another alternative may be to log to a separate file for
each user and periodically do some sort of merge
into one common file. At least at that point you could
open the files as read only but you still have the problem
that the file may be getting updated while you're merging
it.
Jul 23 '05 #4
I couldn't find one ....

VC++ seems to have a

fstream( const char* szName, int nMode, int nProt = filebuf::openprot
); constructor.

The third argument can used for exclusive access. I am not sure if this
is standard compliant

If you find one, I would be eager to know it as well

Raj

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Erick Bodine | last post by:
I am trying to set a new environment variable on a W2k machine with only partial success. The name("SSID") and value("ASIM") show up correctly in...
2
by: zeroDontSpamMetype | last post by:
Hi, I've been including code like this in my programs for some time: fstream fin ("c:\\dave.txt", ios::in); if (!fin) { return -1; //or...
11
by: jim.eggleston | last post by:
Windows doesn't have a HOME environment variable, but it does have HOMEDRIVE and HOMEPATH. Could Windows versions of Python automatically populate...
1
by: liam_herron | last post by:
I have compiled my boost-enabled C++ module and have it working when I explicity set my LD_LIBRARY_PATH before invoking the python2.4 interpreter. ...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the...
9
by: boris.smirnov | last post by:
Hi there, I have a problem with setting environment variable in my script that uses qt library. For this library I have to define a path to tell...
12
by: nathan | last post by:
Hello, Has anyone experienced that records get duplicated by itself. When I look in my tables backend mdb file there are 2 records with the same...
7
by: Joe P. Cool | last post by:
If I call os.environ.clear in a python program child processes still see the deleted entries. But when I iterate over the keys like so names = ...
4
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test,...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.