in reply to Flock

To lock an open filehandle:
flock FH, 2;
To unlock the file:
close FH;
You don't need to explicitly unlock the file. Just close it.

How can I lock a file?