#!/usr/local/bin/perl -w use strict; use Fcntl qw/:flock/; my $file = 'flock.test'; -e $file or open(FILE, ">$file") or die "Can't create $file: $!"; if (fork) { open(FILE, "<$file") or die "Can't open $file for reading in parent: $!"; flock(FILE, LOCK_EX) or die "Can't flock $file in parent: $!"; wait; } else { sleep 1; open(FILE, "<$file") or die "Can't open $file for reading in child: $!"; flock(FILE, LOCK_EX|LOCK_NB) or die "Can't flock $file in child: $!"; }