zoukankan      html  css  js  c++  java
  • key_t键和ftok函数(转)

    其实更加应该提醒我们的是,可以使用grep等工具去自己search the answer!

     

    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

    ftok的典型实现调用stat函数,然后组合以下三个值:
    1.pathname所在的文件系统的信息(stat结构的st_dev成员)
    2.该文件在本文件系统内的索引节点号(stat结构的st_ino成员)
    3. proj_id的低序8位(不能为0)
    从程序运行的结果可以看出,ftok调用返回的整数IPC键由proj_id的低序8位,st_dev成员的低序8位,st_info的低序16位组合而成。
     
    不能保证两个不同的路径名与同一个proj_id的组合产生不同的键,因为上面所列三个条目(文件系统标识符、索引节点、proj_id)中的信息位数可能大于一个整数的信息位数。
  • 相关阅读:
    2021,6,10 xjzx 模拟考试
    平衡树(二)——Treap
    AtCoder Beginner Contest 204 A-E简要题解
    POJ 2311 Cutting Game 题解
    Codeforces 990G GCD Counting 题解
    NOI2021 SDPTT D2T1 我已经完全理解了 DFS 序线段树 题解
    第三届山东省青少年创意编程与智能设计大赛总结
    Luogu P6042 「ACOI2020」学园祭 题解
    联合省选2021 游记
    Codeforces 1498E Two Houses 题解 —— 如何用结论吊打标算
  • 原文地址:https://www.cnblogs.com/lihaozy/p/2626391.html
Copyright © 2011-2022 走看看