Linux的ftok源码

Linux的ftok()函数源码


linux中的System V IPC中的get函数需要用到ftok()来生成一个key,闲来无事把ftok()函数实现了一下。

1. ftok的man手册

NAME
    ftok - convert a pathname and a project identifier to a System V IPC key
SYNOPSIS
     #include <sys/types.h>
    #include <sys/ipc.h>

    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, suitable for use with msgget(2),semget(2), or shmget(2).

2. ftok的实现原理

ftok返回的key_t在Linux中是一个32位的值,它通过取proj参数的最低8个有效位、包含pathname指定文件所属的文件系统的设备的次要设备号的最低8个有效位以及pathname所指定文件的i-node号的最低16个有效位组成。

3. ftok()源码

/*************************************************************************
    > File Name: ipc_ftok.c
    > Author: lxg
    > Mail: liuxingen@nsfocus.com
    > Created Time: 2015年08月01日 星期六 16时45分48秒
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/ipc.h>

#define PROJ_MASK 0x000000ff
#define INODE_MASK 0x0000ffff
#define MINOR_MASK 0x000000ff

key_t ipc_ftok(const char *pathname, int proj_id)
{
    struct stat buf;
    long minor_id = 0L, inode_id = 0L;
    key_t key = 0;

    if(stat(pathname, &buf) == -1 || !proj_id)
    {
        return -1;
    }
    minor_id = (long)minor(buf.st_dev);
    inode_id = (long)buf.st_ino;

    key = inode_id & INODE_MASK;
    key |= (minor_id & MINOR_MASK) << 16;
    key |= (proj_id & PROJ_MASK) << 24; 

    return key;
}

int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        printf("%s pathname proj_id\n", argv[0]);
        return 1;
    }

    printf("ipc_ftok = 0x%x, ftok = 0x%x\n", ipc_ftok(argv[1], atoi(argv[2])), ftok(argv[1], atoi(argv[2])));

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值