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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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
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.
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.
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.