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

What is the scope of flock()?

I'm testing if locking a file descriptor from an other thread will affect the main thread. #include #include #include #include #include #include //#include #include…
Sanich
  • 1,739
  • 6
  • 25
  • 43
3
votes
2 answers

flock() doesn't preventing other process to get exclusive lock

I wrote a simple code to test the flock(): int main (int argc, char * const argv[]) { int fd1; if (fd1 = open( "file1", O_RDWR | O_CREAT | O_TRUNC) == -1) { perror("Cannot open file 1.\n"); fflush(stdout); } …
Sanich
  • 1,739
  • 6
  • 25
  • 43
3
votes
1 answer

flock locking order?

im using a simple test script from http://www.tuxradar.com/practicalphp/8/11/0 like this
John Doe
  • 2,746
  • 2
  • 35
  • 50
3
votes
1 answer

How does PHP handle two processes with the same exclusive lock?

It appears that when php uses flock() to acquire an exclusive lock to a file, and then uses pcntl_fork to split into two processes, both processes have the same exclusive lock - that is to say, further calls to flock($fd, LOCK_EX) on that file…
Benubird
  • 18,551
  • 27
  • 90
  • 141
3
votes
1 answer

What does 200>"$somefile" accomplish?

I've found boilerplate flock(1) code which looks promising. Now I want to understand the components before blindly using it. Seems like these functions are using the third form of flock flock [-sxun] [-w timeout] fd The third form is convenient…
quickshiftin
  • 66,362
  • 10
  • 68
  • 89
3
votes
1 answer

When would a blocking call to flock fail?

I was reading about flock. The example has this line: flock($fh, LOCK_EX) or die "Cannot lock mailbox - $!\n"; This call is a blocking call right? So if the lock is already taken the call blocks. I assume that if the call returns the lock is…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
3
votes
1 answer

How to lock a directory for exclusive access in PHP on Windows?

I'm using the flock() method to get an advisory lock on a directory, and prevent another process from accessing the same directory at the same time. Because flock() operates on a file handle, I have to open a handle on the directory…
BenMorel
  • 34,448
  • 50
  • 182
  • 322
3
votes
1 answer

Bash: How to lock also when perform an outside script

Here is my bash code: ( flock -n -e 200 || (echo "This script is currently being run" && exit 1) sleep 10 ...Call some functions which is written in another script... sleep 5 ) 200>/tmp/blah.lockfile I'm running the script from two…
Subway
  • 5,286
  • 11
  • 48
  • 59
3
votes
2 answers

PHP flock() non-blocking still block why?

I'm using flock() function to check if another instance of the script is already running by obtaining the lock on a temporary file so next instance should check if the file is not locked otherwise it stops
Atef
  • 593
  • 1
  • 8
  • 18
3
votes
0 answers

Is PHP flock safe in multithreaded mode on linux?

The PHP flock documentation says it is unsafe on some multithreaded servers like ISAPI, because the lock is a process wide thing, so if one instance of php on thread 1 locks a file another php instance on thread 2 may also lock it because from the…
JanKanis
  • 6,346
  • 5
  • 38
  • 42
3
votes
2 answers

Perl: flock() works on Linux, ignores previous lock on AIX

In a nutshell: wrote a Perl script using flock(). On Linux, it behaves as expected. On AIX, flock() always returns 1, even though another instance of the script, using flock(), should be holding an exclusive lock on the lockfile. We ship a Bash…
Thomas Heywood
  • 903
  • 2
  • 9
  • 18
3
votes
2 answers

Writing to same file at the very same time

Ok, I know that there's been similar questions on this site about this problem, but none of this questions and provided answers isn't exactly what I need. I'm building flat-file based CMS. What if, for example: 2, 3, 10..... fwrite in appending…
Miloš Đakonović
  • 3,751
  • 5
  • 35
  • 55
2
votes
2 answers

PHP Accessing a file that has been locked for editing - Permission Denied error

I'm doing some csv parsing, and if I cancel the parsing (by navigating to a different page or refreshing) before the fclose() function gets called, I get the below error when I go back and start up the program again: Warning: unlink(spreadsheet.csv)…
user954912
  • 243
  • 1
  • 5
  • 12
2
votes
0 answers

What happens when a process has exclusive lock on a file and another process tries to write on it?

I'm trying to check what happens if a process has an exclusive lock on a file and another process try to write on this file. Will it fail or just block until the lock is lifted ? To get an answer, I tried write something but it doesn't seem to be…
Rowooz
  • 31
  • 3
2
votes
1 answer

how do I flock in perl

If I run the following script in parallel I could see few are waiting for acquiring lock but some are running in parallel. LOCK_EX is not working as expected. I don't understand what is missing here. $|++; my $lockHandle; my…