I am looking for a perl code that reads/writes to a data file, every time any other program tries to read/write from that file the reading/writing program will block until the first will complete

To make things simpler I am currently trying to have a program which reads a file , sort the file and pop the first line.

For e.g , input file is: (called "a_file")

8 7 6 5 4 3 2 1

If 3 processes are running in parallel or serially, the final file will be:

1 2 3 4 5

I wrote:

#! /usr/local/bin/perl chomp($pid = $$); $LOCK_EXCLUSIVE = 2; $UNLOCK = 8; $fifo = "ENV{HOME}/a_file"; popup($fifo); sub popup { my($fifo) = @_; while (! -e "$fifo.lock") { open (LOCK, "> $fifo.lock") || die "problem opening $f +ifo\n"; flock LOCK , $LOCK_EXCLUSIVE; print LOCK $pid; open (FILE, "< $fifo") || die "problem opening $fifo\n +"; @lines = <FILE>; close(FILE) || warn "update queue file exited $?\n";; open (FILE, "> $fifo") || die "problem opening $fifo\n +"; @sorted = sort @lines; $pop = shift @sorted; print "$hostname is poping $pop\n"; print FILE "@sorted\n"; close(FILE) || warn "update queue file exited $?\n";; close(LOCK) || warn "lock file exited $?\n";; $str=`cat $fifo.lock`; flock LOCK, $UNLOCK; } if ($pid eq $str) { print "PID lock file is going to be removed\n" ; system("rm $fifo.lock"); } }

But it looks that while I running in parallel on 3 different hosts it does not work as I expect , I get:

linux24 is poping PID lock file is going to be removed linux28 is poping PID lock file is going to be removed

Also I found this code over the web but I fail to run it , it get into infinite loop –

chdir; # go home $FIFO = '.signature'; $ENV{PATH} .= ":/etc:/usr/games"; while (1) { unless (-p $FIFO) { unlink $FIFO; system('mknod', $FIFO, 'p') && die "can't mknod $FIFO: $!"; } # next line blocks until there's a reader open (FIFO, "> $FIFO") || die "can't write $FIFO: $!"; print FIFO "John Smith (smith\@host.org\n"; close FIFO; sleep 2; # to avoid dup signals }

Any idea? I will highly appreciate if you can advise.

Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

GrandFather added readmore tags


In reply to mkfifo/mknode by azaria

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.