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

Re: newbie writing a counter

by nardo (Friar)
on Aug 07, 2001 at 01:47 UTC ( [id://102630]=note: print w/replies, xml ) Need Help??


in reply to newbie writing a counter

#opens counter data file open(COUNTERDAT,"./counter.dat"); #locks file, and if can't, goes back to READCOUNTER to try again flock COUNTERDAT, 1 or next READCOUNTER;
...and later...
#opens counter data file again open(COUNTERDAT,">./counter.dat"); #locks file, and if can't, goes back to WRITECOUNTER flock COUNTERDAT, 2 or next WRITECOUNTER;
What happens when open(COUNTERDAT,">./counter.dat"); runs and then your operating system switches to another process which runs
open(COUNTERDAT,"./counter.dat"); flock COUNTERDAT, 1 or next READCOUNTER;
Answer: The second process now has the file locked but the contents was destroyed with the open from the first process. The second process will see that the count is zero and will write '1' to the file. The solution is to open the file for reading/writing: open(COUNTERDAT, "<+./counter.dat"); then seek to the beginning of the file when you want to write the new number (if you were potentially writing a smaller byte count you would want to truncate the file, but since a number will always be at least as long as the previous number (assuming positives) this is not necessary).

Replies are listed 'Best First'.
Re: Re: newbie writing a counter
by jrbush82 (Initiate) on Aug 07, 2001 at 23:42 UTC
    In the first file open, I set an array so that I can then add 1 to the number. Even if all the information is destroyed in the first file open (which is read only right?), I have still written the array which is stored. When the first process is done, it goes to the second, which opens the file as read/write and then writes the new variable, which was the first item in the array plus one. When that is done, I then open it one last time in read only, and then set a new array so that I can then post the number as output to HTML. This is the way I see what is going on. I don't quite follow you on what you are saying.
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://102630]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found