downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

shmop_read> <shmop_delete
Last updated: Fri, 13 Nov 2009

view this page in

shmop_open

(PHP 4 >= 4.0.4, PHP 5)

shmop_open共有メモリブロックを作成またはオープンする

説明

int shmop_open ( int $key , string $flags , int $mode , int $size )

shmop_open() は共有メモリブロックを作成または オープンします。

パラメータ

key

共有メモリブロックのシステム ID であり、10 進数または 16 進数で指定することが可能です。

flags

フラグに設定できる内容は、次のとおりです。

  • "a" アクセス用(shmat に SHM_RDONLY を設定する) 既存の共有メモリセグメントを読み込み専用でオープンする必要がある場合に このフラグを使用してください。
  • "c" 作成用(IPC_CREATE を設定する) 新規に共有メモリセグメントが必要な場合にこのフラグを使用してください。 もし同じキーのセグメントがすでに存在する場合、それを読み書きモードで オープンしようと試みます。
  • "w" 読み込み & 書き込みアクセス用 共有メモリセグメントの読み込みや書き込みの必要がある場合にこのフラグを 使用してください。たいていの場合はこのフラグを使用します。
  • "n" 新規メモリセグメントの作成用(IPC_CREATE|IPC_EXCL を設定する) 新規に共有メモリセグメントが必要で、もし同じフラグのセグメントが 存在するときには失敗させたい場合にこのフラグを使用してください。 セキュリティを確保するために、このフラグは有用です。これを使用する ことで、条件の競合による問題を避けることが可能です。

mode

共有メモリセグメントに設定したい 許可属性で、ファイルに関する許可属性と同様なものです。許可属性は、 例えば 0644 のような 8 進数形式で渡す必要があります。

size

作成したい共有メモリブロックの大きさをバイト数で指定します。

注意: 注意: 既存のメモリセグメントをオープンする場合には、3 番目および 4 番目の引数には 0 を指定する必要があります。成功時に

返り値

成功した場合は、shmop_open() は 作成した共有メモリセグメントにアクセスするために使用する ID を返します。 失敗した場合に FALSE を返します。

例1 共有メモリブロックを新規に作成する

<?php
$shm_key 
ftok(__FILE__'t');
$shm_id shmop_open($shm_key"c"0644100);
?>

この例は、ftok() が返すシステム ID の共有メモリブロックをオープンします。

参考



shmop_read> <shmop_delete
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
shmop_open
DinosauRUS
29-Aug-2008 10:24
If you faced with any problem you're going to solve with shared memmory, but your server doesn't support it, you can use files instead. I've wrote simple wrapper for this and its suites for me. Hope it will be usefull for you too.

<?php

define
(kSHARED_FOLDER, "shared/");
define(kSHARED_MAX_ATTEMPS, 10);
define(kSESSION_SHARED, "shared_");

class
Shared {
    var
$id = 0;
    var
$filename = '';
    var
$filepointer;
   
    var
$data = array();
    var
$date = 0;
   
    function
Shared($id) {
       
$this->id = $id;
       
       
$this->filename = kSHARED_FOLDER.$this->id;
       
        if(empty(
$this->filename))
        {
            print
"no filename";
            return
false;       
        }
       
       
$this->date = $_SESSION[kSESSION_SHARED.$id];
           
    }
   
    function
clear() {
        if (
$this->id == null)
        {
            return
false;
        }
           
       
$counter = 0;
       
ignore_user_abort(true);
        if((
$this->filepointer = @fopen($this->filename, "w")) == false) {       
           
ignore_user_abort(false);
            return
false;
        }

        while(
true) {
            if (
$counter >= kSHARED_MAX_ATTEMPS) {
               
fclose($this->filepointer);
               
ignore_user_abort(false);
                return
false;
            }
           
            if(
flock($this->filepointer, LOCK_EX) == false) {
               
$counter++;
               
usleep(rand(1, 25000));
            }
            else
                break;
        }

        if(
flock($this->filepointer, LOCK_UN) == false) {       
           
ignore_user_abort(false);
            return
false;
        }
       
        unset(
$this->data);
       
$this->data = array();

       
fclose($this->filepointer);
       
$this->date = $_SESSION[kSESSION_SHARED.$id] = filemtime($this->filename);
       
ignore_user_abort(false);
       
        return
true;
    }
   
    function
setObjectForKey($value, $key) {
        if (
$this->id == null)
            return
false;
           
       
$counter = 0;
       
ignore_user_abort(true);
        if((
$this->filepointer = @fopen($this->filename, "a+")) == false) {       
           
ignore_user_abort(false);
            print
"can not open file<br>";
            return
false;
        }

        while(
true) {
            if (
$counter >= kSHARED_MAX_ATTEMPS) {
               
fclose($this->filepointer);
                print(
"1 aborted...");
               
ignore_user_abort(false);
                return
false;
            }
           
           
$block;
            if(
flock($this->filepointer, LOCK_EX, $block) == false) {
               
$counter++;
                print(
"1 waiting...");
               
usleep(rand(1, 25000));
            }
            else
                break;
        }
       
       
$data = file_get_contents($this->filename);
       
$array = array();
        if (!empty(
$data))
           
$array = unserialize($data);

       
$array[$key] = $value;
       
$data = serialize($array);
       
ftruncate($this->filepointer, 0);
       
fseek($this->filepointer, 0, SEEK_SET);
       
fwrite($this->filepointer, $data);
       
       
$this->data = $array;
       
        if(
flock($this->filepointer, LOCK_UN) == false) {       
           
ignore_user_abort(false);
            return
false;
        }

       
fclose($this->filepointer);
       
$this->date = $_SESSION[kSESSION_SHARED.$id] = filemtime($this->filename);
       
ignore_user_abort(false);
       
        return
true;
    }
   
    function
getObjectForKey($key)    {
        if (
$this->id == null)
            return
null;
           
       
$counter = 0;
       
ignore_user_abort(true);
   
        if((
$this->filepointer = @fopen($this->filename, "a+")) == false) {       
           
ignore_user_abort(false);
            print(
"can not open<br>");
            return
null;
        }

        if (
$this->date == filemtime($this->filename)) {           
           
fclose($this->filepointer);
            return
$this->data[$key];
        }
       
        while(
true) {
            if (
$counter >= kSHARED_MAX_ATTEMPS) {
               
fclose($this->filepointer);
               
ignore_user_abort(false);
                print(
"2 aborted<br>");
                return
null;
            }
           
            if(
flock($this->filepointerLOCK_SH ) == false) {
               
$counter++;
                print(
"2 waiting...<br>");
               
usleep(rand(1, 25000));
            }
            else
                break;
        }
       
       
fseek($this->filepointer, 0);
       
$data = file_get_contents($this->filename);
       
$array = array();
        if (!empty(
$data))
           
$array = unserialize($data);
           
       
$data = $array[$key];
       
$this->data = $array;
       
        if(
flock($this->filepointer, LOCK_UN) == false) {       
           
ignore_user_abort(false);
            return
$data;
        }

       
fclose($this->filepointer);
       
$this->date = $_SESSION[kSESSION_SHARED.$id] = filemtime($this->filename);
       
ignore_user_abort(false);
       
        return
$data;
    }
}
?>
erelsgl at gmail dot com
27-Nov-2007 11:04
=== Checking if a shared memory exists ===
The solution provided by Mitchell_Shnier at ieee dot orgZ doesn't work on my computer - I get a warning "Invalid flag ac".

In order to check if a shared-memory exists, you just have to open it with the "a" or "w" flag, while hiding the warnings using the "@" operator:
<?php
@$shid = shmop_open($systemId, "a", 0666, 0);
if (!empty(
$shid)) {
            ...
shared memory exists
} else {
            ...
shared memory doesnt exist
}
?>
Craig Manley
06-Jan-2005 01:19
To: macmaster at pobox dot com:

To clear up some new confusion: you said the shm key is 8 bytes long. As far as I know it's 4 bytes (32bits).
Check out the output of ipcs on Linux below to see what I mean.

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status     
0x6e6a694d 65538      mijnbel   644        65536      0                      
0x326e794d 98307      mijnbel   644        65536      0                      
0x62417347 131076     smsklap   644        65536      0
me at here dot com
08-Dec-2004 01:04
Just an alternative idea if 'shared memory' is what you need for your websites, you can use tmpfs (on Linux):

Get root to do this:
mkdir /home/myname/tmpfs
chown myname:mygroup /home/myname/tmpfs
..and this in a script executed at boot time:
mount -t tmpfs /mnt/tmpfs /home/myuser/tmpfs

Now you can use regular file functions (including locking) to access shared memory between all your processes.

More info: http://docsun.cites.uiuc.edu/sun_docs/C/
...and this because the note editor doesn't accept long lines...
solaris_9/SUNWaadm/SYSADV1/p150.html
daniele_dll at yahoo dot it
01-Feb-2004 09:28
There is a little ftok function. This function isn't included into php for windows so i've grabbed it directly from linux glibc 2.3.2 source code. I hope that this can be useful.
There is the code:

<?php
function ftok($pathname, $proj_id) {
   
$st = @stat($pathname);
    if (!
$st) {
        return -
1;
    }
   
   
$key = sprintf("%u", (($st['ino'] & 0xffff) | (($st['dev'] & 0xff) << 16) | (($proj_id & 0xff) << 24)));
    return
$key;
}

echo
ftok($_SERVER["SCRIPT_FILENAME"], 250);
?>

sorry for my english :)
Chris Petersen
28-Aug-2003 06:18
Be warned that if you try to shmop_open with a key set to zero, shmop_open will seemingly work, and you can write to it, but you will not be able to read from it or delete it.  If you're not careful, you can continue doing this - creating more and more shared memory blocks at "zero" until eventually you WILL start getting errors saying that php can't access or create the shared memory block, and you will have to restart your machine to free up all of those "zero" blocks.
Mitchell_Shnier at ieee dot orgZ
08-Nov-2001 01:51
To check whether a particular shared memory segment is already created, you need to concatenate the "a" and "c" flags. For example (where $SystemKey is the Unix key used by the other process(es) with which you want to share this memory segment)...<BR>
$shm_id = shmop_open($SystemKey, "ac", 0, 0);
if ($shm_id) {
   #it is already created
} else {
   #you need to create it with shmop_open using "c" only
}<BR>
Using only "a" does not work (just as using only IPC_EXCL in the Unix shmget() call is meaningless). Also, use the ipcs shell command to see your shared memory segments.
macmaster at pobox dot com
30-Mar-2001 08:15
the key is a LONG variable type, meaning that the key can only be eight (8) bytes long, which can be too short if you're using any form of automagic key generation (like a parsed filename)

shmop_read> <shmop_delete
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites