Solved

share a file between two processes.

Posted on 2002-06-26
9
193 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
Independent Software Vendors: 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2015 compilation and missing DLLs 9 181
FMX enumerated colours 2 132
How to copy an image file into clipboard C/C++? 1 217
How do i run a c++ file? 15 51
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

713 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