Alex
Alex

Reputation: 1

Write to file from many threads

i have a problem. my progrem creates a some number of processes in the system (Windows). They all must write some data in ONE file on a disk. so, i need to synchronize them... but don't know... exactly: my program calls SetWindowsHook and injects one dll in many processes. and they all need to write some data to one file

Upvotes: 0

Views: 314

Answers (6)

jay.lee
jay.lee

Reputation: 19837

I recently had to do almost this exact thing (for logging to a single file from a dll injected into multiple processes).

Use _fsopen() to open the file in each process and then use a mutex for synchronization to ensure that only one process at a time is ever writing to the file.

Upvotes: 0

Mark Wilkins
Mark Wilkins

Reputation: 41222

Single System As David mentioned, you can use a mutex to accomplish this task. In Windows, this is done by using named mutexes and (if you want) named semaphores to do this.

The function CreateMutex can be used to both create the mutex (by the first process) and open it by the other processes. Supply the same lpName value in all processes. Use WaitForSingleObject to gain ownership of the mutex. And use ReleaseMutex to give up ownership.

An example of creating a named mutex can be found here.

If use a named semaphore, you can accomplish the same thing by giving the semaphore an initial count of 1. Use CreateSemaphore to create and open it, WaitForSingleObject to gain ownership (same as with a mutex) and ReleaseSemaphore to give up ownwership.

Multiple Systems The above approach assumes that the processes are all running on the same system. If the processes are running on different systems, then you may need to use the idea mentioned by DVD. You can lock portions of a file and use that as the synchronization method. For example, you could, by convention, lock 1 byte at some offset (it can even be past the end of the file) as a type of semaphore. Using this mechanism, though, may mean you need to implement some kind of efficient wait depending on the functions you use. If you use CreateFile and LockFileEx, you can have the function do a blocked wait by not specifying LOCKFILE_FAIL_IMMEDIATELY in the call.

Upvotes: 1

bbosak
bbosak

Reputation: 5529

Use Stream.Synchronized() See http://msdn.microsoft.com/en-us/library/system.io.stream.synchronized.aspx. This method only works for C# though

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612954

The synchronisation object that works across processes is a mutex.

Upvotes: 2

Joon
Joon

Reputation: 2147

The answer to your problem is to implement Thread synchronization.

If you are using C#, you can put a lock{} statement over your file writing code.

For other languages, you must use a Monitor or Mutex class to synchronize.

Upvotes: 0

DVD
DVD

Reputation: 1784

Windows have a lock foreach file, so if one process is writting in a file windows wont let another write. Mutex is what you want, protect the code where you are writting into the file with one.

Upvotes: 1

Related Questions