Recent research on multi-process concurrency of liunx servers has found many problems among process concurrency. The most important one is content collaboration. So a simple table class based on liunx semaphores is made to share it.
<?php namespace Table; /* Multi-process shared memory based on table lock */ class Table { /* Shared memory handle */ protected $shm_resource; /* Semaphore handle */ protected $signal; /** * Table constructor. * * @param int table size */ public function __construct($size=1024) { /* Generating shared memory handles */ $shm_mem=shm_attach(ftok(__FILE__,'a'),$size,0666); /* Shared Memory Handle Mounting */ $this->shm_resource=$shm_mem; /* Mount semaphore */ $this->signal=sem_get(ftok(__FILE__,'b')); } /** * Shared memory write message * * @param int $key * @param string $value * @param bool serialize * @return bool isWriteSuccess */ public function writeMem(int $key,string $value,bool $serialize=TRUE) { if(sem_acquire($this->signal)) { /* Write shared memory data */ $writeRes=shm_put_var($this->shm_resource,$key,$serialize?serialize($value):$value); /* Release semaphore */ sem_release($this->signal); return $writeRes; } } /** * Get shared memory messages * * * @param int $key * @param bool $unserialize * @return string content */ public function readMem(int $key,bool $unserialize=TRUE) { if(sem_acquire($this->signal)) { /* Read shared memory resources */ $content=shm_get_var($this->shm_resource,$key); $content=$unserialize?unserialize($content):$content; /* Release semaphore */ sem_release($this->signal); return $content; } } /** * Atomic modification of line content * * * @param int $key * @param \Closure $func * @param bool $serialize * @return bool Write state */ public function updateMem(int $key,\Closure $func,bool $serialize=TRUE) { /* Closure modification */ $res=call_user_func($func,$this->readMem($key)); /* Write to shared memory */ return (bool)$this->writeMem($key,$res); } /** * Delete k values from tables * * * @param int $key * @return bool */ public function delMem(int $key) { /* Check for presence */ if(!shm_has_var($this->shm_resource,$key))return false; /* Clear content */ if(sem_acquire($this->signal)) { /* Execution deletion */ $status=(bool)shm_remove_var($this->shm_resource,$key); /* Release semaphore */ sem_release($this->signal); return $status; } /* No return false */ return false; } /** * Destructor */ public function __destruct() { // TODO: Implement __destruct() method. /* Clear semaphore handle */ sem_remove($this->signal); /* Clear shared memory */ shm_detach($this->shm_resource); } }
The above table class implements a case of multi-process operation under the function of automatic queuing operation table based on sem signal.
<?php namespace process class Process { public function getInfo() { /* Instantiated class */ $table=new \Table\Table(); $pid=pcntl_fork(); if($pid==0) { /* Subprocess logic */ /* Shared table writing */ while(true) { echo 'Subprocesses wait to acquire table locks'.PHP_EOL; $table->writeMem(1,time()); echo 'Subprocesses have released table locks'.PHP_EOL; sleep(2); } } elseif($pid>0) { /* Parent process logic */ /* Parent process read */ while (true) { echo 'Parent process acquires table locks'.PHP_EOL; echo $table->readMem(1).PHP_EOL; echo 'Parent process releases table locks'.PHP_EOL; sleep(2); } } } }