1833873 Members
2025 Online
110063 Solutions
New Discussion

Re: Show Open Files

 
SOLVED
Go to solution
Tom Jackson
Valued Contributor

Show Open Files

Hi:

Does anyone know how to show if a file is open from "C"? I know about the fuser command, but it executes from the command line. I need a library call from "C".

Tom
8 REPLIES 8
Ken Hubnik_2
Honored Contributor

Re: Show Open Files

If you have glance and you know the process ID you can do a F to show all open files for that process.
A. Clay Stephenson
Acclaimed Contributor

Re: Show Open Files

Look at pstat_getfile(). The other thing that I would suggest is that you download the source code for the 'lsof' utility. That should give you anything you want to know about open files.
http://gatekeep.cs.utah.edu/hppd/hpux/Sysadmin/lsof-4.64/
If it ain't broke, I can fix that.
Christopher McCray_1
Honored Contributor

Re: Show Open Files

Hello,

Try downloading and installing the lsof utility. This link has links to various sites:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xee1a6049dbb6d611abdb0090277a778c,00.html

Hope this helps

Chris
It wasn't me!!!!
Tom Jackson
Valued Contributor

Re: Show Open Files

Hi Ken:

I don't know the PID. I need to check if log files are open before launching a process. If the log files are open, I need to build a command that uses a different simulation model and logfile.

Tom
Tom Jackson
Valued Contributor

Re: Show Open Files

Hi:

I'm going to try to get the pstat_getfile() function to work. Does anyone have a code snippet?

Tom
Mike Stroyan
Honored Contributor
Solution

Re: Show Open Files

Here is an example using pstat_getfile. This one looks for any process with a strpty open. It uses statfs() and readdir() to get the file system id and inode numbers of the files in question. You could also use statfs() and stat() to get the file system id and st_ino inode number of a particular path.
Once you have the file system id and inode number you loop through every process and open file to find matches.
Vishnu_3
Advisor

Re: Show Open Files

You may be having code like this...

FILE* fp = NULL;
char *strFileName = "myfile.txt";

fp = fopen(strFileName, "r");

if (fp) {

printf("file %s is open", strFileName);

}

here if (fp) will execute only if file is open..

Vishnu.

Tom Jackson
Valued Contributor

Re: Show Open Files

Mike:

Very slick! It found the open files I was looking for!

But I did have a problem compiling. The first error message I got said I needed an ANSI compiler. So I tried my gcc. Then I got this goofy error message:
: missing '(' after predicate

So I modified the main declaration to look like this:
int main(argc, argv, envp)
int argc;
char *argv[];
char *envp[];

And I was able to cc it.

Thanks for your help!

Tom