API to identify whether file is opened or closed or used by other process in Linux
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Are you trying to find out if the file should be locked?
There is nothing wrong with renaming a file while it is open. It happens a lot - frequently with log files. They get renamed, then the process writing the file is given a signal to recreate the file. This permits an atomic log rotation where no log entries are lost.
It is even permissible to delete the file (this is normally done for disposable scratch files - if the process terminates/aborts, the data is then discarded).
in command line you can use fuser or stat, and in c or c++ you can try to use stat (especially st_nlink of struct stat; /* number of hard links */)
Linux and windows handle it differently - would be nice to see what is your original problem, there can be another solution
I don't think the hard link is updated just because a file is open. That is reserved for other names/paths that can be associated with the inode on disk. The "inode open" link is in memory only.
fuser identifies open files by scanning the /proc filesystem. stat is only a command to format the results of the stat library function.
File is removing successfully If i tries remove a file which is in writing mode in Linux.
Here I want the solution as,
Would fail to remove the file If I tries remove a file which is in writing mode in Linux.
-regards
Nagendra Rednam
That is how it is supposed to work. This allows for disposable scratch files to be automatically cleaned up with a process exit.
Directory manipulations are independent of inode operations (reading/writing). Deleting a file does nothing but remove a link count AND removal from the designated directory. If a file is open, the in memory version of the inode is maintained. The file is not deleted until that inode is deallocated (file is closed) and the link count is zero.
I would check for file status(whether it is in open mode for reading/writing or not) before removing, If it is in open mode then I wont try for remove otherwise I will remove it.
So I would like to know about file (whether it is in open mode or not) before remove.
fuser will tell you if a file is opened. But in linux you can safely remove that file, you need not check that state, it will be handled automatically by the system.
Not directly no. The reason there isn't is that fuser has to scan the entire process list in /proc to see if one of the processes has the file...
You CAN use file locks for that purpose though - if the application that opens the file establishes an exclusive lock on it, you can then use the lock semantics to see if another application has an exclusive lock. If it does, then the C++ application can just not delete the file. If it successfully gets the lock, then delete the file.
Such locks are automatically removed when the file is closed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.