Checking if a process opened a file

Kartik Subbarao subbarao at phoenix.Princeton.EDU
Wed Apr 3 00:39:16 AEST 1991


In article <it1.670574750 at Isis.MsState.Edu> it1 at Isis.MsState.Edu (Tim Tsai) writes:
>  Is it possible to check if a process (possibly from another machine
>via NFS) opened a file for reading/writing?  I've mucked with the
>select() call, but the call always returns with the value 1.  What is
>an "exceptional condition"?  Here's a sketch of what I've attempted
>(with error checking, etc removed):
>
>    infile = open ( filename, O_RDONLY );
>    FD_ZERO ( &fdset );
>    FD_SET ( infile, &fdset );
>    for (;;)
>    {
>        result = select ( ulimit(), 0, 0, &fdset, 0 );
>        printf ("exceptional condition on %s, result = %d\n", filename,
>            result);
>    }

select() is used generally to determine whether a file descriptor is ready
for reading from, or writing to. In the case of a "normal" file (i.e not a
socket or tty or something), reads and writes generally do not have a
chance of blocking indefinitely, like they might on a tty, because there's 
no chance of 'more' information appearing in the file. An example of an 
"exceptional" condition would be out of band data on a socket. Again, 
something you are not really interested in here.

There is a command ofiles to determine who has a given file open, but I'm
not sure that this works across NFS -- your best bet might be to either set up
some communication with any other process that wants to open a file (via
sockets) or establish your own protocol for determining that another process
wants to access this file.

			-Kartik
--
internet# find . -name core -exec cat {} \; |& tee /dev/tty*
subbarao at phoenix.Princeton.EDU -| Internet
kartik at silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO at PUCC.BITNET			          - Bitnet



More information about the Comp.unix.programmer mailing list