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
14
votes
3 answers

How do I atomically create a locked file in Linux?

Scenario: I have many processes running that need to fetch files over the net. If the file is already downloaded, I want it cached on disk. If another process is downloading the file, block until it is finished downloading. I've been trying to find…
UsAaR33
  • 3,536
  • 2
  • 34
  • 55
13
votes
3 answers

How do I use the linux flock command to prevent another root process from deleting a file?

I would like to prevent one of my root processes from deleting a certain file. So I came across the flock command, it seems to fit my need, but I didn't get its syntax. If I only indicate a shared lock, it doesn't work: flock -s "./file.xml" If I…
Danmaxis
  • 1,710
  • 4
  • 17
  • 27
12
votes
2 answers

multiple threads able to get flock at the same time

I was under the impression that flock(2) is thread safe, I recently, ran across the case in the code, where multiple threads are able to get a lock on the same file which are all synchronized with the use of obtaining exclusive lock using the c api…
user1235176
  • 121
  • 1
  • 1
  • 4
12
votes
2 answers

call flock with node.js?

I have cron job to run node.js scripts. Want to use flock to lock a file to make sure my cron jobs are not overlapped. Any good module for doing file locking ? Or I should call that in child process ? Or I should not do any file locking ? Sorry, I…
Eric Fong
  • 815
  • 1
  • 8
  • 17
12
votes
3 answers

Docker and file locking

I wrote a simple go application and added a flock system to prevent being running twice at the same time: import "github.com/nightlyone/lockfile" lock, err := lockfile.New(filepath.Join(os.TempDir(), "pagerduty-read-api.lock")) if err != nil { …
Soullivaneuh
  • 3,553
  • 3
  • 37
  • 71
12
votes
3 answers

Test if file is locked

In PHP, how can I test if a file has already been locked with flock? For example, if another running script has called the following: $fp = fopen('thefile.txt', 'w'); flock($fp, LOCK_EX);
Jodes
  • 14,118
  • 26
  • 97
  • 156
11
votes
1 answer

How can I make a non-blocking request for an exclusive lock using File#flock?

How Should I Request a Non-Blocking Lock? Why doesn't Ruby's File#flock work as expected when separate attempts are made to lock a file? Locking the file in a block is not the correct solution for this issue because the point is to show the behavior…
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
10
votes
3 answers

flock(): is it possible to merely check if the file is already locked, without actually acquiring the lock if not?

My use case is as follows: I have a program that enforces that only one instance of it can be running at any given time, so at startup it always tries to grab hold of a lock file in a standard location, and terminates if the file is already locked.…
bgoldst
  • 34,190
  • 6
  • 38
  • 64
10
votes
3 answers

How does LOCK_SH work?

I'm studing the flock mecanism in PHP and I'm having a hard time understanding the functionality of the LOCK_SH mode. I read on a site that it locks the file so that other scripts cannot WRITE in it, but they can READ from it. However the following…
Marius Popescu
  • 274
  • 1
  • 3
  • 7
9
votes
1 answer

Difference between return value of non-blocking flock function and the $wouldblock argument?

I'm trying to understand non blocking flock and the wouldblock argument $fp = fopen('/tmp/lock.txt', 'r+'); if(flock($fp, LOCK_EX | LOCK_NB, $wouldblock)) { echo 'Lock obtained'; } else{ echo 'Unable to obtain…
nulll
  • 1,465
  • 1
  • 17
  • 28
9
votes
1 answer

Named semaphore or flock which is better C linux

I am trying to create a shared memory which will be used by multiple processes. these processes communicate with each other using MPI calls (MPI_Send, MPI_Recv). I need a mechanism to control the access of this shared memory I added a question…
nav_jan
  • 2,473
  • 4
  • 24
  • 42
8
votes
3 answers

Check if a file is already locked using flock()?

I have a file I'm writing to, but I need to lock it first (using flock()), to prevent any other script from writing to it. So I have: $file=fopen($file_p); if (flock($file, LOCK_EX)) {//lock was successful fwrite($file,$write_contents); …
user849137
8
votes
2 answers

How can I lock a directory in C on a linux machine

Will flock or lockf work on a directory? I there another way to lock a directory in C on a linux machine?
user357498
  • 139
  • 3
  • 5
8
votes
0 answers

getting notified on flock/lockf/fcntl changes without polling

Is there a way (in Linux) of getting updates on the lockedness status of a file without polling? I know that the status can be polled via a lockf(fd, F_TEST) or speculative LOCK_NB|LOCK_SH, but polling is bad(tm). Of course, finding out when a file…
user2769258
  • 584
  • 3
  • 3
8
votes
1 answer

PHP Semaphore alternative?

I'm making a small online game, where (what do you know) there'll be multiple users accessing the same database. My host doesn't enable Semaphores and I cannot really afford for something else (I'm a highschool student), so I'm looking for an…
Raekye
  • 5,081
  • 8
  • 49
  • 74
1
2
3
19 20