Outils pour utilisateurs

Outils du site


informatique:php:shared_memory

Ceci est une ancienne révision du document !


PHP and Shared Memory

Implementations

shmop_*

shmop is to provide a symple interface to shared memory that can be used with OTHER NON php shm creators.

Versions of Windows previous to Windows 2000 do not support shared memory.

Remember, that shared memory is NOT safe against simultaneous access.

shm_*

shm

This extension is not available on Windows platforms.

Remember, that shared memory is NOT safe against simultaneous access. Use semaphores for synchronization.

PHP compilation options :

  • –enable-sysvsem: To enable System V semaphore support
  • –enable-sysvshm: To enable the System V shared memory support
  • –enable-sysvmsg: To enable the System V messages support

Runtime option:

  • sysvshm.init_mem

Essais

Essais01

Création d'une block de mémoire partagée et remplissage avec plusieurs variables. Lecture de ce block et ces variables. Destruction du bloque de mémoire partagée.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Shared Memory - Essais01</title>
  <style type="text/css">
  </style>
  </head>
<body>
 
<h1>Shared Memory - Essais01</h1>
<ul>
  <li><a href="essais01.php?test=WRITER">WRITER</a></li>
  <li><a href="essais01.php?test=READER">READER</a></li>
  <li><a href="essais01.php?test=REMOVE">REMOVE</a></li>
</ul>
 
<?php
 
error_reporting(-1);
 
$action = (isset($_GET['test']) && preg_match('/[a-z]{1,20}/i',$_GET['test']) ) ? $_GET['test'] : null ;
switch($action)
{
  case 'WRITER':
    action_writer();
    break;
  case 'READER':
    action_reader();
    break;
  case 'REMOVE':
    action_remove();
    break;
}
 
function getShmKey()
{
  return ftok(__FILE__, 'A' );
}
 
function action_writer()
{
  echo 'WRITER:';
  echo '<pre>';
  $SHM_KEY = getShmKey();
  $data =  shm_attach( $SHM_KEY, 1024, 0600);
  // plusieurs variables dans un même segment de mémoire.
  $test1 = array("hello","world","1","2","3");
  $test2 = array("hello","world","4","5","6");
  $test3 = array("hello","world","7","8","9");
  shm_put_var( $data, 1, $test1);
  shm_put_var( $data, 2,$test2);
  shm_put_var( $data, 3,$test3);
  print_r(shm_get_var($data, 1));
  print_r(shm_get_var($data, 2));
  print_r(shm_get_var($data, 3));
  shm_detach($data);
  echo '</pre>';
}
 
function action_reader()
{
  echo 'READER:';
  echo '<pre>';
  $SHM_KEY = getShmKey();
  $data =  shm_attach( $SHM_KEY, 1024, 0666);
  print_r(shm_get_var($data, 1));
  print_r(shm_get_var($data, 2));
  print_r(shm_get_var($data, 3));
 shm_detach($data);
  echo '</pre>';
}
 
function action_remove()
{
  echo 'REMOVE:';
  echo '<pre>';
  $SHM_KEY = getShmKey();
  $data =  shm_attach( $SHM_KEY, 1024, 0666);
  shm_remove($data);
  shm_detach($data);
  echo '</pre>';
}
 
?>
 
</body>
</html>

Essais02

Quelle est la limite de mémoire partagée.

Chez OVH c'est < 32Mo.

<code php> <!DOCTYPE HTML PUBLIC “-W3CDTD HTML 4.01 TransitionalEN”> Shared Memory - Essais01

Shared Memory - Essais02

'; $SHM_KEY = getShmKey(); $size = 1024*1024 ; for( $i=0; $i<60; $i++) { $size=intval($size); if( $size==0 ) break; $data = shm_attach( $SHM_KEY, $size, 0600); if( $data == null || $data === FALSE) { echo 'FAILED for size='.formatBytesSize($size)."\n"; break; } else { echo 'OK for size='.formatBytesSize($size)."\n"; shm_remove($data); shm_detach($data); } $size += 1024*1024 ; } echo ''; } /** * @parame int $sizeInBytes * @return string */ function formatBytesSize( $sizeInBytes, $afterColon=2) { // less than 1kB if($sizeInBytes < 1024) { return $sizeInBytes . ' B'; } // between 1kB and 1MB if ($sizeInBytes >= 1024 && $sizeInBytes < 1048576) { return number_format( $sizeInBytes / 1024, $afterColon) . ' KB'; } // between 1MB and 1GB if ($sizeInBytes >= 1048576 && $sizeInBytes < 1073741824) { return number_format( $sizeInBytes / 1048576, $afterColon) . ' MB'; } // more than 1GB return number_format( $sizeInBytes / 1073741824, $afterColon) . ' GB'; } ?> </code

informatique/php/shared_memory.1267464051.txt.gz · Dernière modification : 19/05/2012 00:15 (modification externe)

Sauf mention contraire, le contenu de ce wiki est placé sous les termes de la licence suivante : CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki