Questions tagged [flock]

Shell: The `flock` utility manages locks in scripts; C programming: flock() applies or removes an advisory lock on an open file.

flock is a command line utility to manage flock advisory locks - i.e., they're a convention instead of a hard restriction.

298 questions
6
votes
1 answer

How to unlock using lockf()?

I want to read from txt file and append the next number in it, I want to use fork as well to work as a second process. In following code, I need help to unlock the file. I am unable to unlock the file. int main() { int x; pid_t child = fork(); …
user1542921
  • 61
  • 1
  • 2
6
votes
4 answers

PHP flock() - what's under the hood?

After wrestling with PHP source for a half an hour, I gave up. :P The question is - what system call does the PHP flock() function call boil down to on a Gentoo Linux system? I'm having some issues with it (like…
Vilx-
  • 104,512
  • 87
  • 279
  • 422
5
votes
4 answers

Preventing deadlock caused by flock

I am trying to simulate a file writing on a busy site. I have written a following code which eventually end up freezing computer. $loop = 10000; $sleep = 500000; $i =0; while($i < $loop) { $mtime = microtime(); $mtime = explode("…
Maximus
  • 2,906
  • 4
  • 35
  • 55
5
votes
2 answers

Where is flock() for Perl on Windows?

I have a Perl script that I'd like to run on Windows, using either Strawberry Perl or ActivePerl; I don't care which. This script however, uses flock() calls, which does not seem to be included in either of those versions of Perl. Can anyone help…
gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
5
votes
3 answers

How to create a mutex method in PHP per variable value

I need to have a mutex method in PHP so that it keeps exclusivity by a variable value. This is that threads with same value should enter that method one at a time while threads with different values may access that method arbitrarily. For example,…
carlosV2
  • 1,167
  • 6
  • 15
5
votes
3 answers

flock permission denied bash

I have written a little test script to prevent running my script simultaneously with flock: #!/bin/bash scriptname=$(basename $0) lock="/var/run/${scriptname}" umask 0002 exec 200>$lock flock -n 200 || exit 1 ## The code: sleep 60 echo "Hello…
user3603714
5
votes
4 answers

How do I check the exit code of a command executed by flock?

Greetings all. I'm setting up a cron job to execute a bash script, and I'm worried that the next one may start before the previous one ends. A little googling reveals that a popular way to address this is the flock command, used in the following…
Paul Accisano
  • 1,416
  • 1
  • 14
  • 25
5
votes
1 answer

Use flock() in the sigaction handler

flock() is generally async-signal-safe because it is a system call. Its semantics make it hard to implement it differently. It is not in the POSIX's list of async-signal-safe functions because it is not in POSIX at all. Is it possible to use flock()…
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
4
votes
1 answer

How to use flock on Linux

I am looking at the flock docs: http://www.tutorialspoint.com/unix_system_calls/flock.htm https://linux.die.net/man/1/flock I have this: #!/usr/bin/env bash temp_dir="$HOME/temperton/tmp"; mkdir -p "$temp_dir" ( flock -x "$temp_dir/a" echo…
user5047085
4
votes
1 answer

Why is File::FcntlLock's l_type always "F_UNLCK" even if the file is locked?

The Perl subroutine below uses File::FcntlLock to check if a file is locked. Why does it return 0 and print /tmp/test.pid is unlocked. even if the file is locked? sub getPidOwningLock { my $filename = shift; my $fs = new File::FcntlLock; …
Saulo Silva
  • 1,219
  • 1
  • 20
  • 37
4
votes
4 answers

How can I ensure only one copy of a Perl script is running at a time?

I need to ensure that only one copy of my Perl script is running at a time. According to the suggestions here I wrote a sub to do the check: sub check_instances { open my $fh, '<', $0 or die $!; unless (flock($fh, LOCK_EX|LOCK_NB)) { …
planetp
  • 14,248
  • 20
  • 86
  • 160
4
votes
1 answer

Not sure how flock works. Need some clarification

I have a bash script that makes a cURL request and writes the output to a file called resp.txt. I need to create an exclusive lock so that I can write to that file and not worry about multiple users running the script and editing the text file at…
RattleyCooper
  • 4,997
  • 5
  • 27
  • 43
4
votes
2 answers

PHP flock() not locking

I am having trouble figuring out why flock() is not behaving properly in the following scenario. The following code is placed into two different PHP scripts one "test1.php" and the other "test2.php". The point of the code is to create a file which…
user3254100
  • 41
  • 1
  • 3
4
votes
3 answers

Attempting to redefine IO::Tee::PRINT for thread safety

Thanks to some help, I've started attempting to get thread-safe logging working for my script - however, I don't seem to have it working correctly: use Fcntl ':flock'; no warnings 'redefine'; sub IO::Tee::PRINT { my $self = shift; my $ret =…
MrDuk
  • 16,578
  • 18
  • 74
  • 133
4
votes
1 answer

Locking Files in Bash

I have a Problem to find a good concept on locking files in bash, Basically I want to achieve the following: Lock File Read in the data in the file (multiple times) Do stuff with the data. Write new stuff to the file (not necessarily to the…
mjb4
  • 989
  • 1
  • 9
  • 14