Created
March 9, 2017 20:31
-
-
Save mohsenyz/069e7d14ff261e198e9254e494ca06d8 to your computer and use it in GitHub Desktop.
Php shared memory
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 characters
| <?php | |
| class ShmLib | |
| { | |
| var $bright; | |
| var $SHM_KEY; | |
| var $my_pid; | |
| function ShmLib($SHM_KEY=null) | |
| { | |
| $this->my_pid = 123456; | |
| if (is_null($SHM_KEY)) $this->SHM_KEY = '123123123'; | |
| $this->bright = shm_attach($this->SHM_KEY, 10000, 0666); | |
| $this->one_instance(); | |
| } | |
| function get_msg($id,$remove=true) | |
| { | |
| if(@$msg=shm_get_var($this->bright,$id)) | |
| { | |
| if ($remove) @shm_remove_var($this->bright,$id); | |
| return $msg; | |
| } else return false; | |
| } | |
| function put_msg($id,$msg) | |
| { | |
| @shm_put_var($this->bright,$id,$msg); | |
| return true; | |
| } | |
| function one_instance() | |
| { | |
| $SHM_PID = $this->get_msg(1,false); | |
| if((strpos(exec('ps p'.$SHM_PID),$_SERVER['SCRIPT_FILENAME'])) === false) | |
| $this->snd_msg(1,$this->my_pid); else | |
| { | |
| echo "This program already exists on pid: $SHM_PID\r\n\r\n"; | |
| exit; | |
| } | |
| } | |
| } | |
| $shm = new ShmLib(); | |
| $shm->put_msg(2, "hello"); | |
| $hello = $shm->get_msg(2);\\will be "hello" | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment