IPC机制key值的各位组成

key_t ftok(const char *_pathname, int _proj_id)

key值的第31~24位为ftok()第二个参数的低8位;

key值的第23~16位为ftok()第一个参数文件属性的st_dev成员的低8位;

key值的第15~0位为ftok()第一个参数文件属性的st_ino属性的低16位。

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

int main(int argc, char *argv[])
{
    key_t key;
    int i;
    struct stat buf;
    if(argc!=3)
    {
        printf("use: command path number\n");
        return 1;
    }
    i=atoi(argv[2]);
    if(stat(argv[1],&buf)==-1)
    {
        perror("stat");
        exit(EXIT_FAILURE);
    }
    printf("file st_dev=%x\n",buf.st_dev);
    printf("file st_ino=%x\n",buf.st_ino);
    printf("number=%x\n",i);
    key=ftok(argv[1],i);
    printf("key=0x%x\tkey>>24=%x\tkey&0xffff=%x\t(key>>16)&0xff=%x\n",key,key>>24,key&0xffff,(key>>16)&0xff);
    return 0;

}

posted on 2014-06-12 20:00  lakeone  阅读(881)  评论(0编辑  收藏  举报

导航