LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   API to identify whether file is opened or closed or used by other process in Linux (https://www.linuxquestions.org/questions/linux-newbie-8/api-to-identify-whether-file-is-opened-or-closed-or-used-by-other-process-in-linux-4175512014/)

nagendrar 07-23-2014 07:27 AM

API to identify whether file is opened or closed or used by other process in Linux
 
I have two processes. One process is writing the file and another process is rename it to some other directory.

Here my problem is,

If second process is tried to rename the file if is in write mode(by first process) then it is successfully renaming in Linux but failed in windows.

How can I identify by second process about file (whether file is opened or closed or used by other process) in Linux using C++ API?

Please help me to solve this problem.


-regards
Nagendra

schneidz 07-23-2014 07:47 AM

man lsof.

jpollard 07-23-2014 08:06 AM

What is the problem you are trying to solve?

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).

pan64 07-23-2014 08:19 AM

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

jpollard 07-23-2014 08:45 AM

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.

nagendrar 07-24-2014 04:42 AM

File is removing successfully If i try to remove a file which is in writing mode in Linux.

Here I want the solution as,
Would fail to remove the file If I try to remove a file which is in writing mode in Linux.


-regards
Nagendra Rednam

chrism01 07-24-2014 05:52 AM

Actually the file isn't physically removed until no processes have it open.
As above, check the /proc and/or look at the src for lsof, fuser.

jpollard 07-24-2014 06:13 AM

Quote:

Originally Posted by nagendrar (Post 5208812)
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.

nagendrar 07-24-2014 06:22 AM

File is removed physically. But showing as 'deleted' with lsof command.

Here my problem is,

How would I know whether file is in open mode or not?

Please help me to solve this problem.

-regards
Nagendra Rednam

pan64 07-24-2014 06:25 AM

if it is deleted but still displayed means it is opened somewhere (otherwise it could have been already really removed).

nagendrar 07-24-2014 06:58 AM

Here My intention is ,

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.

Please help me to get the data about file status.

-regards
Nagendra Rednam

pan64 07-24-2014 07:02 AM

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.

nagendrar 07-24-2014 07:48 AM

Is there any C++ API related to this? Please help me.

-regards
Nagendra Rednam

jpollard 07-24-2014 09:01 AM

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.


All times are GMT -5. The time now is 02:34 PM.