views:

29

answers:

2

I have legacy code which I need to improve for performance reasons. My application comprises of two executables that need to exchange certain information. In the legacy code, one exe writes to a file ( the file name is passed as an argument to exe) and the second executable first checks if such a file exists; if does not exist checks again and when it finds it, then goes on to read the contents of the file. This way information in transferred between the two executables. The way the code is structured, the second executable is successful on the first try itself.

Now I have to clean this code up and was wondering what are the disadvantages of using files as a means of communication rather than some inter-process communication like pipes.Is opening and reading a file more expensive than pipes? Are there any other disadvantages? And how significant do you think would be the performance degradation.

The legacy code is run on both windows and linux.

A: 

One issue I've frequently faced with such kind of setups is the synchronization of access to the file, for e.g. if the first process is still writing when the second file tries to read or vice versa. Depending on your requirements this can lead to all sorts of bad behavior.

It's always nice to use a real IPC mechanism but if your app has to be cross-platform, that really limits your choices.

Sijin
A: 

Some problems with using files for IPC are:

  • What happens when process (1) is writing to the file when process (2) finds it? You need to have special logic to handle this case.

  • What happens if process (1) wishes to send another message while process (2) is still reading from the file? (1) would have to somehow detect that the file cannot be written to, and wait until it is available.

  • Files could become a bottleneck under large amounts of message traffic, especially if you are only using a single file for IPC.

To determine if file I/O is a performance bottleneck for you, we need to understand more about the messages you are sending. How large they are, how frequently they are sent, etc. Otherwise it is difficult to judge what affect they have on your performance, if any.

That said, I have used files in the past to pass information between processes, although typically either new filenames will be created each time or files will be used to pass large amounts of data and a smaller IPC message will be used to signal when the file is ready.

In my opinion unless you have a reason to use files - such as transfers of large amounts of data - I would prefer a traditional IPC mechanism such as pipes, sockets, etc. But you would have to implement it carefully to make sure everything works on both platforms.

Justin Ethier