Click to See Complete Forum and Search --> : Semaphore files


Jonathan
11-08-2003, 11:03 PM
What are semaphore files and how can i create them?

Scriptage
11-10-2003, 06:11 PM
Semaphore files are files that are opened as a lock to prevent simultaneous writes to a file. They prevent data corruption.

#!/usr/bin/perl -w

use Fcntl qw(:flock);
use strict;

sub get_lock{
open(SEM, ">semaphore.sem") || die "Couldn't create semaphore: $!";
flock SEM, LOCK_EX;
}

sub release_lock{
close(SEM);
}

get_lock();

my $input = <STDIN>;

open(FILE, ">file.txt") || die "Cannot open file: $!";
print FILE "$input\n"
close(FILE);

release_lock();

Jonathan
11-10-2003, 06:32 PM
do i just make a blank document and save it as .sem?

Scriptage
11-11-2003, 03:48 PM
no.
Just calling a file.sem wont work.

You have to use the Fcntl module in perl or manually write your own.
ie:

sub get_lock{
if(open(SEM, "semaphore.sem")){
get_lock();
}else{
open(SEM, ">semaphore.sem");
close(SEM);
}
}

sub release_lock{
unlink("semaphore.sem");
}

get_lock();
open(FILE, "somefile.txt")
my $data = <FILE>;
close(FILE);
release_lock();

This has the same effect but is somewhat unreliable.

Scriptage
11-11-2003, 03:49 PM
may have got my wires crossed...

the first code given will automatically write the needed semaphore file (which is a blank file with a .sem ending although it could be .txt, .dat or anything)

Jeff Mott
11-11-2003, 04:17 PM
Although using an extra file in the example given by Scriptage is completely redundant. You're flocking a file, so why not just flock the file you're actually using?open(FILE, 'somefile.txt') or die $!;
flock(FILE, LOCK_SH) or die $!;
my $data = <FILE>;
close(FILE) or warn $!;

Scriptage
11-11-2003, 04:19 PM
using another file makes it easier to lock multiple files using get_lock() and release_lock()

Jeff Mott
11-11-2003, 04:43 PM
Why would you want to lock multiple files at the same time? That just means files will end up being locked while they're not being used. It seems better to lock each file as needed. It also gives you more versitility in choosing whether each individual file should received a shared or exclusive lock.

Scriptage
11-11-2003, 05:48 PM
no i mean:

get_lock();
somefile here
release_lock();

get_lock();
another file here
release_lock();

get_lock();
final file here
release_lock();

wihtout

open FILE...
flock FILE, LOCK_EX;
close(FILE);

open FILE2...
flock FILE2, LOCK_EX;
close(FILE2);

etc

it is just cleaner

Jeff Mott
11-11-2003, 07:27 PM
it is just cleanerCleaner? In place of one statement (flock) you now have two (get_lock and release_lock). And from your example, since the locks are not specific to any particular file, placing a lock on "some file" means also placing a lock on "another file" and "final file" even though they are not being used at that time. And you can't use a shared lock for any of them.

All I see in that example is more coding for a less efficient program.

Jonathan
11-11-2003, 10:37 PM
I just need to know how to make one, and is it easy? Like can i just write to a .txt instead of a .sem?

Scriptage
11-12-2003, 05:13 AM
fair enough jeff.
the locking of the file happens at the line

flock FILE, LOCK_EX;

you dont actually need a semaphore file.