Chinaunix首页 | 论坛 | 博客
  • 博客访问: 186627
  • 博文数量: 40
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 418
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-26 22:37
文章存档

2015年(4)

2014年(27)

2013年(9)

我的朋友

分类: LINUX

2014-08-12 22:06:09

ftok函数:将一个路径名和一个project_id转换成为一个system V IPC key的函数
函数原型:
#include
#include
      key_t ftok(const char *pathname, int proj_id);

ftok的主要作用就是生成一个供msgget,semget以及shmget等操作的key值
其生成key值算法如下:
    使用指明路径文件的索引的低16位,proj_id的低8位,以及设备号的低8位。因此,只要设备和proj_id相同,即使文件不同,也很可能因为文件索引的低16位相同而造成相同的返回key值。

以下来自man页:
FTOK(3)                                              Linux Programmer's Manual                                             FTOK(3)

NAME
       ftok - convert a pathname and a project identifier to a System V IPC key

SYNOPSIS
       #include
       #include

       key_t ftok(const char *pathname, int proj_id);

DESCRIPTION
       The  ftok() function uses the identity of the file named by the given pathname (which must refer to an existing, accessible
       file) and the least significant 8 bits of proj_id (which must be nonzero) to generate a key_t type System V IPC key,  suit‐
       able for use with msgget(2), semget(2), or shmget(2).

       The  resulting  value  is  the same for all pathnames that name the same file, when the same value of proj_id is used.  The
       value returned should be different when the (simultaneously existing) files or the project IDs differ.

RETURN VALUE
       On success, the generated key_t value is returned.  On failure -1 is returned, with errno indicating the error as  for  the
       stat(2) system call.

CONFORMING TO
       POSIX.1-2001.

NOTES
       Under libc4 and libc5 (and under SunOS 4.x) the prototype was:

              key_t ftok(char *pathname, char proj_id);

       Today  proj_id  is  an  int, but still only 8 bits are used.  Typical usage has an ASCII character proj_id, that is why the
       behavior is said to be undefined when proj_id is zero.

       Of course no guarantee can be given that the resulting key_t is unique.  Typically, a  best  effort  attempt  combines  the
       given  proj_id byte, the lower 16 bits of the inode number, and the lower 8 bits of the device number into a 32-bit result.
       Collisions may easily happen, for example between files on /dev/hda1 and files on /dev/sda1.

SEE ALSO
       msgget(2), semget(2), shmget(2), stat(2), svipc(7)




阅读(2773) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~