Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
When the first process is done, it goes to the second

If you're running on an operating system which does not support multitasking, then this statement is correct, but if you're running on any modern OS (Windows 9x/NT, unix) then you can not control when the OS switches to another process. Your operating system, in order to run multiple processes at once, will periodically switch from one process to another (assuming it is a preemptive multitasking OS, you will have no control over when it does this) so after the first process opens the file for writing (thus erasing the contents of the file) you can not ensure that the flock call will occur before another process runs.

First process erases file
OS switches to second process
Second process opens locks and reads in empty file, closes file releasing the lock which allows first process to lock the file
OS switches to first process which locks and writes correct count to file
First process terminates leaving file with correct count
Second process increments empty count to 1 and writes 1 to the file
Second process terminates leaving the file with the count of 1 regardless of what it was when process one started.

In reply to Re: Re: Re: newbie writing a counter by nardo
in thread newbie writing a counter by jrbush82

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2024-04-23 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found