Questions tagged [file-locking]

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

372 questions
970
votes
20 answers

Is there a way to check if a file is in use?

I'm writing a program in C# that needs to repeatedly access 1 image file. Most of the time it works, but if my computer's running fast, it will try to access the file before it's been saved back to the filesystem and throw an error: "File in use by…
Dawsy
  • 10,249
  • 5
  • 20
  • 13
212
votes
15 answers

Locking a file in Python

I need to lock a file for writing in Python. It will be accessed from multiple Python processes at once. I have found some solutions online, but most fail for my purposes as they are often only Unix based or Windows based.
Evan Fosmark
  • 98,895
  • 36
  • 105
  • 117
189
votes
18 answers

How do I find out which process is locking a file using .NET?

I've seen several of answers about using Handle or Process Monitor, but I would like to be able to find out in my own code (C#) which process is locking a file. I have a nasty feeling that I'm going to have to spelunk around in the win32 API, but if…
AJ.
  • 13,461
  • 19
  • 51
  • 63
145
votes
1 answer

Notepad beats them all?

On a Windows Server 2012 R2 system, a Kotlin program uses FileChannel.tryLock() to hold an exclusive lock on a file, like this: val fileRw = RandomAccessFile(file, "rw") fileRw.channel.tryLock() With this lock in place, I cannot open the file…
MonoThreaded
  • 11,429
  • 12
  • 71
  • 102
78
votes
4 answers

How to prevent file locking when undeploying a Tomcat web app?

I am using the manager app in tomcat 7, and i am unable to undeploy an application completely. It says FAIL - Unable to delete [F:\apache-tomcat-7.0.33\webapps\balaji]. The continued presence of this file may cause problems. I read somewhere its…
bali208
  • 2,257
  • 7
  • 40
  • 43
74
votes
2 answers

Reading a file used by another process

I am monitoring a text file that is being written to by a server program. Every time the file is changed the content will be outputted to a window in my program. The problem is that I can't use the Streamreader on the file as it is being used by…
Christoffer
  • 7,470
  • 9
  • 39
  • 55
58
votes
9 answers

How do I delete a file which is locked by another process in C#?

I'm looking for a way to delete a file which is locked by another process using C#. I suspect the method must be able to find which process is locking the file (perhaps by tracking the handles, although I'm not sure how to do this in C#) then close…
Dean Bates
  • 1,927
  • 5
  • 25
  • 24
48
votes
7 answers

Open in Java(TM) Platform SE binary

I tried to delete a file that I have two of, one slightly changed, so I could delete the older one and replace it with the new one I changed. When I tried to delete the file I got the error message 'file in use' where it said the action can't be…
user1642596
  • 481
  • 1
  • 4
  • 3
40
votes
8 answers

Mac OS X equivalent of Linux flock(1) command

Is there a flock command on Mac OS X that manages file lock? http://linux.die.net/man/1/flock
png
  • 5,990
  • 2
  • 25
  • 16
28
votes
1 answer

Linux flock, how to "just" lock a file?

In Bash, I'm trying to make a function getLock to be used with different lock names. function getLock { getLock_FILE="${1}" getLock_OP="${2}" case "${getLock_OP}" in "LOCK_UN") flock -u "${getLock_FILE}" …
JorgeeFG
  • 5,651
  • 12
  • 59
  • 92
27
votes
3 answers

Is a move operation in Unix atomic?

Suppose there are 2 processes P1 and P2, and they access a shared file Foo.txt. Suppose P2 is reading from Foo.txt. I don't want P1 to write to Foo.txt while P2 is reading it. So I thought I could make P1 write to Foo.tmp and as a last step,…
Chaos
  • 11,213
  • 14
  • 42
  • 69
23
votes
3 answers

Java FileLock for Reading and Writing

I have a process that will be called rather frequently from cron to read a file that has certain move related commands in it. My process needs to read and write to this data file - and keep it locked to prevent other processes from touching it…
bobtheowl2
  • 641
  • 2
  • 10
  • 19
22
votes
2 answers

File.Copy locks source file after completion

We are trying to copy a file from a server, down to a local machine in a .NET 2.0 application (C#), but keep having the source file unnecessarily locked. We suspect it is something configured on the file server that is causing this behaviour, but…
Jayden
  • 2,656
  • 2
  • 26
  • 31
18
votes
1 answer

python lockf and flock behaviour

I have read enough posts on stackoverflow regarding the difference between flock/lockf/fcntl but I am unable to answer the below observation: >>> import fcntl >>> a = open('/tmp/locktest', 'w') >>> b = open('/tmp/locktest', 'w') >>> fcntl.lockf(a,…
Jatin Kumar
  • 2,635
  • 9
  • 36
  • 46
18
votes
6 answers

Lock file exclusively then delete/move it

I'm implementing a class in C# that is supposed to monitor a directory, process the files as they are dropped then delete (or move) the processed file as soon as processing is complete. Since there can be multiple threads running this code, the…
GDemartini
  • 341
  • 1
  • 3
  • 6
1
2 3
24 25