PHP Doku:: Erzeugt aus einem Dateipfad und einem Projektbezeichner einen System V IPC Schlüssel - function.ftok.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzErweiterungen zur ProzesskontrolleSemaphore, Shared Memory and IPCSemaphore Funktionenftok

Ein Service von Reinhard Neidl - Webprogrammierung.

Semaphore Funktionen

<<Semaphore Funktionen

msg_get_queue>>

ftok

(PHP 4 >= 4.2.0, PHP 5)

ftok Erzeugt aus einem Dateipfad und einem Projektbezeichner einen System V IPC Schlüssel

Beschreibung

int ftok ( string $pathname , string $proj )

Diese Funktion erzeugt aus dem pathname einer existierenden Datei und einem Projektbezeichner proj einen Integer Schlüsselwert der von Funktionen wie shmop_open() und anderen als System V IPC Schlüssel genutzt werden kann.

Parameter-Liste

pathname

Pfad zu einer existierenden Datei.

proj

Einbuchstabige Projekt-ID

Rückgabewerte

Bei Erfolg wird der erzeugte Schlüssel zurückgegeben, im Fehlerfall ist der Rückgabewert stattdessen -1.

Siehe auch

  • shmop_open() - Erstellt oder öffnet einen gemeinsamen Speicherblock
  • sem_get() - Zugriff auf ein Semaphor anfordern


7 BenutzerBeiträge:
- Beiträge aktualisieren...
Peter MOLNAR
18.05.2007 14:38
mbowie at buzmo dot com wrote:
"The result of this is that if you're using "1" as the id on the PHP side, you'll need to use 49 elsewhere."

You can always use chr() as an alternative.
marco at greenlightsolutions dot nl
17.04.2007 17:08
As ftok uses only the last 16 bits of the inode of the file, you can get collisions on large filesystems. Unfortunately, on large filesystems you can get collisions rather quickly: if you have a collection of 350-400 files, odds are that two of them have inodes with the same last 16 bits. So I've taken to using fileinode instead of ftok with functions like shmop_open.
mbowie at buzmo dot com
20.10.2004 23:41
If you're planning to use ftok() to generate an IPC identifier to share with other applications, note that PHP uses the ASCII value of the proj parameter to generate the key, not the proj (aka id) parameter itself.

The result of this is that if you're using "1" as the id on the PHP side, you'll need to use 49 elsewhere.

This may not be the case under all OS's, but certainly is for FreeBSD which requires the id parameter passed to ftok to be an int.

Also of note, ipcs and ipcrm are extremely useful for debugging SysV queues etc.

References:
http://www.freebsd.org/cgi/man.cgi?query=ftok
http://www.asciitable.com
abk at avatartechnology dot com
17.06.2004 10:17
Thanks to  daniele_dll@yahoo.it who got this in turn from linux glibc 2.3.2: http://www.php.net/manual/en/function.shmop-open.php -- I'm putting this here because it might be helpful to others.

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;
}
david dot rech at virusmedia dot de
27.05.2004 4:50
Missing ftok() on Windows? Here's my little workaround:

<?php
if( !function_exists('ftok') )
{
    function
ftok($filename = "", $proj = "")
    {
        if( empty(
$filename) || !file_exists($filename) )
        {
            return -
1;
        }
        else
        {
           
$filename = $filename . (string) $proj;
            for(
$key = array(); sizeof($key) < strlen($filename); $key[] = ord(substr($filename, sizeof($key), 1)));
            return
dechex(array_sum($key));
        }
    }
}
?>

NOTE: There *may* be duplicate keys, even if probability is low.

The key's were NOT computed like the original UNIX ftok() because i.e. fileinode() is also missing on windows. Normally ftok() computes a key based on the file inode and the system minor id of the harddrive the file resides.

Behaviour is like PHPs ftok(), -1 is returned if file is missing or $filename is empty, computed int as hex on success.

--
Regards,
David Rech
kimaz at swecom dot org
3.05.2004 3:35
You dont have to use ftok() for specifying an System V IPC identifier, though its a good thing to do so.

Passing a regular int to, e.g. msg_get_queue, will have the same effect aslong as you use that value when reading/writing.

I use it for some minor tasks generating small queue's.
andreyKEINSPAM at php dot net
24.04.2004 18:43
This function is not part neither of ext/sysvsem nor ext/sysvshm but comes with the core functions of PHP (from ext/standard).



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)