key_t键
System V IPC使用key_t值作为它们的名字,在Redhat linux(后续验证默认都在该平台下)下key_t被定义为int类型,追溯如下:
/usr/include/sys/ipc.h
#ifndef __key_t_defined
typedef __key_t key_t;
#define __key_t_defined
#endif
/usr/include/bits/types.h
typedef __DADDR_T_TYPE __daddr_t; /* The type of a disk address. */
typedef __SWBLK_T_TYPE __swblk_t; /* Type of a swap block maybe? */
typedef __KEY_T_TYPE __key_t; /* Type of an IPC key */
/usr/include/bits/typesizes.h
#define __KEY_T_TYPE __S32_TYPE
/usr/include/bits/types.h
#define __S32_TYPE int
ftok函数
函数ftok把一个已存在的路径名和一个整数标识得转换成一个key_t值,称为IPC键:
# 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。
该函数把从pathname导出的信息与id的低序8位组合成一个整数IPC键。
sample:
#include "unpipc.h"
int main(int argc, char **argv)
{
struct stat stat;
if (argc != 2)
err_quit("usage: ftok
");
Stat(argv[1], &stat);
printf("st_dev: %lx, st_ino: %lx, key: %x/n",
(u_long) stat.st_dev,(u_long) stat.st_ino,
Ftok(argv[1], 0x57));
exit(0);
}
程序运行结果:
[cbs@linux svipc]$ ./ftok /tmp/mysql.sock
st_dev: 802, st_ino: 34219, key: 57024219
1.pathname所在的文件系统的信息(stat结构的st_dev成员)
2.该文件在本文件系统内的索引节点号(stat结构的st_ino成员)
3. proj_id的低序8位(不能为0)