Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associate a file with a process manually

I have a simple locking mechanism where a process will win a lock, if it is able to call mkdir on a certain path x.

Want I want to do is have other processes be able to search for which process owns the lock.

One way to do that, would be to use ps and find which processes have open files - so what I want to do is associate x with the process that created it. How can I do that?

For example, can I read the file, and determine which process id was responsible for creating it?

like image 255
Alexander Mills Avatar asked Aug 21 '26 20:08

Alexander Mills


1 Answers

lsof /path/to/directory can tell which process has the file currently locked. Checking the FD column in the output:

u - file is open for reading and writing

w - file is open for write

r - file is open for read.

But anyway it shows which process has currently locked the file at a specified time, but you can't tell which process did create the file/dir. As a solution, you can name the directory with the "PID" extension, i.e.:

mkdir /path/to/somedir/yourdir$$

or

mkdir /path/to/somedir/yourdir & echo $! > /path/to/pidfile

For more detailed logs about file operations, loggedfs may help. It allows to logs every operation in a filesystem (I belive it logs the PID that created the file).

like image 52
Ardit Avatar answered Aug 24 '26 12:08

Ardit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!