免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5991 | 回复: 17
打印 上一主题 下一主题

[函数] ftok()函数在不同平台下实现的兼容性 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-12-03 17:31 |只看该作者 |倒序浏览
因为在开发中涉及多种系统平台,在系统移植时发现ftok()函数在不同平台下存在一定的差异性。当然,根本原因不在于ftok()本身,而应该是操作系统对于文件系统管理的差异性。

测试代码如下:
  1. #include <stdio.h>;
  2. #include <sys/ipc.h>;

  3. main()
  4. {
  5.     sprintf( "key=%0x\n", ftok( "aaa.txt", 1000 ) );
  6. }
复制代码


测试涉及的操作系统:RedHat AS3U3、Sco OpenServer 5.0.6、UnixWare 7.1.1、Solaris 9 x86 u7。

在RH AS3环境下,对aaa.txt进行修改后程序的输出值都有变化,说明该文件在修改后存储位置发生了变化。

在Sco OpenServer 5.0.6、UnixWare 7.1.1、Solaris 9 x86 u7下执行相同的程序,无论对aaa.txt的文件内容做何种修改都对程序的输出无影响。

因此,如果在linux下通过ftok()产生ipc键值、且ftok()与配置文件相关,则在更改了配置文件后必须将应用重起。否则将导致不可预料的后果!

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2004-12-03 17:35 |只看该作者

ftok()函数在不同平台下实现的兼容性

I 服了 U!
IPC 本来就是在同一台机器上通讯用的嘛!

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2004-12-03 17:45 |只看该作者

ftok()函数在不同平台下实现的兼容性

刚才试了一下,
RH9 下修改了 f 确实会影响 k,
再看 RH9 下对 ftok 的 man:
  1. FTOK(3)                    Linux Programmer's Manual                   FTOK(3)

  2. NAME
  3.        ftok  -  convert  a pathname and a project identifier to a System V IPC
  4.        key

  5. SYNOPSIS
  6.        # include <sys/types.h>;
  7.        # include <sys/ipc.h>;

  8.        key_t ftok(const char *pathname, int proj_id);

  9. DESCRIPTION
  10.        The ftok function uses the identity of the  file  named  by  the  given
  11.        pathname  (which  must  refer  to an existing, accessible file) and the
  12.        least significant 8 bits of proj_id (which must be nonzero) to generate
  13.        a  key_t  type  System  V  IPC  key,  suitable  for use with msgget(2),
  14.        semget(2), or shmget(2).

  15.        The resulting value is the same for all pathnames that  name  the  same
  16.        file, when the same value of proj_id is used. The value returned should
  17.        be different when the (simultaneously existing) files  or  the  project
  18.        IDs differ.

  19. RETURN VALUE
  20.        On  success  the  generated  key_t  value is returned. On failure -1 is
  21.        returned, with errno indicating the error as  for  the  stat(2)  system
  22.        call.

  23. CONFORMING TO
  24.        XPG4

  25. NOTES
  26.        Under libc4 and libc5 (and under SunOS 4.x) the prototype was
  27.               key_t ftok(char *pathname, char proj_id);
  28.        Today  proj_id is an int, but still only 8 bits are used. Typical usage
  29.        has an ASCII character proj_id, that is why the behaviour is said to be
  30.        undefined when proj_id is zero.

  31.        Of course no guarantee can be given that the resulting key_t is unique.
  32.        Typically, a best effort attempt combines the given proj_id  byte,  the
  33.        lower  16 bits of the i-node number, and the lower 8 bits of the device
  34.        number into a 32-bit result.  Collisions may easily happen, for example
  35.        between files on /dev/hda1 and files on /dev/sda1.

  36. SEE ALSO
  37.        ipc(5), msgget(2), semget(2), shmget(2), stat(2)

  38. Linux 2.4                         2001-11-28                           FTOK(3)
复制代码

按照我对 “ The resulting value is the same for all pathnames that  name  the  same
       file, ” 的理解,似乎不应该这样,
是不是可以认定是 RH9 的一个 BUG??

论坛徽章:
0
4 [报告]
发表于 2004-12-03 17:52 |只看该作者

ftok()函数在不同平台下实现的兼容性

真的会有变化哦。试了一下,不过变来变去都是2个值中的一个,我的机器是17460896和17460895

论坛徽章:
0
5 [报告]
发表于 2004-12-03 17:55 |只看该作者

ftok()函数在不同平台下实现的兼容性

平时都是这么用的
key_t ftok(getenv("CWD",,1);
这样应该没问题了吧?

论坛徽章:
0
6 [报告]
发表于 2004-12-03 21:54 |只看该作者

ftok()函数在不同平台下实现的兼容性

一般来说,ftok对应文件的inode,因此,无论多少连接,指向的都是同一个inode。我觉得这句话应该是这样理解的。

论坛徽章:
0
7 [报告]
发表于 2004-12-03 22:14 |只看该作者

ftok()函数在不同平台下实现的兼容性

原帖由 "flw" 发表:
按照我对 “ The resulting value is the same for all pathnames that  name  the  same
       file, ” 的理解,似乎不应该这样,
是不是可以认定是 RH9 的一个 BUG??


我也是这样认为的!我还没有在别的linux平台下试验过。有机会的话我都会试以下的。

应该来说如果文件的inode未变,ftok()的值也不应该变。除非是在保存原文件的时候实际做了删除->;创建的操作!

论坛徽章:
0
8 [报告]
发表于 2004-12-03 22:23 |只看该作者

ftok()函数在不同平台下实现的兼容性

[quote]原帖由 "albcamus"]真的会有变化哦。试了一下,不过变来变去都是2个值中的一个,我的机器是17460896和17460895[/quote 发表:



还有一种情况未试:新创建一个文件、然后再对aaa.txt做修改,看输出值是否变化。如果变化的话说明保存文件时确实会改动文件的位置!

论坛徽章:
0
9 [报告]
发表于 2004-12-03 23:09 |只看该作者

ftok()函数在不同平台下实现的兼容性

我觉得改变位置与vi相关吧?

论坛徽章:
0
10 [报告]
发表于 2004-12-04 09:43 |只看该作者

ftok()函数在不同平台下实现的兼容性

[quote]原帖由 "FH"]我觉得改变位置与vi相关吧?[/quote 发表:


如果文件位置确实改变,那的确是由于vi引起的,但其结果倒过来影响到了ftok()的输出。所以我在前头说如果更改了配置文件,而这个配置文件又与ftok()相关的话,那就需要将应用重起。否则将因为不同进程中获得的ipckey不一致而导致程序出错。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP