view Paper/chapter/one_flock_test1.pl @ 2:a2da5ed4a3b3

add about Context etc
author ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
date Sat, 08 Jan 2022 11:59:24 +0900
parents 853d7c65f524
children
line wrap: on
line source

#!/usr/local/bin/perl

use Fcntl ':flock';

$plane="plane_reserve";
$hotel="hotel_reserve";

my $user = $ARGV[0];

open(HOTEL,"+<$hotel") || die("$$ $user can't open $plane\n");
open(PLANE,"+<$plane") || die("$$ $user can't open $plane\n");
flock(HOTEL,LOCK_EX);
flock(PLANE,LOCK_EX);
sleep 1;
&reserve('HOTEL',$hotel);
# flock(HOTEL,LOCK_UN);
close(HOTEL);   # and release lock

sleep 1;
&reserve('PLANE',$plane);
close(PLANE);   # and release lock


sub reserve {
    my ($fh,$file)=@_;
    if(($value = <$fh>)>0) {
	seek($fh, 0, 0);
	print $fh $value-1,"\n";
	print "$user $file reserved\n";
	return 1;
    } else {
	die("$$ $user can't reserve $file\n");
    }
}