?
Solved

Communication and synchronization between Windows applications

Posted on 2005-03-03
4
Medium Priority
?
158 Views
Last Modified: 2013-11-13
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
Comment
Question by:syedasimmeesaq
[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 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 13454434
>>  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
 
LVL 9

Expert Comment

by:rcarlan
ID: 13482397
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

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project
Suggested Courses

752 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