informatique:php:shared_memory
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| informatique:php:shared_memory [01/03/2010 18:20] – cyrille | informatique:php:shared_memory [19/05/2012 00:18] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 32: | Ligne 32: | ||
| 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. | 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. | ||
| - | <code php> | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | <meta http-equiv=" | ||
| - | < | ||
| - | <style type=" | ||
| - | </ | ||
| - | </ | ||
| - | < | ||
| - | < | + | [[/informatique/php/shared_memory/essais01|code source Essais01]] |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | + | ||
| - | <?php | + | |
| - | + | ||
| - | error_reporting(-1); | + | |
| - | + | ||
| - | $action = (isset($_GET[' | + | |
| - | switch($action) | + | |
| - | { | + | |
| - | case ' | + | |
| - | action_writer(); | + | |
| - | break; | + | |
| - | case ' | + | |
| - | action_reader(); | + | |
| - | break; | + | |
| - | case ' | + | |
| - | action_remove(); | + | |
| - | break; | + | |
| - | } | + | |
| - | + | ||
| - | function getShmKey() | + | |
| - | { | + | |
| - | return ftok(__FILE__, | + | |
| - | } | + | |
| - | + | ||
| - | function action_writer() | + | |
| - | { | + | |
| - | echo ' | + | |
| - | echo '< | + | |
| - | $SHM_KEY = getShmKey(); | + | |
| - | $data = shm_attach( $SHM_KEY, 1024, 0600); | + | |
| - | // plusieurs variables dans un même segment de mémoire. | + | |
| - | $test1 = array(" | + | |
| - | $test2 = array(" | + | |
| - | $test3 = array(" | + | |
| - | shm_put_var( $data, 1, $test1); | + | |
| - | shm_put_var( $data, 2, | + | |
| - | shm_put_var( $data, 3, | + | |
| - | print_r(shm_get_var($data, | + | |
| - | print_r(shm_get_var($data, | + | |
| - | print_r(shm_get_var($data, | + | |
| - | shm_detach($data); | + | |
| - | echo '</ | + | |
| - | } | + | |
| - | + | ||
| - | function action_reader() | + | |
| - | { | + | |
| - | echo ' | + | |
| - | echo '< | + | |
| - | $SHM_KEY = getShmKey(); | + | |
| - | $data = shm_attach( $SHM_KEY, 1024, 0666); | + | |
| - | print_r(shm_get_var($data, | + | |
| - | print_r(shm_get_var($data, | + | |
| - | print_r(shm_get_var($data, | + | |
| - | | + | |
| - | echo '</ | + | |
| - | } | + | |
| - | + | ||
| - | function action_remove() | + | |
| - | { | + | |
| - | echo ' | + | |
| - | echo '< | + | |
| - | $SHM_KEY = getShmKey(); | + | |
| - | $data = shm_attach( $SHM_KEY, 1024, 0666); | + | |
| - | shm_remove($data); | + | |
| - | shm_detach($data); | + | |
| - | echo '</ | + | |
| - | } | + | |
| - | + | ||
| - | ?> | + | |
| - | + | ||
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | </ | + | |
| ==== Essais02 ==== | ==== Essais02 ==== | ||
| Ligne 130: | Ligne 41: | ||
| Chez OVH c'est < 32Mo. | Chez OVH c'est < 32Mo. | ||
| - | <code php> | + | [[/informatique/php/shared_memory/essais02|code source |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | <meta http-equiv=" | + | |
| - | < | + | |
| - | <style type=" | + | |
| - | </ | + | |
| - | </ | + | |
| - | < | + | |
| - | + | ||
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | + | ||
| - | <?php | + | |
| - | + | ||
| - | error_reporting(-1); | + | |
| - | + | ||
| - | $action = (isset($_GET[' | + | |
| - | switch($action) | + | |
| - | { | + | |
| - | case ' | + | |
| - | action_searchmaxmem(); | + | |
| - | break; | + | |
| - | } | + | |
| - | + | ||
| - | function getShmKey() | + | |
| - | { | + | |
| - | return ftok(__FILE__, | + | |
| - | } | + | |
| - | + | ||
| - | function action_searchmaxmem() | + | |
| - | { | + | |
| - | echo ' | + | |
| - | echo '< | + | |
| - | $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 ' | + | |
| - | break; | + | |
| - | } | + | |
| - | else | + | |
| - | { | + | |
| - | echo 'OK for size=' | + | |
| - | shm_remove($data); | + | |
| - | shm_detach($data); | + | |
| - | } | + | |
| - | $size += 1024*1024 ; | + | |
| - | } | + | |
| - | echo '</ | + | |
| - | } | + | |
| - | + | ||
| - | /** | + | |
| - | * @parame int $sizeInBytes | + | |
| - | * @return string | + | |
| - | */ | + | |
| - | function formatBytesSize( $sizeInBytes, | + | |
| - | { | + | |
| - | // 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 : (modification externe)
