Solved

share a file between two processes.

Posted on 2002-06-26
9
189 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

18 Experts available now in Live!

Get 1:1 Help Now