系统建立IPC通讯(如消息队列、共享内存时)必须指定一个ID值。通常情况下,该id值通过ftok函数得到。
ftok原型如下:
key_t ftok( char * fname, int id )

fname就时你指定的文件名(该文件必须是存在而且可以访问的),id是子序号,虽然为int,但是只有8个比特被使用(0-255)。

当成功执行的时候,一个key_t值将会被返回,否则 -1 被返回。

在一般的UNIX实现中,是将文件的索引节点号取出,前面加上子序号得到key_t的返回值。如指定文件的索引节点号为65538,换算成16进制为
0x010002,而你指定的ID值为38,换算成16进制为0x26,则最后的key_t返回值为0x26010002。
查询文件索引节点号的方法是: ls -i

以下为测试程序:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>

#define IPCKEY 0x11
int main( void )
{
    int i=0;
    for ( i = 1; i < 256; ++ i )
        printf( "key = %x\n", ftok( "/tmp", i ) );

return 0;
}

在成功获取到key之后,就可以使用该key作为某种方法的进程间通信的key值,例如shmget共享内存的方式。

shmget的函数原型为

int shmget( key_t, size_t, flag);

在创建成功后,就返回共享内存的描述符。在shmget中使用到的key_t就是通过ftok的方式生成的

实例:

#include <sys/shm.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#define SIZE 1024

extern int errno;

int main()
{
int shmid;
char *shmptr;

//创建共享内存
if((shmid = shmget(IPC_PRIVATE, SIZE, 0600)) < 0)
   {
    printf("shmget error:%s\n", strerror(errno));
    return -1;
   }

//将共享内存连接到 可用地址上

if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1)
{
    printf("shmat error:%s\n", strerror(errno));
    return -1;
}
memcpy(shmptr, "hello world", sizeof("hello world"));
printf("share memory from %lx to %lx, content:%s\n",(unsigned long)shmptr, (unsigned long)(shmptr + SIZE), shmptr);

//拆卸共享内存
if((shmctl(shmid, IPC_RMID, 0) < 0))
{
    printf("shmctl error:%s\n", strerror(errno));
    return -1;
}
}

多进程之间共享内存情况:

#include <sys/shm.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define SIZE 1024

extern int errno;

int main()
{
int shmid;
char *shmptr;
key_t key;
pid_t pid;

if((pid = fork()) < 0)
{
    printf("fork error:%s\n", strerror(errno));
    return -1;
   }
else if(pid == 0)
   {
     sleep(2);
    if((key = ftok("/dev/null", 1)) < 0)
    {
      printf("ftok error:%s\n", strerror(errno));
      return -1;
    }
if((shmid = shmget(key, SIZE, 0600)) < 0)
{
    printf("shmget error:%s\n", strerror(errno));
    exit(-1);
   }

if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1)
{
    printf("shmat error:%s\n", strerror(errno));
    exit(-1);
}
//memcpy(shmptr, "hello world", sizeof("hello world"));
printf("child:pid is %d,share memory from %lx to %lx,
content:%s\n",getpid(), (unsigned long)shmptr, (unsigned long)(shmptr +
SIZE
), shmptr);
printf("child process sleep 2 seconds\n");
sleep(2);
if((shmctl(shmid, IPC_RMID, 0) < 0))
{
    printf("shmctl error:%s\n", strerror(errno));
    exit(-1);
}
   exit(0);
   }
   //parent
   else
   {
    if((key = ftok("/dev/null", 1)) < 0)
    {
      printf("ftok error:%s\n", strerror(errno));
      return -1;
    }
if((shmid = shmget(key, SIZE, 0600|IPC_CREAT|IPC_EXCL)) < 0)

{
    printf("shmget error:%s\n", strerror(errno));
    exit(-1);
   }

if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1)
{
    printf("shmat error:%s\n", strerror(errno));
    exit(-1);
}
memcpy(shmptr, "hello world", sizeof("hello world"));
printf("parent:pid is %d,share memory from %lx to %lx,
content:%s\n",getpid(),(unsigned long)shmptr, (unsigned long)(shmptr +
SIZE
), shmptr);
printf("parent process sleep 2 seconds\n");
sleep(2);
if((shmctl(shmid, IPC_RMID, 0) < 0))
{
    printf("shmctl error:%s\n", strerror(errno));
    exit(-1);
}
   }

waitpid(pid,NULL,0);
exit(0);
}

输出为:

shmctl(shmid, IPC_RMID, 0)的作用是从系统中删除该恭喜存储段。因为每个共享存储段有一个连接计数(shmid_ds结构中的shm_nattch),所以除非使用该段的最后一个进程终止与该段脱接,否则不会实际上删除该存储段

key_t键和ftok函数的更多相关文章

  1. 【IPC通信】key_t键和ftok函数

    System V IPC分为三种: System V消息队列 System V信号量 System V共享内存区 这三种类型的IPC使用key_t值做为它们的名字. key_t这个数据类型在<s ...

  2. ftok()函数深度解析

    [转载] 原文链接:https://blog.csdn.net/u013485792/article/details/50764224 关于ftok函数,先不去了解它的作用来先说说为什么要用它,共享内 ...

  3. ftok函数

    ftok函数 系统建立IPC通讯(消息队列.信号量和共享内存)时必须指定一个ID值.通常情况下,该id值通过ftok函数得到. ftok原型 头文件: #include <sys/types.h ...

  4. ftok函数的使用

    ftok函数的定义:系统建立IPC通讯 (消息队列.信号量和共享内存) 时必须指定一个ID值.通常情况下,该id值通过ftok函数得到. 头文件 #include <sys/types.h> ...

  5. ftok函数例子

    #include <stdio.h>#include <sys/types.h>#include <sys/ipc.h>int main( void ){ int ...

  6. enter键触发的函数

    enter键触发的函数示例: <input type="text" onkeydown="fun();"> function fun() { if( ...

  7. php课程 4-17 数组键值操作函数有哪些

    php课程 4-17  数组键值操作函数有哪些 一.总结 一句话总结:多看学习视频 1.php中数组的键值操作函数有哪6个? • array_values();获取数组中的值• array_keys( ...

  8. mysql中,通过json_insert函数向json字段插入键值?json_insert函数的使用?

    需求描述: 通过json_insert向json字段中插入值,在此进行实验,记录下. 操作过程: 1.查看已经有的包含json数据类型的表 mysql> select * from tab_js ...

  9. jQuery元素属性attr设置多个键值或函数 删除属性removeAttr

    $("Element").attr(name) '取得第一个匹配的属性值,比如$("img").attr("src") $("El ...

随机推荐

  1. iostream/fstream中的输入输出流指针的绑定,tie函数的使用。

      为了兼容c语言的输入输出,c++里面采用tie将输入输出流经行绑定,所以cin/cout并不是独立的.当执行cin时,cout同时会被执行.反之亦然. by defalut,cin is tied ...

  2. 正则表达式的秘籍-b

    一. 正则表达式和其他方法的比较 1.我们一般将谓词和正则表达式配合使用,这是最常用的方法. - (BOOL)validateNumber:(NSString *) textString {     ...

  3. iOS序列化与反序列化

    1到底这个序列化有啥作用? 面向对象的程序在运行的时候会创建一个复杂的对象图,经常要以二进制的方法序列化这个对象图,这个过程叫做Archiving. 二进制流可以通过网络或写入文件中(来源于某教材的一 ...

  4. Linux开启服务器问题(李蕾问题)

    每次启动192.168.1.223服务器时,都要执行这个命令! #:service iptables stop

  5. IIS的Unicode漏洞攻击

    IIS有十多种常见漏洞,但利用得最多的莫过于Unicode解析错误漏洞.微软IIS 4.0/5.0在Unicode字符解码的实现中存在一个安全漏洞,用户可以远程通过IIS执行任意命令.当IIS打开文件 ...

  6. hdu 4815 Little Tiger vs. Deep Monkey

    概率dp,有点像背包的做法: dp[i][j]代表前i个数组成的j数的概率为多少 #include<cstdio> #include<cstring> #define maxn ...

  7. var a=[]; 和 var a=new Array(); 的区别,为什么前者效率高

    因为 JSON格式的语法是引擎直接解释的.而new Array 则需要调用Array的构造器.还有就是1.当你需要将一个数字转化为字符串时可以这样定义:var s=""+1; 这样 ...

  8. POJ2222+暴力搜索

    一共2^15个状态 比较简单 /* 2^15 states */ #include<stdio.h> #include<string.h> #include<stdlib ...

  9. 企业2.0杀出一号种子选手 “Linkwedo”横空出世

    当下,最热门的话题就是企业2.0和1.0的新老交替,在过去的时间里OA在国内几乎是企业1.0的代名词,各大知名OA厂商一直占领着国内的企业市场,但企业2.0在全球越演越烈,甚至大有替代企业1.0的的迹 ...

  10. 学习笔记-[Maven实战]-第三章:Maven使用入门(2)

    使用maven执行编译和测试 1.maven执行编译 (1).在pom.xml上点右键,选择Maven build... (2).在Goals里输入clean complie,执行编译 执行结果: [ ...