Solved

share a file between two processes.

Posted on 2002-06-26
9
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find boost header files using gnu make (MinGW) on Win 7 11 112
Grammars for C C++ and java 1 145
Unable to start eclipse ? 17 175
How do I get Window Title of all opened process? 4 61
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

751 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