Tell if another process open a file

Tell if another process open a file

Post by Jzho » Fri, 23 Mar 2001 13:27:09



Process A opens file F for read. Process B will open file F for read. Is
there a system call which will tell me A is opening F before B opens F?

Thanks.

 
 
 

Tell if another process open a file

Post by Logan Sh » Fri, 23 Mar 2001 13:52:41




>Process A opens file F for read. Process B will open file F for read. Is
>there a system call which will tell me A is opening F before B opens F?

You can have A lock the file.  Then you can use fcntl (..., F_GETLK, ...)
to determine whether the first has locked it, although it would probably
be better just to try to lock it (in order to avoid race conditions).

Of course, I'm not sure why you care why both files are opening
something for reading...

  - Logan
--
whose?  my  your   his  her   our   their   _its_
who's?  I'm you're he's she's we're they're _it's_

 
 
 

Tell if another process open a file

Post by Jzho » Fri, 23 Mar 2001 14:02:54


If B try to lock the file F, B will always succeed since both A and B are
read the file F. If A writes to F, B won't get the lock. Am I right on
this?




> >Process A opens file F for read. Process B will open file F for read. Is
> >there a system call which will tell me A is opening F before B opens F?

> You can have A lock the file.  Then you can use fcntl (..., F_GETLK, ...)
> to determine whether the first has locked it, although it would probably
> be better just to try to lock it (in order to avoid race conditions).

> Of course, I'm not sure why you care why both files are opening
> something for reading...

>   - Logan
> --
> whose?  my  your   his  her   our   their   _its_
> who's?  I'm you're he's she's we're they're _it's_

 
 
 

Tell if another process open a file

Post by Logan Sh » Fri, 23 Mar 2001 14:50:32




>If B try to lock the file F, B will always succeed since both A and B are
>read the file F. If A writes to F, B won't get the lock. Am I right on
>this?

A lock will not cause a write or read to happen.
A lock will not cause a write or read to not happen.
A write or read will not cause a lock to happen.
A write or read will not cause a lock to not happen.

There are two types of locks.

The first type is an exclusive lock.  When you call fcntl() and get an
exclusive lock (using F_WRLCK), you are guaranteed that no other
process will get an exclusive lock on the same file (or region of the
file) until you release the lock.  If a lock already exists, your
process will wait until it is released (assuming you used F_SETLKW
instead of F_SETLK).

The second type is a shared lock.  If you get a shared lock, this does
not prevent other processes from getting a shared lock.  However, it
does prevent other processes from getting an exclusive lock, and an
exclusive lock prevents other processes from getting a shared lock.

For this reason, people often use exclusive locks for programs that
need to write to a file and shared locks for those that need to read
from it.  This way, several processes can read the file at a time
(which does not cause a problem with data integrity) but if a process
wants to write to it, it must wait for all other processes to unlock
it, and reading processes can obtain the lock if a process is busy
writing to it.

If this doesn't answer the question, perhaps you could briefly explain
what you're trying to accomplish by preventing simultaneous reads of a
file.

  - Logan
--
whose?  my  your   his  her   our   their   _its_
who's?  I'm you're he's she's we're they're _it's_

 
 
 

Tell if another process open a file

Post by Gordon Burdi » Fri, 23 Mar 2001 15:12:06


Quote:>>If B try to lock the file F, B will always succeed since both A and B are
>>read the file F. If A writes to F, B won't get the lock. Am I right on
>>this?

>A lock will not cause a write or read to happen.
>A lock will not cause a write or read to not happen.

On some implementations, there are mandatory locks, and
a mandatory lock *WILL* cause a write or a read to not
happen (or rather, not happen YET).  An advisory lock
will not affect writes or reads.  Mandatory locks make
great denial-of-service attacks - lock some critical file
and go to sleep, and you may be able to block anyone
else from logging in - so there are generally restrictions
on what files you can use them on.  I don't know of
any system with ONLY mandatory locks: advisory locks
are available also.

Quote:>A write or read will not cause a lock to happen.
>A write or read will not cause a lock to not happen.
>There are two types of locks.

There are more types of lock than that.

There are mandatory locks vs. advisory locks, described above.
There are blocking lock requests vs. non-blocking lock requests -
either you wait for access or get an immediate error return.
And there are write (exclusive) locks vs. read(shared) locks.
And there are record (file region) locks vs. whole-file locks.

Quote:>The first type is an exclusive lock.  When you call fcntl() and get an
>exclusive lock (using F_WRLCK), you are guaranteed that no other
>process will get an exclusive lock on the same file (or region of the
>file) until you release the lock.  If a lock already exists, your
>process will wait until it is released (assuming you used F_SETLKW
>instead of F_SETLK).

>The second type is a shared lock.  If you get a shared lock, this does
>not prevent other processes from getting a shared lock.  However, it
>does prevent other processes from getting an exclusive lock, and an
>exclusive lock prevents other processes from getting a shared lock.

And, of course, ANSI C doesn't support any type of lock, nor
does it support having "another process" to lock against.

                                        Gordon L. Burditt

 
 
 

Tell if another process open a file

Post by Barry Margoli » Sat, 24 Mar 2001 02:22:30




>>>If B try to lock the file F, B will always succeed since both A and B are
>>>read the file F. If A writes to F, B won't get the lock. Am I right on
>>>this?

>>A lock will not cause a write or read to happen.
>>A lock will not cause a write or read to not happen.

>On some implementations, there are mandatory locks, and
>a mandatory lock *WILL* cause a write or a read to not
>happen (or rather, not happen YET).  An advisory lock
>will not affect writes or reads.  Mandatory locks make
>great denial-of-service attacks - lock some critical file
>and go to sleep, and you may be able to block anyone
>else from logging in - so there are generally restrictions
>on what files you can use them on.  I don't know of
>any system with ONLY mandatory locks: advisory locks
>are available also.

I believe that on systems with mandatory locking, the way it's controlled
is usually by the file's permission bits.  On Solaris, setting the set-GID
bit but turning off the group-execute bit indicates that locking should be
mandatory rather than advisory.

Since only the file's owner and root can set its permissions, this prevents
someone from setting a mandatory lock on a critical system file.

Quote:>And, of course, ANSI C doesn't support any type of lock, nor
>does it support having "another process" to lock against.

But since this thread is cross-posted to several Unix newsgroups and no
non-Unix groups, it's pretty likely that the poster is only concerned about
Unix solutions, not limiting himself to standard C.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Tell if another process open a file

Post by -hs- » Sat, 24 Mar 2001 06:24:47


Jzhou a crit dans le message ...

Quote:

>Process A opens file F for read. Process B will open file F for read. Is
>there a system call which will tell me A is opening F before B opens F?

Sorry, C have no processes.

[x-post trimmed.]

--
-hs-    "spaces, not tabs"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc

 
 
 

Tell if another process open a file

Post by Micah Cowa » Sat, 24 Mar 2001 08:13:10



> >And, of course, ANSI C doesn't support any type of lock, nor
> >does it support having "another process" to lock against.

> But since this thread is cross-posted to several Unix newsgroups and no
> non-Unix groups, it's pretty likely that the poster is only concerned about
> Unix solutions, not limiting himself to standard C.

False assumption, false conclusion.  Check your Newsgroups: headers
again, and then quit posting off-topic messages to comp.lang.c.

Followups set.

Micah

--
Let every man be swift to hear, slow to speak, slow to wrath
                                               James 1:19

 
 
 

1. How can a process tell if another process has a file open?

Is there a way to tell if another process has a file open? I'm running two
processes which open the same file (call them process A and process B).
Process A creates the file, writes to it, and closes it. Process B is
monitoring the directory where process A will be creating files. When it
detects a file, it will open it, read and process it, close it, and remove
the file.

My problem is that I have no control over process A. This process is
developed by another organization here at NCR. I can only control
process B. Is there any way that process B can detect if process A (or
any other process) has the file open?

2. IPChains REDIRECT

3. How do I tell what process has a file open?

4. Where to get old workstations?

5. How to tell which process(es) have a given file/inode open under SVr4?

6. FP97/Apache wrong date

7. System call tell how many files open in a process

8. blocking root login remotely

9. How to tell if their are open file handles when exiting a process?

10. How to tell if a file is open by another process?

11. How can you tell if a streams file is opened by another process?

12. map a process open file to a open socket ?

13. How to tell which open ports are attached to which processes?