in reply to Re: module for file handle control?
in thread module for file handle control?

That code will never work... there is a race condition! Please, please don't use it!!! What happens if something else creates a file with the same $filename after your test?

  • Comment on Re: Re: module for file handle control?

Replies are listed 'Best First'.
Re: Re: Re: module for file handle control?
by matth (Monk) on Dec 02, 2002 at 21:57 UTC
    What is a race condition>

      From www.linuxfocus.org/English/September2001/article198.meta.shtml:

      The general principle defining race conditions is the following : a process wants to access a system resource exclusively. It checks that the resource is not already used by another process, then uses it as it pleases. The race condition occurs when another process tries to use the same resource in the time-lag between the first process checking that resource and actually taking it over. The side effects may vary. The classical case in OS theory is the deadlock of both processes. More often it leads to application malfunction or even to security holes when a process wrongfully benefits from the privileges another.

      In other words, using the above code:

      while (-e $filename){ $filename.= '.new'; } # while being here, another script can create a file with # the same $filename, causing a bug... open(F, "> $filename");

      To be safe, you should 'lock' the resource and be sure that nothing else can use it. The practical solution is inside File::Temp, that is shipped with Perl since version 5.6.1 (I think)

      Ciao, Valerio