Outils pour utilisateurs

Outils du site


informatique:php:shared_memory:essais01
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
  <head>
  <meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot;>
  <title>Shared Memory - Essais01</title>
  <style type=&quot;text/css&quot;>
  </style>
  </head>
<body>
 
<h1>Shared Memory - Essais01</h1>
<ul>
  <li><a href=&quot;essais01.php?test=WRITER&quot;>WRITER</a></li>
  <li><a href=&quot;essais01.php?test=READER&quot;>READER</a></li>
  <li><a href=&quot;essais01.php?test=REMOVE&quot;>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(&quot;hello&quot;,&quot;world&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;);
  $test2 = array(&quot;hello&quot;,&quot;world&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;);
  $test3 = array(&quot;hello&quot;,&quot;world&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;);
  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>
informatique/php/shared_memory/essais01.txt · Dernière modification : de 127.0.0.1

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