Last active
January 13, 2017 14:08
-
-
Save sawantuday/76799779db91e88ef2dae844d59fec4d to your computer and use it in GitHub Desktop.
Revisions
-
sawantuday revised this gist
Jan 13, 2017 . No changes.There are no files selected for viewing
-
sawantuday revised this gist
Jan 13, 2017 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ <?php class SharedMemory { private $shmID; private $semaphoreID; -
sawantuday created this gist
Jan 13, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ class SharedMemory { private $shmID; private $semaphoreID; public function __construct($token, $memSize=10000){ $this->shmID = shm_attach($token, $memSize); $this->semaphoreID = sem_get($token); if($this->shmID === false){ error_log("Failed to create shared memory segment"); } } public function __destruct(){ if($this->shmID) shm_detach($this->shmID); // sem_release($this->semaphoreID); } public function getVar($key){ sem_acquire($this->semaphoreID); $var = shm_has_var($this->shmID, $key) ? shm_get_var($this->shmID, $key) : false; sem_release($this->semaphoreID); return $var; } public function putVar($key, $val){ sem_acquire($this->semaphoreID); $success = shm_put_var($this->shmID, $key, $val); sem_release($this->semaphoreID); return $success; } public function delVar($key){ sem_acquire($this->semaphoreID); $success = shm_has_var($this->shmID, $key) && shm_remove_var($this->shmID, $key); sem_release($this->semaphoreID); return $success; } public function delSegment(){ sem_acquire($this->semaphoreID); // shm_detach($this->shmID); // not required $success = shm_remove($this->shmID); sem_release($this->semaphoreID); sem_remove($this->semaphoreID); $this->shmID = null; $this->semaphoreID = null; return $success; } } // $memory = new SharedMemory($token=1001); // $memory->putVar(1, 1000); // echo $memory->getVar(1); // $memory->delVar(1); // $memory->delSegment();