ftok file php,PHP ftok 用法 手册 | 示例代码

abk at avatartechnology dot com

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;

}

vlatko dot surlan at evorion dot hr

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.

david dot rech at virusmedia dot de

Missing ftok() on Windows? Here's my little workaround:

{

functionftok($filename="",$proj="")

{

if( empty($filename) !file_exists($filename) )

{

return -1;

}

else

{$filename=$filename. (string)$proj;

for($key= array();sizeof($key)

returndechex(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

mbowie at buzmo dot com

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.

seelts at gmail dot com

abk@avatartechnology.com has copied the code of daniele_dll@yahoo.it

But it is not correct.

The right version is here:

if (!$fileStats) {

return -1;

}

returnsprintf('%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 at greenlightsolutions dot nl

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.

kimaz at swecom dot org

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

This function is not part neither of ext/sysvsem nor ext/sysvshm but comes with the core functions of PHP (from ext/standard).

Peter MOLNAR

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值