PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old June 26th, 2004, 11:44 AM
chess4us chess4us is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 8 chess4us User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Looking for suggestions

A developer of a text based CMS written in php is looking for suggestions to solve a problem and I thought I would ask people here for help.

The solution must work on a windows and linux platform.

The problem appears to be that the text files i.e. (members, stats, forums) will become corrupt sometimes when they are accessed or changed to
to often, he is presently trying to solve the problem by locking the files to try to prevent corruption. Doesn't appear to be working correctly.

So if anyone has an idea, suggestion or method on how to correct this problem please let me know.

Thanks in advance!!!

Reply With Quote
  #2  
Old June 26th, 2004, 01:24 PM
Archemides Archemides is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 25 Archemides User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Archemides Send a message via AIM to Archemides Send a message via Yahoo to Archemides
Thoughts on record/file locking

I am very much of a beginner, and self-taught. But here are my thoughts. A while ago I was wondering what happens at a website if a php script updates a text file. I was wondering what happens if several people come to a site at once and try to open/update the same file. It seems obvious to me that updates to a record in a mysql database would not be corrupted, since each client would be accessing the same mysql engine, and the engine would somehow queue the requests, and update the record in an orderly fashion, without corrupting the record.

Some years ago, in the 1980's I asked a unix programmer about record locking issues. He laughed in a somewhat scoffing manner, and then, after thinking for a moment, commented that and empty file would be created by the record for each record accessed, and that other users or processes would first test for the existence of that file. If the file (whose name perhaps is the record key) did exist, then a message would be issued stating that the record was in use, and to try later. Once the original application finished with that record, it would delete the empty file, and by this fashion, other applications/users would know that the record was now "unlocked".

Perhaps such a scheme might be useful in this situation. A php application could create an empty file which would indicate that the other file was being updated. Once the update is finished and the file is closed, then the "flag" file could be deleted. It would certainly be interesting if this type of tactic actually eliminated the file corruption.



A different thought would be to switch from a file to a blob field in a mysql record. I imagine that one should be able to store the same sorts of things in a large field in a mysql row that one stores in a simple text file. In this way, one would depend upon the mysql engine itself to maintain read/write/update file integrity.








Reply With Quote
  #3  
Old June 26th, 2004, 01:51 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 5
If this is indeed the problem, which I have to confess to being doubtful of without some further explanation of what is going on and perhaps some code, then the suggestion above should work if implemented properly but it suffers from a major flaw.

Lets assume two clients are attempting to access the file at the same time. In an ideal world the first client checks for the flag file, finds there isn't one and creates it then the second client will discover it exists if it runs the test. Unfortunately this will not always be the case since the first client may run the test, then the second one may run the test before the flag file is created meaning they both think they can have access to the main file.

To solve this problem you need to implement something that works indivisibly for both the test and setting of the flag in whatever form it takes. Ie nothing can happen in between the test being run and the flag being set. Whether this is the case or not with the scheme described above I don't know, but if this is the problem then you will need to find out before implementing something otherwise this problem will reoccur later and be even harder to locate since it will happen intermitently depending on the timings of the accesses.

Hope this helps,

-KM-

Reply With Quote
  #4  
Old June 27th, 2004, 09:55 AM
chess4us chess4us is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 8 chess4us User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi

Here is the snipet of code being used to attemp to control the text files, one of the draw backs I have noticed
is when a file get locked it appears to stay locked, if anyone can suggest a better way or an improvement to this
code, that would be great.

Again thanks in advance.

PHP Code:
function write_lock($filename) {
$result 0;
if (
file_exists($filename)) {
    if (
$this->is_locked($filename) == 0) {
     
$lp fopen($filename ".lock","w");
     
fclose($lp);
     @
chmod($filename,0777);
     
$result 1;
    }
}
return 
$result;
}
 
function 
is_locked($filename) {
$result 0;
if (
file_exists($filename)) {
    if (
file_exists($filename ".lock")) {
     
$result 1;
    }
}
return 
$result;
}
 
function 
write_unlock($filename) {
$result 0;
if (
$this->is_locked($filename)) {
    @
unlink($filename ".lock");
    
$result 1;
}
return 
$result;
}
 
function 
wait_for_unlock($filename,$time 300) {
$st gettimeofday();
$start $st["usec"];
while(
$this->is_locked($filename)) {
    
$no gettimeofday();
    if ((
$no["usec"]-$start) > $time) {
     break 
1;
    }
}
}
 
function 
safe_file($filename) {
$result 0;
$diff 0;
$st explode(" ",microtime());
$start $st[1] . substr($st[0],2,4);
while (
$diff 1000) {
    if (!
$this->is_locked($filename)) {
     
$this->write_lock($filename);
     
$result file($filename);
     
$this->write_unlock($filename);
     
$diff 2000;
    }
    else {
     
$no explode(" ",microtime());
     
$now $no[1] . substr($no[0],2,4);
     
$diff $now-$start;
    }
}
return 
$result;
}
 
 


Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Looking for suggestions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway