Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Shutting the Barn Door [File Locking]

Name: Anonymous 2011-01-20 16:21

Problem: The open call erases the file before the process has the lock

        open F, "> somefile"; # DERP you already erased all the data in somefile by opening it
        flock F, LOCK_EX;     # before obtaining this lock
        # Now write F
        close F;


Now you have two problems: +< still erases the file before the process has the lock

        open F, "+< somefile";
        flock F, LOCK_EX;
        truncate F, 0;
        # Now write F
        close F;


Dear /prog/, what am I missing?

Name: Anonymous 2011-01-20 16:23

Read SICP.

Name: Anonymous 2011-01-20 16:38


(define s (make-semaphore 1))

(call-with-semaphore s (lambda ()
  (with-output-to-file herp (write derp)))

Name: Anonymous 2011-01-20 16:44

Name: Anonymous 2011-01-20 16:57

google

Name: Anonymous 2011-01-20 17:00

So you're trying to open the file before doing the thing that checks whether you can open it or not?

Name: Anonymous 2011-01-20 17:06

>>6
How would you check whether you can open it or not without opening it?

Name: Anonymous 2011-01-20 17:07

>>7
man stat

Name: Anonymous 2011-01-20 17:28

>>8
But that might cause a race condition.

Name: Anonymous 2011-01-20 17:36

Provide two arguments for open, not three, as Perl Critic advises.
Also, open it for reading with exclusive locking, wait for other processes to go away, reopen in write mode, do your shit, LOCK_UN, close.

Name: Anonymous 2011-01-20 17:38

open it for reading with exclusive locking
Interesting. I will do this.

Don't change these.
Name: Email:
Entire Thread Thread List