示例#1
1
 /**
  * Writes a message to the shared memory.
  *
  * @param mixed   $message The message to send
  * @param integer $signal  The signal to send afterward
  * @param integer $pause   The number of microseconds to pause after signalling
  */
 public function send($message, $signal = null, $pause = 500)
 {
     $messageArray = array();
     if (($shmId = @shmop_open($this->pid, 'a', 0, 0)) > 0) {
         // Read any existing messages in shared memory
         $readMessage = shmop_read($shmId, 0, shmop_size($shmId));
         $messageArray[] = unserialize($readMessage);
         shmop_delete($shmId);
         shmop_close($shmId);
     }
     // Add the current message to the end of the array, and serialize it
     $messageArray[] = $message;
     $serializedMessage = serialize($messageArray);
     // Write new serialized message to shared memory
     $shmId = shmop_open($this->pid, 'c', 0644, strlen($serializedMessage));
     if (!$shmId) {
         throw new ProcessControlException(sprintf('Not able to create shared memory segment for PID: %s', $this->pid));
     } else {
         if (shmop_write($shmId, $serializedMessage, 0) !== strlen($serializedMessage)) {
             throw new ProcessControlException(sprintf('Not able to write message to shared memory segment for segment ID: %s', $shmId));
         }
     }
     if (false === $signal) {
         return;
     }
     $this->signal($signal ?: $this->signal);
     usleep($pause);
 }
示例#2
0
 function MonClose()
 {
     if (!shmop_delete($this->{$shm_id})) {
         debug("No se pudo borrar el segmento de memoria compartida.", "red");
     }
     shmop_close($this->{$shm_id});
 }
示例#3
0
 /**
  * Resize memory block
  * @param int $size
  * @return bool
  */
 protected function resize($size)
 {
     if ($size > $this->max_size) {
         return false;
     }
     //should be called AFTER reading memory (to not loose changing of variables)
     if (empty($this->mem)) {
         return false;
     }
     ignore_user_abort(true);
     set_time_limit(180);
     if (is_array($this->mem)) {
         $this->mem[self::map_info][self::map_info_resized] = $this->mem[self::map_info][self::map_info_resized] + 1;
         $this->mem[self::map_info][self::map_info_resizetime] = time();
     }
     shmop_delete($this->shm);
     shmop_close($this->shm);
     $t = serialize($this->mem);
     $memsize = strlen($t);
     if ($memsize > $size) {
         $size = $memsize + 1000;
     }
     $this->shm = shmop_open($this->shmkey, "n", 0777, $size);
     if (!$this->shm) {
         return false;
     }
     //mmm... oops.
     unset($this->mem);
     $w = shmop_write($this->shm, str_pad($t, shmop_size($this->shm), ' ', STR_PAD_RIGHT), 0);
     if (!$w) {
         return false;
     }
     return true;
 }
示例#4
0
 /**
  * @param $key
  * @param int $mode
  * @param int $size
  * @return mixed|null
  * @throws OpenSharedMemoryException
  * @throws ReadSharedMemoryException
  * @throws SupportSharedMemoryException
  */
 public static function read($key, $mode = 0644, $size = 100)
 {
     if (!self::checkSHMOPSupport()) {
         return null;
     }
     @($shm_id = shmop_open($key, "a", $mode, $size));
     //read only
     if (!$shm_id) {
         throw new OpenSharedMemoryException("The shared memory block could not be opened");
     }
     @($cached_string = shmop_read($shm_id, 0, $size));
     if (!$cached_string) {
         shmop_delete($shm_id);
         shmop_close($shm_id);
         throw new ReadSharedMemoryException("The shared memory block could not be read");
     }
     $data = json_decode($cached_string, true);
     if (isset($data['expiration']) && time() > $data['expiration'] || !isset($data['expiration'])) {
         shmop_delete($shm_id);
         shmop_close($shm_id);
         return null;
     }
     shmop_close($shm_id);
     return unserialize($data['value']);
 }
示例#5
0
 public function create($key, $buckets = 12281, $size = 10000000)
 {
     if ($key == 0) {
         $this->errmsg = "key is 0";
         return false;
     }
     $this->shmId = @shmop_open($key, 'a', 0, 0);
     if ($this->shmId != false) {
         if (!shmop_delete($this->shmId)) {
             $this->errmsg = 'delete exist shm 0x' . dechex($key) . ' error';
             return false;
         }
     }
     $this->shmId = shmop_open($key, 'c', 0777, $size);
     if ($this->shmId == false) {
         $this->errmsg = 'create shm 0x' . dechex($key) . ' error';
         return false;
     }
     $head['bsize'] = $size;
     $head['size'] = 0;
     $head['buckets'] = $buckets;
     $head['start'] = 24;
     $head['data'] = $buckets * 8 + 24;
     $head['free'] = $buckets * 8 + 24;
     return $this->setHead($head);
 }
示例#6
0
 function rm($key)
 {
     $this->shmop_key = ftok($this->pre . $key);
     $this->shmop_id = shmop_open($this->shmop_key, 'c', 0644, 0);
     $result = shmop_delete($this->shmop_id);
     shmop_close($this->shmop_id);
     return $result;
 }
示例#7
0
 /**
  * Get all the exceptions added to the shared memory.
  *
  * @return \Throwable[]
  */
 public function getExceptions() : array
 {
     $memory = shmop_open($this->key, "a", 0, 0);
     $exceptions = $this->unserialize($memory);
     shmop_delete($memory);
     shmop_close($memory);
     return $exceptions;
 }
function __unlock()
{
    if (isset($_SERVER['sync'])) {
        shmop_delete($_SERVER['sync']);
        shmop_close($_SERVER['sync']);
        unset($_SERVER['sync']);
    }
}
示例#9
0
 /**
  * @return boolean
  */
 public function delete()
 {
     if (!$this->exists()) {
         return true;
     }
     $shmId = shmop_open($this->shmKey, 'w', 0, 0);
     $result = (bool) shmop_delete($shmId);
     shmop_close($shmId);
     return $result;
 }
示例#10
0
 public function delete($id, $size)
 {
     $shm = $this->open($id, $size);
     if (shmop_delete($shm)) {
         $keyfile = $this->getKeyFile($id);
         if (file_exists($keyfile)) {
             unlink($keyfile);
         }
     }
     return true;
 }
示例#11
0
 public function getCallbackParams()
 {
     $shmId = @shmop_open($this->pid, 'a', 0644, 0);
     if (empty($shmId)) {
         return false;
     }
     $datas = unserialize(shmop_read($shmId, 0, shmop_size($shmId)));
     shmop_delete($shmId);
     shmop_close($shmId);
     return $datas;
 }
示例#12
0
 /**
  * delete shared memory
  *
  * @return  void
  * @throws  RuntimeException
  */
 public function delete()
 {
     $s = @shmop_open($this->genKey(), 'a', 0, 0);
     if ($s === false) {
         return;
     }
     if (!shmop_delete($s)) {
         throw new RuntimeException('could not delete shared memory');
     }
     shmop_close($s);
     unlink('/tmp/' . sha1($this->pid));
 }
示例#13
0
 public function __destruct()
 {
     if ($this->changed) {
         $serialized = serialize($this->data);
         if (strlen($serialized) > shmop_size($this->res)) {
             shmop_delete($this->res);
             $this->res = shmop_open($id, 'c', 0644, ceil(strlen($serialized) * 1.25));
         }
         shmop_write($this->res, $serialized, 0);
     }
     shmop_close($this->res);
 }
示例#14
0
 protected function tearDown()
 {
     if ($shmid = @shmop_open(self::SHMOP_ID, 'w', 0644, 0)) {
         /*
          * Fill memory block for fix bug
          * @link https://bugs.php.net/bug.php?id=71921
          */
         shmop_write($shmid, str_pad('', strlen(self::WORD), ' '), 0);
         shmop_delete($shmid);
         shmop_close($shmid);
     }
 }
示例#15
0
 function stream_write($data)
 {
     $p = $this->path;
     $fp = fopen($p, 'w');
     fwrite($fp, $data);
     fclose($fp);
     $this->id = ftok($this->path, 'M');
     if ($t = @shmop_open($this->id, 'a', 0, 0)) {
         shmop_delete($t);
     }
     $t = shmop_open($this->id, 'c', 0755, strlen($data));
     shmop_write($t, $data, 0);
 }
示例#16
0
文件: Shmop.php 项目: glial/glial
 public function deletemem($shmkey)
 {
     $size = shmop_size($shmkey);
     if ($size > 0) {
         $this->updatestats($size, "del");
     }
     if (!shmop_delete($shmkey)) {
         shmop_close($shmkey);
         return false;
     } else {
         shmop_close($shmkey);
         return true;
     }
 }
 public function delete()
 {
     if ($this->status == self::STATUS_DELETED) {
         return true;
     }
     // Reopen segment if not already opened
     if ($this->status != self::STATUS_OPENED) {
         $this->open();
     }
     $result = shmop_delete($this->shmId);
     $this->shmId = null;
     $this->status = self::STATUS_DELETED;
     return $result;
 }
示例#18
0
文件: Fastdb.php 项目: noikiy/inovi
 private function write($data = null)
 {
     if (is_null($data)) {
         $db = STORAGE_PATH . DS . 'memory' . sha1($this->ns . $this->entity) . '.db';
         $data = $this->data();
         File::delete($db);
         File::put($db, $data);
     }
     shmop_delete($this->db);
     shmop_close($this->db);
     $size = $this->size > strlen($data) ? $this->size : strlen($data);
     $this->size = $size;
     $this->db = shmop_open($this->key, 'c', 0755, $size);
     return shmop_write($this->db, $data, 0);
 }
示例#19
0
文件: GeoIP.php 项目: kubrey/maxgeo
 /**
  * 
  * @param type $file
  * @throws \Exception
  */
 public function geoip_load_shared_mem($file)
 {
     $fp = fopen($file, "rb");
     if (!$fp) {
         throw new \Exception("error opening {$file}: {$php_errormsg}");
     }
     $s_array = fstat($fp);
     $size = $s_array['size'];
     if ($shmid = @shmop_open(GEOIP_SHM_KEY, "w", 0, 0)) {
         shmop_delete($shmid);
         shmop_close($shmid);
     }
     $shmid = shmop_open(GEOIP_SHM_KEY, "c", 0644, $size);
     shmop_write($shmid, fread($fp, $size), 0);
     shmop_close($shmid);
 }
示例#20
0
 public function geoip_load_shared_mem($file)
 {
     $fp = fopen($file, "rb");
     if (!$fp) {
         print "error opening {$file}: {$php_errormsg}\n";
         exit;
     }
     $s_array = fstat($fp);
     $size = $s_array['size'];
     if ($this->shmid = @shmop_open(self::GEOIP_SHM_KEY, "w", 0, 0)) {
         shmop_delete($this->shmid);
         shmop_close($this->shmid);
     }
     $this->shmid = shmop_open(self::GEOIP_SHM_KEY, "c", 0644, $size);
     shmop_write($this->shmid, fread($fp, $size), 0);
     shmop_close($this->shmid);
 }
示例#21
0
 private function _getData()
 {
     // get length of data in shared memory
     $dataLength = $this->_getDataLength();
     if (!$dataLength) {
         return array();
     }
     // read data from shared memory
     $dataShm = shmop_open($this->_dataShmKey, 'c', 0644, $dataLength);
     $serializedData = shmop_read($dataShm, 0, $dataLength);
     $data = @unserialize($serializedData);
     // destroy old data storage
     shmop_delete($dataShm);
     shmop_close($dataShm);
     // data broken
     if (!$data) {
         return array();
     }
     return $data;
 }
示例#22
0
 /**
  * @depends testGetTubes
  */
 public function testDoWork()
 {
     $expected = ['test-tube-1' => '1', 'test-tube-2' => '2'];
     foreach ($expected as $tube => $value) {
         $this->client->addWorker($tube, function (Job $job) {
             // Store string «test-tube-%JOB_BODY%» in shared memory
             $memory = shmop_open(self::$shmKey, 'c', 0644, self::$shmLimit);
             $output = trim(shmop_read($memory, 0, self::$shmLimit));
             $output .= sprintf("\ntest-tube-%s", $job->getBody());
             shmop_write($memory, $output, 0);
             shmop_close($memory);
             exit(1);
         });
         $this->client->putInTube($tube, $value);
     }
     $this->client->doWork();
     $memory = shmop_open(self::$shmKey, 'a', 0, 0);
     $output = shmop_read($memory, 0, self::$shmLimit);
     $this->assertTrue(shmop_delete($memory));
     shmop_close($memory);
     $this->assertNotEmpty($output);
     // Compare number of items in expected list with lines in shared memory
     $this->assertEquals(count($expected), count(array_unique(explode("\n", trim($output)))));
 }
示例#23
0
 /**
  * Delete a block from memory
  *
  * @param int $id memory block id
  */
 function delete_mem($id)
 {
     if ((int) $id) {
         if (!shmop_delete($id)) {
             //$this->log_this("Couldn't mark shared memory block for deletion.\n");
         }
     }
 }
示例#24
0
 /**
  * Remove
  */
 public function remove()
 {
     $exists = $this->exists($this->id);
     if ($exists) {
         shmop_delete($this->shmId);
         shmop_close($this->shmId);
     }
 }
示例#25
0
    echo "failed\n";
} else {
    echo "ok\n";
}
echo "data in memory is: " . shmop_read($shm_id, 0, $written) . "\n";
/* try to append data to the shared memory segment, this should fail */
@shmop_write($shm_id, $write_d1, $written);
echo $php_errormsg . "\n";
shmop_close($shm_id);
echo "shm open for read only: ";
$shm_id = shmop_open($hex_shm_id, "w", 0644, 1024);
if (!$shm_id) {
    echo "failed\n";
} else {
    echo "ok\n";
}
echo "shm write test #1: ";
$written = shmop_write($shm_id, $write_d2, $written);
if ($written != strlen($write_d2)) {
    die("failed\n");
} else {
    echo "ok\n";
}
echo "data in memory is: " . shmop_read($shm_id, 0, strlen($write_d1 . $write_d2)) . "\n";
echo "deletion of shm segment: ";
if (!shmop_delete($shm_id)) {
    echo "failed\n";
} else {
    echo "ok\n";
}
shmop_close($shm_id);
示例#26
0
 /**
  * Loads the database file into shared memory.
  *
  * @param string $filename Path to database file to read into shared memory.
  *
  * @return void
  *
  * @throws CException     - if unable to read the db file.
  */
 protected function loadSharedMemory($filename)
 {
     $fp = fopen($filename, "rb");
     if (!$fp) {
         throw new CException("Unable to open file: {$filename}");
     }
     $s_array = fstat($fp);
     $size = $s_array['size'];
     if ($shmid = @shmop_open(self::SHM_KEY, "w", 0, 0)) {
         shmop_delete($shmid);
         shmop_close($shmid);
     }
     if ($shmid = @shmop_open(self::SHM_KEY, "c", 0644, $size)) {
         $offset = 0;
         while ($offset < $size) {
             $buf = fread($fp, 524288);
             shmop_write($shmid, $buf, $offset);
             $offset += 524288;
         }
         shmop_close($shmid);
     }
     fclose($fp);
 }
示例#27
0
 /**
  * @return bool
  */
 public function flush()
 {
     shmop_delete(self::$id);
     return true;
 }
示例#28
0
 /**
  * Destroy thread context and free relative resources.
  * 
  * @return void
  */
 private function _cleanProcessContext()
 {
     shmop_delete($this->_internalIpcKey);
     shmop_delete($this->_internalSemKey);
     shmop_close($this->_internalIpcKey);
     shmop_close($this->_internalSemKey);
     @unlink($this->_ipcSegFile);
     @unlink($this->_ipcSemFile);
     $this->_isRunning = false;
     $this->_pid = null;
 }
 private function Delete()
 {
     return shmop_delete($this->mShmId);
 }
 public function clear()
 {
     if (false === $this->m_segmentId) {
         throw new Exception_IllegalState('memory/shared/shmop', 'Can not clear a closed shared memory segment.');
     }
     $this->m_offset = 0;
     $this->m_capacity = 0;
     return shmop_delete($this->m_segmentId);
 }