Well done that man!

"Rasmus Lerdorf" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> I think you guys are making this way more complicated than it needs to be.
> Anytime you start talking about locking files, you need to apply a huge
> reality check to yourself and sit down and approach the problem from a
> different direction.
>
> In this case could I suggest that you make use of the fact that appends of
> less than a blocksize are atomic.  Therefore, why not simply append a
> single character to a text file for each vote?  If you are voting for a
> list of things, assigning a character to each item and simply append 'a',
> 'b', 'c' or 'd' to your file.  The size of the file instantly gives you
> the number of votes cast.  Reading the file and counting the number of
> times each letter occurs using something like substr_count() will give you
> the number of votes for each option.
>
> That gives you a lockless and flexible system without the risk of deadlock
> or missing votes due to race conditions.
>
> -Rasmus
>
> On Wed, 22 May 2002, 1LT John W. Holmes wrote:
>
> > > A good structure would be to have one file for each possible answer
and
> > each
> > > file contains the number of votes it has recieved.
> > > Then:
> > > --> Open file for the chosen option as read only
> > > --> Read the value in the file
> > > --> Close the file
> > > --> Increase the value by one using ++
> > > --> Open the file again in write mode
> > > --> Lock the file
> > > --> Write the new value to the file - old one overwritten
> > > --> Unlock the file
> > > --> Close the file
> >
> >
> > That's a bad method. You have to have the lock around the read and the
> > write. With your method, 5 users might read the file, all getting 99 for
the
> > count, and then each one will try to seperatly write 100 to the file. So
you
> > lose 4 actual counts. You want to open the file, read it, update value,
> > write it, unlock, and close the file.
> >
> > Using multiple files would just be a waste of space in my opinion. A
locked
> > file doesn't stop the script, it simply waits for the file to be
unlocked
> > and then continues on.
> >
> > ---John Holmes...
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to