Thread: How to determine what process(es) is(are) locking a file?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    How to determine what process(es) is(are) locking a file?

    I have a Java application that uses java.nio.channels.FileLock to obtain a lock on a file. But unfortunately the API does not provide a method to determine when the lock is unsuccessful what process(es) is(are) locking the file. So I am trying to do this using JNI and C, but I have not had any luck finding source code to do this. Does anyone know how to do this? It's only necessary to work with a Windows(XP and up) environment.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so the first question would be what platform this is implemented on.

    Since the lock is a "OS" lock, the "other" locking process may not be a Java process at all, so any information (possibly) heald by the Java code itself is pretty much useless.

    In Windows, you can use Mark Russinovich's "Process Explorer" to find which process has a particular file open [it won't tell you if the file is locked or not, AFAIK]. www.sysinternals.com is Mark's website, but it appears that the site is now run by Microsoft.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    As stated before, this is for a Windows Environment (XP/Server 2003 mostly). I can not use a third party application, because I need to be able to determine what process(es) is(are) locking the file to send a message to the user through logs. I need to know how, using C code, to determine what process(es) is(are) locking a file.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, there's no published API from Windows to "interrogate" the file-locking data, so that's not going to be easy to achieve.

    Of course, if you are in control of the file-locking [as in, it's YOUR java code], you could implement a "extension" method for the original FileLock in java, that stores [somehow, perhaps in a file?] which process/user is locking which file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I need to know how, using C code, to determine what process(es) is(are) locking a file.
    CreateToolHelp32Snapshot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. How to determine what process(es) is(are) locking a file?
    By niputaidea in forum Windows Programming
    Replies: 4
    Last Post: 12-16-2007, 05:33 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM