<!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>