# So instead I tried the version in the comments on the # tutorial page although I don't really understand it :-(

A semaphore is like a rock sitting on the ground somewhere. Unless the process grabs the rock, it is not supposed to access the file. All the processes agree ahead of time that they won't try to access the file unless they have the rock. However, there is nothing preventing a rogue process from accessing the file directly -- the processes must take it open themselves to only access the file if they grab the rock first. When one process is done processing the file, it sets the rock down on the ground, and then the next process grabs the rock.

So a semaphore has nothing to do with locking a file per se; the semaphore is the rock, and the processes simply agree that they have to grab the rock before accessing the file.

I don't understand what code your Grid::Cluster processes are executing, so I can't help you there. It would probably behoove you to put aside the Grid::Cluster code and see if you can get locks to work on a simple program that spins off processes(or threads), which then sleep for a random amount of time before trying to write to a file. (Maybe that is what the second bit of code you posted is trying to do?)

See if you are making any of the mistakes described here:

http://perl.plover.com/yak/flock/

In reply to Re: Threads and output files (locking) by 7stud
in thread Threads and output files (locking) by Anonymous Monk

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.