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
");
        return 1;
    }
    i=atoi(argv[2]);
    if(stat(argv[1],&buf)==-1)
    {
        perror("stat");
        exit(EXIT_FAILURE);
    }
    printf("file st_dev=%x
",buf.st_dev);
    printf("file st_ino=%x
",buf.st_ino);
    printf("number=%x
",i);
    key=ftok(argv[1],i);
    printf("key=0x%x	key>>24=%x	key&0xffff=%x	(key>>16)&0xff=%x
",key,key>>24,key&0xffff,(key>>16)&0xff);
    return 0;

}

原文地址:https://www.cnblogs.com/lakeone/p/3784582.html