Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7640699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:44:09+00:00 2026-05-31T08:44:09+00:00

I need to find who locked a file using python (posix/linux). Currently I use

  • 0

I need to find who locked a file using python (posix/linux). Currently I use this method:

flk = struct.pack('hhqql', fcntl.F_WRLCK, 0, 0, 0, 0)
flk = struct.unpack('hhqql', fcntl.fcntl(self.__file, fcntl.F_GETLK , flk))

if flk[0] == fcntl.F_UNLCK:
    # file is unlocked ...
else:
    pid = flk[4]

This solution is not architecture-independent. Structure passed to fcntl contains fields such as off_t or pid_t. I cannot make assumptions about sizes of those types.

struct flock {
    ...
    short l_type;    /* Type of lock: F_RDLCK,
                    F_WRLCK, F_UNLCK */
    short l_whence;  /* How to interpret l_start:
                    SEEK_SET, SEEK_CUR, SEEK_END */
    off_t l_start;   /* Starting offset for lock */
    off_t l_len;     /* Number of bytes to lock */
    pid_t l_pid;     /* PID of process blocking our lock
                    (F_GETLK only) */
    ...
};

Is there any other way to find the PID? Or maybe sizes of off_t and pid_t? The solution must be fully portable between different architectures.

Edit
I decided to use lsof program as suggested below. Another option is to parse /proc/locks file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:44:10+00:00Added an answer on May 31, 2026 at 8:44 am

    Maybe you can try use external program, lsof, to do that?

       Lsof revision 4.85 lists on its standard output file information 
       about files opened by processes for the following UNIX dialects:
    
            AIX 5.3
            Apple Darwin 9 and Mac OS X 10.[56]
            FreeBSD 4.9 and 6.4 for x86-based systems
            FreeBSD 8.[02] and 9.0 for AMD64-based systems
            Linux 2.1.72 and above for x86-based systems
            Solaris 9, 10 and 11
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find out who created a file using .NET I have already
This is using phpMyAdmin. I need to find the contact information for Subscribers who
Using a ruby file (or any rake facility) I need to find out if
Everywhere I see, can't find any info about it. I need that people who
I need to find 2 elegant complete implementations of public static DateTime AddBusinessDays(this DateTime
I need to find out the user who has posted the most number of
I need to find the screen resolution of a users screen who visits my
I need to find the person who has the maximum items in a list
I need to find the user name of the person who modified a particular
Using MEF, from inside the exporter, is it possible to find out who is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.