Logo
  • Home
  • About
  • Services
  • Blogs
  • Career
  • Contact
Logo

The UK's #1 Software Development Company. PodTech IO builds custom software solutions for businesses. Call today for a free consultation

  • Address

    4th Floor, 4 Tabernacle Street London EC2A 4LU
  • Email

    info@podtech.com
  • Contact

    +44 (0) 20 8720 6583

Perl – Lock Files (flock)

  • Home
  • Perl – Lock Files (flock)
  • By podtech
  • In Perl

Perl – Lock Files (flock)

The following script is an example of using lock files in Perl. This prevents more than one user or process accessing a file at a time.

[perl]
use Fcntl qw(:DEFAULT :flock);
use Fcntl ‘:flock’;¬†¬†¬†¬† #LOCK_* constants

my($lockneedsremoving)=0;
my($lockfilename)=”/var/.pwd.lock”;

unless (createlock()) {
print “Lock has been created”;
print “This is only run if their was no lock file present”;
}

releaselock();

sub releaselock
{
# Only remove the lockfile if we created it.
if ( $lockneedsremoving  )
{
writelog (“Removing sudo lock”);
-f “${lockfilename}” and unlink “${lockfilename}”;

}
undef $lockneedsremoving;
}

sub createlock
{
sysopen(LOCKFILE, “${lockfilename}”, O_RDWR|O_CREAT) or print (“Cannot open lockfile ${lockfilename}: $!”);
unless (flock(LOCKFILE, LOCK_EX|LOCK_NB))
{
print “Another update is in place.¬† Please try in a minute…”;
return 0;
}

$lockneedsremoving=1;
return 1;
}

[/perl]

flockLock FilesPerlPerl Lock Files
VNC server on Unix
SCCS – script header

Recent Posts

  • DevOps as a Software Development Methodology
  • How Does Spiral Software Development Methodology Work?
  • Waterfall Software Development Methodology And When To Use It
  • What Exactly Is Lean Software Development Methodology?
  • What is Kanban and How Does It Work?

PodTech IO

The UK's #1 Software Development Company. PodTech IO builds custom software solutions for businesses. Call today for a free consultation

Company

  • About
  • Services
  • Contact
  • Blogs
  • Career

Terms

  • Terms & Conditions
  • Privacy Policy

Contact Us

  • ADDRESS

    Headquarter: Tabernacle Street London India: Vi john Tower, Gurgaon, Haryana
  • EMAIL

    info@podtech.com
  • CONTACT

    +44 (0) 20 8720 6583

Copyright ©2023 PodTech IO. All Rights Reserved

Logo