Questions tagged [file-locking]

The concept of locking a file to serialize concurrent accesses to said file.

372 questions
7
votes
2 answers

How to find out which thread is locking a file in java?

I'm trying to delete a file that another thread within my program has previously worked with. I'm unable to delete the file but I'm not sure how to figure out which thread may be using the file. So how do I find out which thread is locking the file…
tomdee
  • 2,319
  • 5
  • 25
  • 39
7
votes
5 answers

PHP lock text file for editing?

I've got a form that writes its input to a textfile. Would it be possible to lock a text file for editing, and perhaps give a friendly message "the file is edited by another user, please try again later." I'd like to avoid conflicts if the file has…
Janne
  • 67
  • 1
  • 4
7
votes
4 answers

Create file in a thread-safe manner

I have an array of filenames and each process need to create and write only to a single file. This is what I came to: foreach ($filenames as $VMidFile) { if (file_exists($VMidFile)) { // A continue; } $fp = fopen($VMidFile,…
zerkms
  • 249,484
  • 69
  • 436
  • 539
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
1 answer

Python : Locking text file on NFS

I have a file results.txt on a server which is accessed by multiple VMs through NFS. A process runs on each of these VMs which reads the results.txt file and modifies it. If two processes, A and B, read the file at the same time, then modification…
gaganso
  • 2,914
  • 2
  • 26
  • 43
6
votes
3 answers

Parallel writing to a file from multiple processes by using echo

I have written a function in our ERP-System which writes a log to a file on the server by simply "echoing" it to the logfile. echo "SOME LOGDATA" >> /users/erp/log/LOGMSG Every time a user triggers a specific event, the LOG function is called. What…
Alexander Baltasar
  • 1,044
  • 1
  • 12
  • 25
6
votes
1 answer

Get username of user who has file open on network drive - Microsoft Office Style

I would like to add user friendly file locking to a software running under Windows (Windows 7 mostly), written in C#. I already achieved the file locking part, by keeping the files in use "open" in the corresponding process. What I now still would…
lorrrris
  • 63
  • 1
  • 5
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
3 answers

JVM crash because of lock on nfs file after network outage

Following code snippet causes JVM crash: if network outage occurs after acquiring lock while (true) { //file shared over nfs String filename = "/home/amit/mount/lock/aLock.txt"; RandomAccessFile file = new…
Amit G
  • 2,293
  • 3
  • 24
  • 44
6
votes
2 answers

Python: Opening a file without creating a lock

I'm trying to create a script in Python to back up some files. But, these files could be renamed or deleted at any time. I don't want my script to prevent that by locking the file; the file should be able to still be deleted at any time during the…
SilentSteel
  • 2,344
  • 1
  • 28
  • 28
5
votes
1 answer

How to avoid file deadlocks when PHP process/server crashes?

I am new to PHP. I understand I can use flock() to lock a file and avoid race conditions when two users reach the same php file adding content to the lockable file. However, what happens if a php process crashes? What happens to the next user…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
5
votes
2 answers

Java - locking a file for exclusive access

My problem is this: I'm using a WatchService to get notified about new files in a specific folder, now if a file gets moved/copied or created in said folder an event gets triggered and the name of the new file gets returned. The problem now is, if I…
icefex
  • 523
  • 1
  • 7
  • 14
5
votes
4 answers

File.Open for read access denied on executing file in Windows

I have a problem with file permissions on executing files in Windows that seems to be solved after following a forum tip[1], but I cannot understand why. Maybe you guys can help. I'm checking the banner of a file by executing it (reading the console…
Vegar Westerlund
  • 1,604
  • 3
  • 16
  • 24
5
votes
4 answers

What does _locking() really do?

Looking for answer of this question I found function _locking(). There tells that it Locks or unlocks bytes of a file (actually I can't understand what does this sentence really mean). If someone have experience of using of this function, is it…
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
5
votes
3 answers

Using java file locks within single JVM and across multiple JVMs

I guess I miss something, but I cannot understand how file locks work in Java. To be more exact - how it is implemented. It seems I cannot acquire (even cannot attempt acquiring) two or more locks for the same file inside single JVM. First lock will…
Yakov
  • 126
  • 1
  • 1
  • 6
1 2
3
24 25