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

Deadlock with flock, fork and terminating parent process

I have a pretty complicated python program. Internally it has a logging system that uses an exclusive (LOCK_EX) fcntl.flock to manage global locking. Effectively, whenever a log message is dumped, the global file lock is acquired, message is…
UsAaR33
  • 3,536
  • 2
  • 34
  • 55
7
votes
1 answer

How to keep linux flock(2) from starving exclusive lock requests?

I am using flock(2) in linux to control access to resources in a homespun database, using both shared and exclusive locking modes. I find that if a shared lock is granted, then another process can also get a shared lock, regardless of whether there…
2wav
  • 148
  • 6
7
votes
2 answers

LOCK_NB Ignored

running this code twice : $fp = @fopen('test.test', "wb"); if (flock($fp, LOCK_NB | LOCK_EX)){ @fwrite($fp, $data); echo 'written'; sleep(5); }else{ echo 'skipped , ok'; } …
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
7
votes
1 answer

Does php's file_get_contents ignore file locking?

I have read php's manual page on the 'file_get_contents' function which does not state anything about how 'file_get_contents` behaves with respect to php's file locking. However in the comment section user Chris suggests that file_get_contents does…
humanityANDpeace
  • 4,350
  • 3
  • 37
  • 63
7
votes
2 answers

Does flock understand && command for multiple bash commands?

I am using bash and flock on centos. Normally I would run cd /to/my/dir && python3.6 runcommand.py But then we add it to cron and don't want output so add > /dev/null 2>&1 And add a flock before it to prevent multiple instances, like so: flock -n…
snh_nl
  • 2,877
  • 6
  • 32
  • 62
7
votes
7 answers

How to write to file in large php application(multiple questions)

What is the best way to write to files in a large php application. Lets say there are lots of writes needed per second. How is the best way to go about this. Could I just open the file and append the data. Or should i open, lock, write and unlock.…
Saif Bechan
  • 16,551
  • 23
  • 83
  • 125
7
votes
2 answers

php flock and fread and fwrite

I see people using flock like this: if (!$fp = fopen($file_name, 'wb')) { return FALSE; } if (flock($fp, LOCK_EX)) { fwrite($fp, serialize($data)); flock($fp, LOCK_UN); } Also this: if (!$fp = @fopen($file_name, 'rb')) …
qwertymk
  • 34,200
  • 28
  • 121
  • 184
6
votes
4 answers

flock-ing a C++ ifstream on Linux (GCC 4.6)

context I'm slowly writing a specialized web server application in C++ (using the C onion http server library and the JSONCPP library for JSON serialization, if that matters)., for a Linux system with GCC 4.6 compiler (I don't care about portability…
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
6
votes
4 answers

Multiple users write to the same file at the same time using PHP

I have a website where at the same moment, there can be multiple users writing to the same file at the same time. An example of my code is below. PHP 5.6.20
John Colbir
  • 65
  • 2
  • 5
6
votes
1 answer

two processes write to one file, prevent mixing the output

I want to get output from two processes and merge them into one file, like: proc1 >> output & proc2 >> output & The problem is that output may be mixed up in the final file. For example if first process writes: hellow and the second process…
ayyoob imani
  • 639
  • 7
  • 16
6
votes
2 answers

C: How to add timeout on flock?

In C programming, is there a way to achieve timeout on flock()? Thanks. #include int flock(int fd, int operation);
user180574
  • 5,681
  • 13
  • 53
  • 94
6
votes
2 answers

How to delete a locked (flock) file without race condition: before or after releasing the lock?

I am implementing a mutual exclusion mechanism based on a file lock. Other instances of my script know that they are not supposed to run when they come accross a specific file, which is locked. In order to achieve this, I have created and locked the…
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
6
votes
1 answer

Can Linux flock(fd, LOCK_EX|LOCK_NB) fail spuriously?

Consider a situation where two processes make concurrent attempts at placing an exclusive lock on some file using flock(fd, LOCK_EX|LOCK_NB). As indicated, the attempts are non-blocking, so one of the two processes is supposed to fail with…
Kristian Spangsege
  • 2,903
  • 1
  • 20
  • 43
6
votes
2 answers

How does the 'Flock' iOS app detect when photos have been taken?

Flock is a reasonably new iOS app from the guys at Bump, which has an interesting feature. It somehow knows when photos have been taken by another app, notifies the user (in a notification center way), and asks the user to share them into an album.…
mblackwell8
  • 3,065
  • 3
  • 21
  • 23
6
votes
5 answers

How can I synchronize access to a file within a single process in node.js?

I have a Web server that reads and writes to a data file on disk. I'd like a file only be written to in a single Web request. Here's an example program that illustrates my problem. It keeps a state file in "/tmp/rw.txt" and increments the integer…
Evan P.
  • 979
  • 1
  • 10
  • 17
1 2
3
19 20