Solved

share a file between two processes.

Posted on 2002-06-26
9
190 Views
Last Modified: 2010-04-01
my application uses an input file. at the end of each run the application writes its results to the same file (the input file).
The application runs successively many times.

I want to run a few instances of the application simultaneously -> more then one process will try to read or write to the file, which is not good.

I want to lock the file in some way so as only one process will have read / write access to it.

I am writin in C++ , not MFC.
0
Comment
Question by:shayb022800
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7110844
There are several methods to do that. The most common one would be to open the file in "exclusive" mode, thus denying all other processes access to the file (can be done by setting dwShareMode to '0' when calling 'CreateFile()'). I'd recommend to allow to open the file for more than one process, but sysnchronize read/write access using a mutex, e.g.

HANDLE g_hFileLock = CreateMutex ( ..., "MyFileLock");

if ( !g_hFileLock && ERROR_ALREADY_EXISTS == GetLastError()) {

  // another process already created the lock
  g_hFileLock = OpenMutex ( SYNCHRONIZE, "MyFileLock");
}

//...

void
WriteToMyFile ( ...) {

 WaitForSingleObject ( g_hFileLock, INFINITE);

 // ... do the writing

 ReleaseMutex ( g_hFileLock);
}
0
 
LVL 16

Expert Comment

by:imladris
ID: 7110855
A standard read write open will normally give you exclusive access to the file. In this case the second read/write open will fail. You should be able to check for an error return.
0
 

Author Comment

by:shayb022800
ID: 7110936
jkr - I am not sure but that looks like MFC to me, that is to heavy for me!

imladris-
While the file is locked, I want the file to be open for read and then for write. If I use the standard open (for read) and the close and open again (for write) the locking will stop and another process might catch the file.

I want to lock the file and then read from it write to it and only then release it!

Please provide a code example if possible.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7110946
>>I am not sure but that looks like MFC

This is no MFC at all! Just plain Win32 API...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Axter
ID: 7110982
>>While the file is locked, I want the file to be open for
>>read and then for write. If I use the standard open (for
>>read) and the close and open again (for write) the
>>locking will stop and another process might catch the
>>file.

Then try opening the file via Read-Write mode.
After reading it, just do a seek, and move the file pointer to the beggining of the file, and then write to it.

You should be able to do what you want using standard C/C++ code.
0
 

Author Comment

by:shayb022800
ID: 7110992
jkr -
Could you be a little more elaborate with your code.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7111016
shayb,
Do you know how to open a file in read-write mode?

Is there a problem with this method?
0
 
LVL 86

Accepted Solution

by:
jkr earned 192 total points
ID: 7111025
OK, I'll try to elaborate :o)

// First, we'll have to create a named synchronizations
// object that can by used by several processes
HANDLE g_hFileLock = CreateMutex ( ..., "MyFileLock");

// Try to create the object. This will fail if we are not
// the 1st process to do so.
if ( !g_hFileLock && ERROR_ALREADY_EXISTS == GetLastError()) {

 // another process already created the lock, so 'Open'
 // it instead.
 g_hFileLock = OpenMutex ( SYNCHRONIZE, "MyFileLock");
}

//...

// this is just a sample function that encapsulates
// a write function that is 'guarded' by a sync object
void
WriteToMyFile ( ...) {

// try to access the sync object. If another process is
// writing to the file, this operation will ensure that
// we wait here until the object is available again.
WaitForSingleObject ( g_hFileLock, INFINITE);

// ... do the writing

// now, release the sync object again, so an other process
// can gain access
ReleaseMutex ( g_hFileLock);
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 7137568
Thanx, but - why did you grade this a "C"?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now