Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Shared Memory Functions

Shared Memory Functions

Introduction

Shmop is an easy to use set of functions that allows PHP to read, write, create and delete Unix shared memory segments.

Note:

Versions of Windows previous to Windows 2000 do not support shared memory. Under Windows, Shmop will only work when PHP is running as a web server module, such as Apache or IIS (CLI and CGI will not work).

Note:

In PHP 4.0.3, these functions were prefixed by shm rather than shmop.

Requirements

No external libraries are needed to build this extension.

Installation

To use shmop you will need to compile PHP with the --enable-shmop parameter in your configure line.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

Resource Types

Predefined Constants

This extension has no constants defined.

Examples

Example 2230. Shared Memory Operations Overview

<?php
 
// Create 100 byte shared memory block with system id of 0xff3
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if (!
$shm_id) {
   echo
"Couldn't create shared memory segment\n";
}

// Get shared memory block's size
$shm_size = shmop_size($shm_id);
echo
"SHM Block Size: " . $shm_size . " has been created.\n";

// Lets write a test string into shared memory
$shm_bytes_written = shmop_write($shm_id, "my shared memory block", 0);
if (
$shm_bytes_written != strlen("my shared memory block")) {
   echo
"Couldn't write the entire length of data\n";
}

// Now lets read the string back
$my_string = shmop_read($shm_id, 0, $shm_size);
if (!
$my_string) {
   echo
"Couldn't read from shared memory block\n";
}
echo
"The data inside shared memory was: " . $my_string . "\n";

//Now lets delete the block and close the shared memory segment
if (!shmop_delete($shm_id)) {
   echo
"Couldn't mark shared memory block for deletion.";
}
shmop_close($shm_id);
 
?>


Table of Contents

shmop_close — Close shared memory block
shmop_delete — Delete shared memory block
shmop_open — Create or open shared memory block
shmop_read — Read data from shared memory block
shmop_size — Get size of shared memory block
shmop_write — Write data into shared memory block

Code Examples / Notes » ref.shmop

hackie

Your segment probobly doesn't exist. You should probobly be using the c flag...
     "a" for access (sets SHM_RDONLY for shmat) use this flag when you need to open an existing shared memory segment for read only
     "c" for create (sets IPC_CREATE) use this flag when you need to create a new shared memory segment or if a segment with the same key exists, try to open it for read and write


stoimenov

Windows does support shared memory through memory mapped file. Check the following functions for details:
* CreateFileMapping
* MapViewOfFile


slavapl

What you need to realise is that sysvshm is extremly php oriented in it's ability, it's quite a kludge interfacing other NON PHP utilities with it. For example have you tried using sysvshm to read an shm segment NOT created by php? It's not possible, because sysvshm uses a proprietry format, in essense it can ONLY be used within PHP unless of course you take time to figure out this format.
So basically, the purpose of shmop is to provide a symple interface to shared memory that can be used with OTHER NON php shm creators.
Hope this clears it up.


php

The manual states:
"Under Windows, Shmop will only work when PHP is running as a web server module, such as Apache or IIS (CLI and CGI will not work)."
I have tested the php_shmop extension using PHP 5.1.6 CLI and it works fully, despite what the manual says.


rei

The idea behind SHMOP is an easy to use shared memory interface,
without any additional headers added to the shared memory segment
or requiring any special special controls to access the shared memory
segment outside of PHP. SHMOP borrows its api from C's api to shm,
which makes it very easy to use, because it treats shared memory, like C, as    
a file of sorts. This makes it very easy to use even for novices, due to this  
functionality. Most importantly SHMOP uses shm segments to store raw data,
which means you don't need to worry about matching headers, etc... when you are
using C, perl or other programming languages to open/create/read/write shm segments
that were create or are going to be used by PHP. In this it differs from
sysvshm, who's shm interface uses a specialized header, which resides inside
the shared memory segment this adds an unnecessary level of difficulty when
you want to access php shm from external programs.
Also, from my personal tests in Linux 2.2/2.4 and FreeBSD 3.3 SHMOP is about
20% faster then sysvshm, mostly due to fact it does not need to parse the
specialized header and stores the data in raw form.


craig manley

Since there is no mention of the (lack of) need for locking here, I took a look into the shmop.c extensions code. So correct me if I'm wrong, but the shmop.c extension uses memcpy() to copy strings to and from shared memory without any form of locking, and as far as I know, memcpy() is not atomic.
If that's true as I suspect, then these 'easy to use' functions are not so 'easy to use' any more and have to be wrapped in locks (e.g. semaphores, flocks, whatever).


joeldg

Just so you know, the ftok function is probably the best for getting the key.. just so there are not people confused with how they are coming up with these hex codes for the id.
$fsize = filesize("/home/joeldg/testdata");
$fdata = file_get_contents("/home/joeldg/testdata");
$shm_id = shmop_open(ftok("/home/joeldg/testdata", 'R'), "c", 0644, $fsize);


hackie

It's not the job of the shmop extension to provide locking, there are many locking schemes avalible, if you need some sort of atomic operations choose a locking scheme that suits you and use it.

joeldg

I wrote a php memcache back in 2003 as a sort of proof of concept
it is use on a few machines for doing heavy page load caching...
it works very well.
Following are some of the core functions I made
<?          
###############################################
#### shared mem functions
/*
   for debugging these
       use `ipcs` to view current memory
       use `ipcrm -m {shmid}` to remove
       on some systems use `ipcclean` to clean up unused memory if you
       don't want to do it by hand
*/
###############################################
   function get_key($fsize, $file){
       if(!file_exists(TMPDIR.TMPPRE.$file)){
           touch(TMPDIR.TMPPRE.$file);
       }
       $shmkey = @shmop_open(ftok(TMPDIR.TMPPRE.$file, 'R'), "c", 0644, $fsize);
       if(!$shmkey) {
               return false;
       }else{
           return $shmkey;
       }//fi
   }
   function writemem($fdata, $shmkey){
       if(MEMCOMPRESS && function_exists('gzcompress')){
           $fdata = @gzcompress($fdata, MEMCOMPRESSLVL);
       }
       $fsize = strlen($fdata);
       $shm_bytes_written = shmop_write($shmkey, $fdata, 0);
       updatestats($shm_bytes_written, "add");
       if($shm_bytes_written != $fsize) {
               return false;
       }else{
           return $shm_bytes_written;
       }//fi
   }
   function readmem($shmkey, $shm_size){
       $my_string = @shmop_read($shmkey, 0, $shm_size);
       if(MEMCOMPRESS && function_exists('gzuncompress')){
           $my_string = @gzuncompress($my_string);
       }
       if(!$my_string) {
               return false;
       }else{
           return $my_string;
       }//fi
   }
   function deletemem($shmkey){
       $size = @shmop_size($shmkey);
       if($size > 0){ updatestats($size, "del"); }
       if(!@shmop_delete($shmkey)) {
           @shmop_close($shmkey);
               return false;
       }else{
           @shmop_close($shmkey);
           return true;
       }
   }
   function closemem($shmkey){
       if(!shmop_close($shmkey)) {
               return false;
       }else{
           return true;
       }
   }
   function iskey($size, $key){
       if($ret = get_key($size, $key)){
           return $ret;
       }else{
           return false;
       }
   }
################################################
?>


roy
I have written a script to highlight the superiority of shared memory storage.
Although it doesn't use the shmop function, the underlying concept is similar.
'/shm_dir/' is a tmpfs directory, which is based on shared memory, that I have mounted on the server.
Below is the result on an Intel Pentium VI 2.8 server:
IO test on 1000 files
IO Result of Regular Directory : 0.079015016555786
IO Result of Shared Memory Directory : 0.047761917114258
IO test on 10000 files
IO Result of Regular Directory : 3.7090260982513
IO Result of Shared Memory Directory : 0.46256303787231
IO test on 40000 files
IO Result of Regular Directory : 117.35703110695 seconds
IO Result of Shared Memory Directory : 2.6221358776093 seconds
The difference is not very apparent nor convincing at 100 files.
But when we step it up a level to 10000 and 40000 files, it becomes pretty obvious that Shared Memory is a better contender.
Script courtesy of http://www.enhost.com
<?
set_time_limit(0);
// Your regular directory. Make sure it is write enabled
$setting['regular_dir'] =  '/home/user/regular_directory/';
// Your shared memory directory.
$setting['shm_dir'] =  '/shm_dir/';
// Number of files to read and write
$setting['files'] =  40000;
function IO_Test($mode)
{
$starttime = time()+microtime();
global $setting;
for($i = 0 ; $i< $setting['files'] ;$i++)
{
$filename = $setting[$mode].'test'.$i.'.txt';
$content = "Just a random content";

// Just some error detection
  if (!$handle = fopen($filename, 'w+'))
  {
echo "Can't open the file ".$filename;
exit;
  }

  if (fwrite($handle, $content ) === FALSE)
  {
  echo "Can't write to file : ".$filename;
  exit;
  }
 
  fclose($handle);

// Read Test
file_get_contents($filename);

}
  $endtime = time()+microtime();
 
  $totaltime = ($endtime - $starttime);
 
  return $totaltime;
}
echo '<b>IO test on '.$setting['files']. ' files</b>
';
echo 'IO Result of <b>Regular</b> Directory : '.IO_Test('regular_dir')   .' seconds
';
echo 'IO Result of <b>Shared Memory</b> Directory : '.IO_Test('shm_dir')       .' seconds
';
/* Removal of files to avoid underestimation
#
# Failure to remove files will result in inaccurate benchmark
# as it will result in the IO_Test function not re-creating the existing files
*/
foreach ( glob($setting['regular_dir']."*.txt") as $filename) {
  unlink($filename);$cnt ++;
}
foreach ( glob($setting['shm_dir']."*.txt") as $filename) {
  unlink($filename);$cnt ++;
}
?>


adamstevenson

Here is a little caching script that uses shared memory.  It provides some examples on how to use some of the functions. http://www.adamstevenson.net/mycached.phps
Helped me to cut down some of my page requests from 2 seconds to .035 seconds.


Change Language


Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
CURL
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Net_Gopher
Haru PDF Functions
hash Functions
HTTP
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
IBM DB2
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
IMAP
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
KADM5
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
oggvorbis
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Radius
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
Semaphore
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker