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
0
votes
1 answer

How best to implement atomic update on a file inside a bash script

I have a script which has multiple functions, running in parallel which checks a file and updates it frequently. I dont want two functions to update the file at the same time and create an issue. So what will be the best way to have an atomic…
Amar C
  • 374
  • 5
  • 17
0
votes
1 answer

PHP LOCK_EX+LOCK_NB on a file

Can we have LOCK_EX+LOCK_NB on a file? Is this syntactically correct? If yes how is it different from LOCK_EX|LOCK_NB $fh = fopen('guestbook.txt','a') or die($php_errormsg); flock($fh,LOCK_EX+LOCK_NB) or die($php_errormsg); Thanks
navule
  • 3,212
  • 2
  • 36
  • 54
0
votes
0 answers

PHP Visitor count with flock

I am trying to use a visitor count. I have made it so it works without flock, but are trying to make it work with flock. I don't really understand where I should use flock, but have tried implement it in a file called VisitorCount. Now I don't get…
MeMeMe
  • 1
0
votes
2 answers

Shared access: How to fix "fread(): Length parameter must be greater than 0"?

When I run this function on multiple scripts one script generated warning: fread(): Length parameter must be greater than 0 function test($n){ echo "

$n at ".time()."

"; for ($i = 0; $i<50; $i++ ){ $fp = fopen("$n.txt", "r"); $s…
Johny Bony
  • 375
  • 2
  • 9
0
votes
1 answer

Warning: flock(): "Illegal operation argument" with LOCK_NB

What am I doing wrong when I try to read data from file using flock to check if the the file lock is released to read? I got Warning: flock(): "Illegal operation argument" function SafeReadContent($file, $t = 500, $limit = 1000){ $fp =…
Johny Bony
  • 375
  • 2
  • 9
0
votes
1 answer

how to notify another process when file lock is released?

I have a small application consisting of two process. Process A downloads a file. Process B uses the downloaded file. When Process A downloads a file, it acquires file lock on it. Process B keep checking whether the file being downloaded is locked…
Subbu
  • 2,063
  • 4
  • 29
  • 42
0
votes
1 answer

In Perl script, I can open / write to/ and close a file, but I get "bad file descriptor" when I try to flock it

I can OPEN the file using the file handle, but when I try to FLOCK using the same file handle I get "bad file descriptor." my $file='/Library/WebServer/Documents/myFile.txt'; open(my $fh, '>', $file) or die "Could not open '$file' - $!"; # I DO…
0
votes
2 answers

Replace file only if not being accessed in bash

My requirement is to replace file only when it is not being accessed. I have following snippet: if [ -f file ] then while true do if [ -n "$(fuser "file")" ] then echo "file is in use..." else echo…
user1228352
  • 569
  • 6
  • 21
0
votes
0 answers

bash: fifo queue with flock

Can flock be set up to be a fifo queue instead of "whoever grabs it first" lock? For instance, if three processes are running the following code: exec 200>/var/tmp/mylock flock -e 200 # blocks until lock is obtained echo "($$) has the…
Rusty Lemur
  • 1,697
  • 1
  • 21
  • 54
0
votes
1 answer

How to write and read a file with forking in C

i'm wondering how can we write and read in a multiprocess program written in C? I tried using flock and file descriptors but it didn't work. Is there anyone to help me? Thanks. int f; f = open("test.txt",O_RDWR |…
sarah
  • 1
  • 1
0
votes
0 answers

How to rename() a file in PHP that needs to remain locked while doing so?

I have a text file which multiple users will be simultaneously editing (limited to an individual line per edit, per user). I have already found a solution for the "line editing" part of the required functionality right here on StackOverflow.com,…
0
votes
1 answer

Why is this file locking function failing?

I wrote the following code for a generic function to acquire a file-based lock in a class in a php script that is run very often. private static $flocks = []; public static function getLock($fname) { $fp = null; try { $fp =…
beppe9000
  • 1,056
  • 1
  • 13
  • 28
0
votes
1 answer

How to make agent flock in their group using BOIDS rule in Netlogo?

Here is what I want to do: I need all agents move forward in a group by using BOIDS rule I want them to be able to turn around after they meet the wall (my world is not wrap, they can't penetrate the wall to reach the other side of the world) here…
linda
  • 117
  • 9
0
votes
0 answers

Fail of bash script with flock

I have a task: Calling a script by curl every ten seconds. For that I do: Create bash-script: admin_cron.sh #!/bin/bash while :; do sleep 10 flock -n /var/www/admin/data/cron_lock -c \ 'curl -m 3…
Melbis
  • 87
  • 10
0
votes
1 answer

flock always returns true

If I call this script
Adam
  • 25,960
  • 22
  • 158
  • 247