Thanks to [email protected] 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;
}
PHP.mk документација
ftok
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
function.ftok.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
function.ftok.php
ftok
Референца за `function.ftok.php` со подобрена типографија и навигација.
ftok
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
ftok — (PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
= NULL
Конвертира име на патека и идентификатор на проект во System V IPC клуч filename Функцијата конвертира
integer на постоечка достапна датотека и идентификатор на проект во
shmop_open() за употреба со, на пример,
Параметри
filename- и други System V IPC клучеви.
project_id- Патека до достапна датотека.
Вратени вредности
Идентификатор на проект. Ова мора да биде еднокарактерен стринг.
-1 се враќа.
Види Исто така
- shmop_open() - Креирај или отвори блок за споделена меморија
- sem_get() - Земи ID на семафор
Белешки од корисници 6 белешки
На успех, вратената вредност ќе биде креираната вредност на клучот, инаку ¶
21 години пред
abk at avatartechnology dot com ¶
12 години пред
The rather unintuitive usage of ftok with shm related functions like shmop_open and shm_attach could simply be explained as a need to avoid shm key collisions. Using ftok with a file that belongs to your project is likely to generate a unique key. Additionally, using ftok with a file from your project avoids the need to store the key so that other processes can access the segment because ftok will always give you the same key if you pass the same file.
vlatko dot surlan at evorion dot hr ¶
21 години пред
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
david dot rech at virusmedia dot de ¶
21 години пред
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
mbowie at buzmo dot com ¶
пред 11 години
[email protected] has copied the code of [email protected]
But it is not correct.
The right version is here:
<?php
function ftok ($filePath, $projectId) {
$fileStats = stat($filePath);
if (!$fileStats) {
return -1;
}
return sprintf('%u',
($fileStats['ino'] & 0xffff) | (($fileStats['dev'] & 0xff) << 16) | ((ord($projectId) & 0xff) << 24)
);
}
?>
The difference is that $projectId string should be used as ASCII value via ord() function. Otherwise it will be interpreted as 0.
marco на greenlightsolutions dot nl ¶
пред 18 години
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.