tags:

views:

129

answers:

2

I have a situation where I need to pick up files from a directory and process them as quickly as they appear. The process feeding files into this directory is writing them at a pretty rapid rate (up to a thousand a minute at peak times) and I need to pull them out and process them as they arrive.

One problem I've had is knowing that my C++ code has opened a file that the sending server has finished with -- that is, that the local FTP server isn't still writing to.

Under Solaris, how can I open a file and know with 100% certainty that no-one else has it open?

I should note that once the file has been written to and closed off it the other server won't open it again, so if I can open it and know I've got exclusive access I don't need to worry about checking that I'm still the only one with the file.

A: 

Is the process that feeds files into the directory is owned by you? If that is the case, then rename the file's extension to .working so that you don't pick up the file that is being used.

EDIT: Since you said it is solaris, write a shell script and use pfiles command to check if the process still uses the file you want to use. If not start processing the file.

Jagannath
No, it isn't owned by us. It's a piece of third party hardware that FTPs files to us.
Andrew
+1  A: 

You can used flock() with operation LOCK_EX to ensure exclusive access. fcntl() is another possible way

#include <sys/file.h>

int flock(int fd, int operation);   

EDIT: Two ways to do this, find an ftp server which locks the file during receiving. I'm afraid you will not be 100% safe if you monitor the ftp server process, using pfiles or lsof (which is available here http://www.sunfreeware.com/) to make sure that no one else is accessing the files.

Maybe you can check the timestamps of the incomming files and if they havn't changed for a few minutes it would be safe to fetch,process or do something with them.

stacker
that depends on the ftp server -- i.e., will the ftp server lock the file ? or will this work against unlocked but active file handles ?
Hassan Syed
In this case a protocol could be used, and after transfering the files another file called 'tx_finished' could be send to indicate the end of transmission.
stacker
We have no control over the device sending the files...
Andrew