saveWikiText($WIKI_ID, file_get_contents($TARGET_FN), $OPTS->get('m'), $OPTS->has('t')); clearLock($WIKI_ID); exit(0); break; #---------------------------------------------------------------------- #---------------------------------------------------------------------- case 'lock': $WIKI_ID = $OPTS->arg(1); if (!$WIKI_ID) { fwrite(STDERR, "Wiki page ID required\n"); exit(1); } if ($OPTS->has('f')) { deleteLock($WIKI_ID); } obtainLock($WIKI_ID); print "Locked : {$WIKI_ID}\n"; exit(0); break; #---------------------------------------------------------------------- #---------------------------------------------------------------------- case 'unlock': $WIKI_ID = $OPTS->arg(1); if (!$WIKI_ID) { fwrite(STDERR, "Wiki page ID required\n"); exit(1); } if ($OPTS->has('f')) { deleteLock($WIKI_ID); } else { clearLock($WIKI_ID);
<?php $mutex = new SyncMutex("UniqueName"); for ($i = 0; $i < 2; $i++) { $pid = pcntl_fork(); if ($pid < 0) { die("fork failed"); } elseif ($pid > 0) { echo "parent process \n"; } else { echo "child process {$i} is born. \n"; obtainLock($mutex, $i); } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child {$status} completed\n"; } function obtainLock($mutex, $i) { echo "process {$i} is getting the mutex \n"; $res = $mutex->lock(200); sleep(1); if (!$res) { echo "process {$i} unable to lock mutex. \n"; } else { echo "process {$i} successfully got the mutex \n"; $mutex->unlock(); } exit; }
<?php $key = ftok('/tmp', 'c'); $sem = sem_get($key); for ($i = 0; $i < 2; $i++) { $pid = pcntl_fork(); if ($pid < 0) { die("fork failed"); } elseif ($pid > 0) { //echo "parent process \n"; } else { echo "child process {$i} is born. \n"; obtainLock($sem, $i); } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child {$status} completed\n"; } sem_remove($sem); // finally remove the sem function obtainLock($sem, $i) { echo "process {$i} is getting the sem \n"; $res = sem_acquire($sem, true); sleep(1); if (!$res) { echo "process {$i} unable to get sem. \n"; } else { echo "process {$i} successfully got the sem \n"; sem_release($sem);
<?php $lock = new SyncSemaphore("UniqueName", 2); for ($i = 0; $i < 2; $i++) { $pid = pcntl_fork(); if ($pid < 0) { die("fork failed"); } elseif ($pid > 0) { echo "parent process \n"; } else { echo "child process {$i} is born. \n"; obtainLock($lock, $i); } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child {$status} completed\n"; } function obtainLock($lock, $i) { echo "process {$i} is getting the lock \n"; $res = $lock->lock(200); sleep(1); if (!$res) { echo "process {$i} unable to lock lock. \n"; } else { echo "process {$i} successfully got the lock \n"; $lock->unlock(); } exit; }