#!/usr/bin/perl -w use strict; use Fcntl ':flock'; my $dir_path = '/home/snam/subdir'; my $lock_file = $dir_path . '/lock.lck'; # Get a 'directory lock' (note the quotes again) :) open LOCK, '>', $lock_file or die "Could not lock directory '$dir_path': $!"; flock LOCK, LOCK_EX; # Now do whatever you want with this directory. # If all programs use the locking mechanism, they # won't have access to this directory until you execute # a line that says "close LOCK;" (or when the script exits) opendir DIR, $dir_path or die "Could not open directory '$dir_path': $!"; my @files = readdir DIR; closedir DIR; for my $d (@files) { next if $d !~ /^[0-9]+$/; my $procdir = "/proc/$d"; open IN, '<', $procdir . '/status' or die "Can't open status with $procdir: $!"; while () { # Fill this in yourself... } } # We're done with the directory! Let someone else access it close LOCK;