Browse Source

locking mechanismen hinuzgefuegt...so gut wies eben geht

master
Georg Hopp 18 years ago
parent
commit
d4303ecfda
  1. 1
      TODO
  2. 104
      ajax+json_home/ajax.php
  3. 2
      ajax+json_home/test1.html
  4. 4
      libs/errException.php
  5. 58
      libs/mutualExclusion.php
  6. 90
      pvtest.php

1
TODO

@ -17,3 +17,4 @@
http://www.php-faq.de/search.php?q=Locking&l=20 http://www.php-faq.de/search.php?q=Locking&l=20
web developer web developer
http://de.wikipedia.org/wiki/Semaphor_%28Informatik%29 -djikstra PV- http://de.wikipedia.org/wiki/Semaphor_%28Informatik%29 -djikstra PV-
http://www.kreissl.info/diggs/bs_04.php

104
ajax+json_home/ajax.php

@ -1,67 +1,49 @@
<?php <?php
// $english = mysql_escape_string($_REQUEST['translate']);
// Der Service ist zur Zeit leider deaktiviert....
// $trans = new SoapClient(
// "http://www.xmethods.net/sd/2001/BabelFishService.wsdl");
/*
try
{
$german = $trans->BabelFish("en_de",$english);
$french = $trans->BabelFish("en_fr",$english);
}
catch(SoapFault $e)
{
$english = "not found";
$german = "not found";
$french = "not found";
}
*/
class dummy
class dummy
{
// nur public member werden via json verschickt.
public $german;
public $english;
public $french;
function __construct ($english, $german, $french)
{ {
// nur public member werden via json verschickt.
public $german;
public $english;
public $french;
function __construct ($english, $german, $french)
{
$this->german = $german;
$this->english = $english;
$this->french["val1"] = $french;
$this->french["val2"] = "jokus";
}
}
$encoding = FALSE;
if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) &&
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE)
$encoding = "x-gzip";
if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) &&
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
$encoding = "gzip";
header ('Content-type: text/plain');
$result = FALSE;
if ($encoding !== FALSE)
$result = gzcompress (json_encode (new dummy (
'doing gzip', 'gezipedte Daten', 'la zippo')));
if ($result !== FALSE)
{
header ('Content-Encoding: ' . $encoding);
print ("\x1f\x8b\x08\x00\x00\x00\x00\x00");
print ($result);
}
else
{
$result = json_encode (new dummy (
'doing no gzip', 'nicht gezipedte Daten', 'no la zippo'));
print ($result);
$this->german = $german;
$this->english = $english;
$this->french["val1"] = $french;
$this->french["val2"] = "jokus";
} }
}
$encoding = FALSE;
if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) &&
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE)
$encoding = "x-gzip";
if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) &&
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
$encoding = "gzip";
header ('Content-type: text/plain');
$result = FALSE;
if ($encoding !== FALSE)
$result = gzcompress (json_encode (new dummy (
'doing gzip', 'gezipedte Daten', 'la zippo')));
if ($result !== FALSE)
{
header ('Content-Encoding: ' . $encoding);
print ("\x1f\x8b\x08\x00\x00\x00\x00\x00");
print ($result);
}
else
{
$result = json_encode (new dummy (
'doing no gzip', 'nicht gezipedte Daten', 'no la zippo'));
print ($result);
}
?> ?>

2
ajax+json_home/test1.html

@ -67,7 +67,7 @@
// anfrage erstellen (GET, url ist localhost, // anfrage erstellen (GET, url ist localhost,
// request ist asynchron // request ist asynchron
var url = 'http://muedv031/~georg/bilder/ajax+json/ajax.php' +
var url = 'http://localhost/~georg/bilder/ajax+json_home/ajax.php' +
'?translate=' + document.getElementById ('myword').value; '?translate=' + document.getElementById ('myword').value;
req.open("GET", url, true); req.open("GET", url, true);

4
libs/errException.php

@ -49,7 +49,7 @@ function setErrExceptionMapping ()
{ {
global $oldErrHandler; global $oldErrHandler;
if ($oldErrHandler == NULL)
if ($oldErrHandler === NULL)
$oldErrHandler = set_error_handler ('mapErrToExc'); $oldErrHandler = set_error_handler ('mapErrToExc');
} }
@ -57,7 +57,7 @@ function resetErrExceptionMapping ()
{ {
global $oldErrHandler; global $oldErrHandler;
if ($oldErrHandler != NULL)
if ($oldErrHandler !== NULL)
{ {
set_error_handler ($oldErrHandler); set_error_handler ($oldErrHandler);
$oldErrHandler = NULL; $oldErrHandler = NULL;

58
libs/mutualExclusion.php

@ -0,0 +1,58 @@
<?php
require_once dirname(__FILE__) . '/../config.php';
require_once LIBDIR . 'errException.php';
function acquireLock ($lockFile, $csId)
{
$fName = $lockFile . $csId . '.lck';
$lock = NULL;
setErrExceptionMapping ();
$lockHandle = fopen ($fName, 'w');
// if available use semaphores for mutual exclusion because flock
// doesn't work reliably in threaded environments.
if (function_exists ('sem_get'))
{
$lock = sem_get (ftok ($fName, $csId), 1, 0644, TRUE);
$state = sem_acquire ($lock);
while ($state === FALSE)
$state = sem_acquire ($lock);
}
else
{
$state = flock ($lock, LOCK_EX);
while ($state === FALSE)
$state = flock ($lock, LOCK_EX);
}
// Here one could write informations in the lockfile...time, pid, etc.
fwrite ($lockHandle, session_id () . '::' . time ());
fflush ($lockHandle);
resetErrExceptionMapping ();
$userAbort = ignore_user_abort(TRUE);
return array ($lockHandle, $lock, $userAbort);
}
function releaseLock ($lock)
{
setErrExceptionMapping ();
if ($lock[1] !== NULL)
sem_release ($lock[1]);
ftruncate ($lock[0], 0);
fclose ($lock[0]);
resetErrExceptionMapping ();
ignore_user_abort($lock[2]);
}
?>

90
pvtest.php

@ -1,93 +1,15 @@
<?php <?php
/*
* Zunächst mal den Gedanken skizziert
* Da es kein wirklich zuverlässiges Locking fuer kritische Bereiche
* in php gibt. Semaphore existiren nur unter Unix und flock arbeitet nicht
* wenn der webserver threaded ist und keine separaten prozesse pro
* request erzeugt habe ich folgenden Gedankengang...nicht 100% Atomar, aber
* möglicherweise das beste was man bekommen kann.
*
* ACQUIRE
* =======
* |
* - leere Datei erzeugen (tmp)
* - link (tmp) nach (lock)
* |
* |-----------------------------
* OK | | Fehler |
* | | |
* | | - lese (lock): sollte blockieren solange diese leer ist
* | | - unlink (lock)
* | | - link (tmp) nach (lock)
* | Fehler | |
* | ---------------|
* | | OK
* | |
* |-----------------------------
* |
* - (tmp) loeschen
* |
* (ENDE)
*
* RELEASE
* =======
* |
* - schreibe nach (lock)
* |
* (ENDE)
*
* Da link angeblick atomar ist muesste das zumindest zuverlaessig Sperren...
* Zusaettzlich sollte man vielleicht noch ein Priorisierung irgendwo
* festhalten, um zu verhindern das ein Prozess ewig wartet, weil er immer
* Pech bei der zuweisung von CPU Zeit hat.
*
* So ein Pech, read blockiert natuerlich nicht bei regulären Dateien, da
* ja EOF erreich wird....da muss ich mir was anderes ausdenken....
* OK, unter UNIX kann ich exec mkfifo machen um eine named pipe zu generieren.
* Fuer windows muss was anderes oder busy waiting (iiieeh) rein....
*
* !!! Zur info, schon der read open auf eine FIFO blocked wenn kein Writer
* existiert. Das war auch ein aspekt den man berücksichtigen muß... !!!
*/
require_once dirname(__FILE__) . '/config.php';
require_once LIBDIR . 'mutualExclusion.php';
function acquire ($lock, $tmp)
{
if (! file_exists ($tmp))
exec ('mkfifo ' . $tmp);
// hier koennte irgendwas bzgl. der Priorisierung in nem else folgen...
if (link ($tmp, $lock) === FALSE)
{
while (true)
{
$handle = fopen ($lock, 'w');
fwrite ($handel, '1');
fclose ($handle);
if (unlink ($lock) === FALSE) continue;
if (link ($tmp, $lock) === FALSE) continue;
break;
}
}
unlink ($tmp);
}
function release ($lock)
{
$handle = fopen ($lock, 'r');
stream_set_blocking ($handle, 0);
if (fread ($handle, 1) === 0)
unlink ($lock);
fclose ($handle);
}
acquire ('lock', 'tmp');
//session_start ();
$lock = acquireLock ('lock/lock', '1');
echo "Lock bekommen\n"; echo "Lock bekommen\n";
sleep (30);
sleep (2);
release ('lock');
releaseLock ($lock);
?> ?>
Loading…
Cancel
Save