Last touched file by a specific program ?

i have a directory where all .csv files are available. i have 3 perl programs(ex: a.pl,b.pl,c.pl) which continuously runs every 1 minute to scan all files in that directory. now i have 2 questions

1) how can i write an app lock on that particular folder to make sure only one program will scan that folder to get files so that other program will not catch same files ?

2) how to know if one perl program already touched a particular file ?
ex:
a.pl program scanned a folder and accessing 1.csv, 2.csv and 3.csv 3 files. Now it released app lock, now b.pl program accessing same folder but it should not read 1.csv, 2.csv and 3.csv files since a.pl already reading or accessed those folders.

Note: i don't want to rename any files in this process. How to do this ?

any help would be appreciated

Create a new file in that directory, that will be used as a lock file. Use flock - perldoc.perl.org on that lock file in all three scripts. Use that lock file to store information about which script executed last in this directory.

This would be one of the few examples the access time stamp could be sensibly used. Keep a.pl 's starting time stamp in the lock file proposed by bartus11; any file access since was by a.pl , any file accessed before can be processed by b.pl .

With respect to RudiC, use of a file's access time can be problematic if any other process reads the file, such as backups.

Why 3 programs? Are they three instances of the same program?

If the expense of starting a new process is not onerous, perhaps it would be easier just to use a dispatcher that launches no more than N of your programs:

(pseudo shell code!)

filescanner | while read file; do
  while [[ ${N} -le $(pgrep appropriateoptions | wc -l) ]]; sleep ${time}; done
  fileomatic ${file} &
done

Or the dispatcher could write a file to one of N named-pipes, one for each fileomatic, something like:

(more pseudo shell code)

work=$(mktemp -d)

N=0

while read fileomatic; do
  (( N = N + 1 ))
  pipe=${work}/pipe.${N}
  ready=${work}/ready.${N}

  mkfifo ${pipe}

  ${fileomatic} ${ready} < ${pipe} &
done 

filescanner | while read file; do

  while sleep ${delay}; do

    ls -1 ${work}/*.ready | read -aready

    if [[ 0 -lt ${#ready
[*]} ]]; then
      i=$(( ${RANDOM} % ${#ready
[*]} ))
      n=${ready[$i]}
      echo "${file}" >> ${work}/pipe.${n##*.}
      break
    fi

  done

done

The idea is that the fileomatic will touch the 'ready' file when it can process a file

Do you run them by crontab?
Then maybe you can simply chain them.

* * * * * a.pl; b.pl; c.pl