PHP: 共有メモリ関数(shmop) - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<session_write_closeshmop_close>
view the version of this page
Last updated: Tue, 21 Dec 2004

CVII. 共有メモリ関数(shmop)

導入

shmop は、共有メモリセグメントをPHPから簡単に読み書きまたは作成、 削除することを可能にする一連の関数です。Windows上では共有メモリが サポートされないため、これらの関数は動作しません。 しかし、Windows 2000以降、php_shmop.dllphp.iniで有効にすることにより、この機能を有効にすることができま す。

注意: PHP 4.0.3では、以下の関数に接頭辞shmopではな くshmが付いていました。 rather than

要件

これらの関数は、標準モジュールの一部として利用可能であり、常に使用できます。

インストール手順

shmopを使用するには、--enable-shmopパラメータを configure に指 定してPHPをコンパイルする必要があります。

実行用の設定

この拡張モジュールは設定ディレクティブを全く定義しません。

定義済みの定数

この拡張モジュールは定数を全く定義しません。

例 1. 共有メモリ操作の概要

<?php
  
// システムID 0xff3を有する 100 バイトの共有メモリブロックを作成する
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if(!
$shm_id) {
   echo
"共有メモリセグメントを作成できませんでした。\n";
}

// 共有メモリのブロック長を得る
$shm_size = shmop_size($shm_id);
echo
"SHM ブロックサイズ: ".$shm_size. " が作成されました。\n";

// 共有メモリにテスト用の文字列を書き込んでみる
$shm_bytes_written = shmop_write($shm_id, "my shared memory block", 0);
if(
$shm_bytes_written != strlen("my shared memory block")) {
   echo
"データ全体を書き込めませんでした。\n";
}

// その文字列を再び読み込んでみる
$my_string = shmop_read($shm_id, 0, $shm_size);
if(!
$my_string) {
   echo
"共有メモリブロックから読み込めません。\n";
}
echo
"共有メモリ内のデータは次のようになります: ".$my_string."\n";

// ブロックを削除し、共有メモリセグメントを閉じる
if(!shmop_delete($shm_id)) {
   echo
"共有メモリブロックに削除用のマークを付けることができません。";
}
shmop_close($shm_id);
  
?>

目次
shmop_close -- 共有メモリブロックを閉じる
shmop_delete -- 共有メモリブロックを削除する
shmop_open -- 共有メモリブロックを作成またはオープンする
shmop_read -- 共有メモリブロックからデータを読み込む
shmop_size -- 共有メモリブロックの大きさを得る
shmop_write -- 共有メモリブロックにデータを書き込む


add a note add a note User Contributed Notes
共有メモリ関数(shmop)
Craig Manley
06-Jan-2005 11:19
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 AT listbid.com
02-May-2003 07:48
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);
stoimenov at email dot com
24-Jul-2002 12:18
Windows does support shared memory through memory mapped file. Check the following functions for details:

 * CreateFileMapping
 * MapViewOfFile
hackie at misato dot prohost dot org
02-May-2002 01:15
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
medvitz at medvitz dot net
30-Mar-2002 05:53
These functions work on windows.  You have to install as an ISAPI filter, but these functions work great......
rei at prohost dot org
11-Jan-2001 11:16
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.
slavapl at mailandnews dot com
11-Jan-2001 11:02
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.

<session_write_closeshmop_close>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Mon Mar 14 08:13:06 2005 Local time zone must be set--see zic manual page