[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: opening twice with O_RDWR

From: Paul Pluzhnikov
Subject: Re: opening twice with O_RDWR
Date: Sun, 31 Dec 2006 14:31:20 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

Mike - EMAIL IGNORED <> writes:

> Requirement:  To allow a file to be opened not more than once
> at a time.  (Emphasis: by any means, within or across
> processes.)

That requirement is incomplete. The following "solution" satisfies
it, but probably doesn't satisfy your *actual* requirements:

  char template[] = "tmpXXXXXX";
  int fd = mkstemp(template);

This file can only be opened once, since no other part of the
current process, and no other process knows its name.

>> If you want multiple processes to cooperate on writing a single file,
>> the proper way to achieve that is with "advisory file locking"; see
>> "man flock".
> Thanks for this; I will read it later, but a quick look suggests
> that it is what I need.

But it doesn't satisfy your requirement (as stated) either --
it doesn't prevent anybody from opening the file multiple times.

It probably does satisfy your *actual* requirements, but since you
never said what they are, we left to guess at your intentions.

It would have been much better for you to think about and state
your actual requirements, or your intentions (from which we possibly
can deduce your requirements better than you can).

> The singleton will open the file once but allow access from different
> objects without potential interference, or risk of multiple open
> attempts (thereby protecting me from my worst enemy -- myself).

Ah, you apparently only have one file that you need to worry about.
Then this is a degenerate case of "have your program keep track of
files already opened" (the approach I suggested given initial
incomplete info).

In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]