LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-23-2014, 07:27 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 151

Rep: Reputation: 15
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
 
Old 07-23-2014, 07:47 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,106

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
man lsof.
 
Old 07-23-2014, 08:06 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
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).

Last edited by jpollard; 07-23-2014 at 08:12 AM.
 
Old 07-23-2014, 08:19 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,203

Rep: Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675
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
 
Old 07-23-2014, 08:45 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
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.

Last edited by jpollard; 07-23-2014 at 08:50 AM.
 
Old 07-24-2014, 04:42 AM   #6
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 151

Original Poster
Rep: Reputation: 15
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

Last edited by nagendrar; 07-24-2014 at 06:14 AM.
 
Old 07-24-2014, 05:52 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
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.
 
Old 07-24-2014, 06:13 AM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
Quote:
Originally Posted by nagendrar View Post
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.
 
Old 07-24-2014, 06:22 AM   #9
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 151

Original Poster
Rep: Reputation: 15
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
 
Old 07-24-2014, 06:25 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,203

Rep: Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675
if it is deleted but still displayed means it is opened somewhere (otherwise it could have been already really removed).
 
Old 07-24-2014, 06:58 AM   #11
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 151

Original Poster
Rep: Reputation: 15
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
 
Old 07-24-2014, 07:02 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,203

Rep: Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675
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.
 
Old 07-24-2014, 07:48 AM   #13
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 151

Original Poster
Rep: Reputation: 15
Is there any C++ API related to this? Please help me.

-regards
Nagendra Rednam
 
Old 07-24-2014, 09:01 AM   #14
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,688

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
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.

Last edited by jpollard; 07-24-2014 at 09:17 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial port becomes unreachable after being opened and closed once. prushik Programming 10 09-25-2009 09:17 AM
Is There any API in linux to know process ID and it's status sivareddy_jntu Linux - General 6 08-21-2009 06:19 PM
User session opened and closed msg in /var/log/messages andiramesh Linux - Newbie 9 09-05-2008 05:47 AM
Accessing shared memory opened in a windows process in a connected linux pc gonzalesmico Programming 3 06-07-2007 02:31 PM
using a socket descriptor opened by another process sudheernair Programming 0 04-16-2004 06:29 AM


All times are GMT -5. The time now is 01:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration