Solved

share a file between two processes.

Posted on 2002-06-26
9
196 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

626 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