1753969 Members
7138 Online
108811 Solutions
New Discussion юеВ

Open status of a file

 
SOLVED
Go to solution
Tharangini Karthikeyan
Occasional Contributor

Open status of a file

Hi
How can we find open status of a file?Is there any structure which tells us abt the file's open status.Each time when we open a file I think some reference count is incremented (which is used by unlink) Can we access that count.In the stat structure we have a field which gives us number of hard links. some thing like that which tells how many times a file is opened.Can we access the structure struct inode which is in inode.h.Is there any field in that structure which gives me the open status of the file or the number of times the file is refered?
Please do reply for these questions.
7 REPLIES 7
Stefan Farrelly
Honored Contributor

Re: Open status of a file

lsof will do it. You can download from http://hpux.cs.utah.edu

Look at its -f or +f option, it shows file structure use count.



Im from Palmerston North, New Zealand, but somehow ended up in London...
A. Clay Stephenson
Acclaimed Contributor

Re: Open status of a file

This is probably a continuation of your earlier posting. None of the system call like stat() will do this for you. You actually have to examine kernel data structures. You do have the ability to do a pstat_getproc() to get all the processes on the systems followed by a pstat_getfile (or _getfile2) for each process. That loop will get the data for you but it is obviously a computationally expensive method.

Assuming this is an extension of your earlier question, you really should rethink the process and use advisory file locking - a much more portable solution.
If it ain't broke, I can fix that.
Tharangini Karthikeyan
Occasional Contributor

Re: Open status of a file

Hi
Thank you for replying me.
But still i am not getting the correct solution.

When I go for advisory locking only if the process are different my second lock fails,if it is of same process it cooly locks.

Why there is no any way to access the reference count of a file?


Tharangini
Klaus Crusius
Trusted Contributor

Re: Open status of a file

I think, Clay is right, when he recommends the use of file locks. You are right, if you state, that file locks cannot prevent the access from 2 spots in the same process. But that could be handled by using process-internal communication, like (global) variable.
Even if the current open count could be easyly determined, it is not clear, if the count would be correct, if the file is used twice in the same process.
It would not be relyable to use this counter anyway, because it cannot be guaranteed, that it increases after it has been inquired, and before your process accesses.
There is a live before death!
A. Clay Stephenson
Acclaimed Contributor

Re: Open status of a file

In your earlier posting, I suggested that you use a flag (either a global variable or a static local variable) to indicate that the file has already been opened.

My plan is to use the flag for the same process
AND to use lockf() as well to lock the file for other processes. That should cover you in all cases.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Open status of a file

I have thought of a method that will work and be portable.

#define STDERR_FDES 2
struct stat s1,s2;
/* Get the maximum possible fdes */
nfiles = (int) sysconf(_SC_OPEN_MAX);


fdes = open(fname,O_RDWR,0600);
cc = fstat(fdes,&s1);
i = STDERR_FDES + 1
fnd = FALSE
while (i < nfiles && !fnd)
{
if (i != fdes)
{
cc = fstat(i,&s2);
if (cc == 0) /* valid fdes */
{
fnd = ((s1.st_dev == s2.st_dev) &&
(s1.st_ino == s2.st_ino));
}
}
if (!fnd) ++i;
}
if (fnd)
{
printf("File was already open\n");
(void) close(fdes);
}
else
{
printf("File was not opened\n");
}

The idea is that we determine the maximum possible fdes and loop thru the valid fdes's
looking for match device and inode numbers. If we find them, the file is already open.


I still don't know why a flag can't be used but the above method will work.
If it ain't broke, I can fix that.
Tharangini Karthikeyan
Occasional Contributor

Re: Open status of a file

Hi
Thank u every one for replying me.

Clay,
actually I am not interested in using flags .That's why I was not convinced with that solution.I'll go ahead with ur last solution.Even I had the same solution.

Thank u .

Tharangini