• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Communication and synchronization between Windows applications

Using  Visual C++, I need to have different applications read (and write to) the same text file at same time. The text file contains lines of phone number. Each record should be used only once by appilcations. For example, when one application reads in 5 records, the next application would continue reading from the 6th and so on. So there is an index variable which saves current position and to be accessed by all applications.
The question is what is the machanism for Windows to achieve this?  what are the Win APIs for it.
Also as multiple applications are reading the file simuteniously, what are Win API (or any thing) to be used to avoid racing problem. ( this could happen while one app reads in a record, but before increase record index to point to next record, the second app reads the same record)

Please notice that this is not multi threads issue. Its multi app(tasking) comm and synch issue.
Thank you for you help!
0
syedasimmeesaq
Asked:
syedasimmeesaq
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>>  same text file at same time.

It doesn't matter what language you are using...there is no way to accomplish this using a plain text file.  The ony way you could possibly hope to make this a synchronzied and atomic operation would be to open the text file in an exclusive reading mode so that no other app can open it at the same time.  This way each app can read some data and update the index without interference from the others.

You will just have to attempt to open the file in exclusive mode and if you get an error then another application currently has the file open.  You would then need to wait and tray again in a loop until you are finally able to open it.

One potential problem I see is that you would have the possibility of never being able to open the file.  This would be the same as "thread starvation" in multi-threaded applications where a thread is never able to get control of an object because other threads are always grabbing it first.

One solution to this problem would be to have the applications communicate with some kind of server/master app that tells each app when it is its turn to use the file.  It would simply use a queue mechanism so that each requestor is guaranteed to eventually get its turn.

It should be noted that database systems are specifically designed to handle this kind of situation using record locking mechanisms.

~IM
0
 
rcarlanCommented:
You should consider using a mutex.
Mutexes are designed specifically to help synchronise access to a shared resource across multiple processes.
Each process that needs to operate on the file locks the mutex, does its stuff and then releases the mutex.
If the mutex is locked, the process waits until it is released, at which point it acquires its lock, does its stuff and then releases the mutex.

You said in your original post that this is not a multi-threaded issue - but it is. Each process runs in a separate thread and you need a way of synchronising these threads.
Mutexes are the answer.

Let me know if you want some example code on how to do this. If so, let me know what libraries you have access to (e.g. MFC, Boost, etc). Alternatively, it can obviously be done with just Windows APIs.
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now