linux环境编程-ftok()函数详解

雅筎

雅筎

2016-03-31 15:48

下面,图老师小编带您去了解一下linux环境编程-ftok()函数详解,生活就是不断的发现新事物,get新技能~

linux环境编程-ftok()函数详解

   系统建立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

  #include

  #include

  #define IPCKEY 0x11

  int main( void )

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  int i=0;

  for ( i = 1; i 256; ++ i )

  printf( "key = %xn", ftok( "/tmp", i ) );

  return 0;

  }

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

  shmget的函数原型为

  int shmget( key_t, size_t, flag);

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

  实例:

  #include

  #include

  #include

  #include

  #include

  #define SIZE 1024

  extern int errno;

  int main()

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  int shmid;

  char *shmptr;

  //创建共享内存

  if((shmid = shmget(IPC_PRIVATE, SIZE, 0600)) 0)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("shmget error:%sn", strerror(errno));

  return -1;

  }

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

  if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("shmat error:%sn", strerror(errno));

  return -1;

  }

  memcpy(shmptr, "hello world", sizeof("hello world"));

  printf("share memory from %lx to %lx, content:%sn",(unsigned long)shmptr, (unsigned long)(shmptr + SIZE), shmptr);

  //拆卸共享内存

  if((shmctl(shmid, IPC_RMID, 0) 0))

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("shmctl error:%sn", strerror(errno));

  return -1;

  }

  }

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

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #define SIZE 1024

  extern int errno;

  int main()

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  int shmid;

  char *shmptr;

  key_t key;

  pid_t pid;

  if((pid = fork()) 0)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("fork error:%sn", strerror(errno));

  return -1;

  }

  else if(pid == 0)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  sleep(2);

  if((key = ftok("/dev/null", 1)) 0)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("ftok error:%sn", strerror(errno));

  return -1;

  }

  if((shmid = shmget(key, SIZE, 0600)) 0)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("shmget error:%sn", strerror(errno));

  exit(-1);

  }

  if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("shmat error:%sn", strerror(errno));

  exit(-1);

  }

  //memcpy(shmptr, "hello world", sizeof("hello world"));

  printf("child:pid is %d,share memory from %lx to %lx, content:%sn",getpid(), (unsigned long)shmptr, (unsigned long)(shmptr + SIZE

  ), shmptr);

  printf("child process sleep 2 secondsn");

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  sleep(2);

  if((shmctl(shmid, IPC_RMID, 0) 0))

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("shmctl error:%sn", strerror(errno));

  exit(-1);

  }

  exit(0);

  }

  //parent

  else

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  if((key = ftok("/dev/null", 1)) 0)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("ftok error:%sn", strerror(errno));

  return -1;

  }

  if((shmid = shmget(key, SIZE, 0600|IPC_CREAT|IPC_EXCL)) 0)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("shmget error:%sn", strerror(errno));

  exit(-1);

  }

  if((shmptr = (char*)shmat(shmid, 0, 0)) == (void*)-1)

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("shmat error:%sn", strerror(errno));

  exit(-1);

  }

  memcpy(shmptr, "hello world", sizeof("hello world"));

  printf("parent:pid is %d,share memory from %lx to %lx, content:%sn",getpid(),(unsigned long)shmptr, (unsigned long)(shmptr + SIZE

  ), shmptr);

  printf("parent process sleep 2 secondsn");

  sleep(2);

  if((shmctl(shmid, IPC_RMID, 0) 0))

  {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/fuwuqi/)

  printf("shmctl error:%sn", strerror(errno));

  exit(-1);

  }

  }

  waitpid(pid,NULL,0);

  exit(0);

  }

  输出为:

linux环境编程-ftok()函数详解   图老师

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

展开更多 50%)
分享

猜你喜欢

linux环境编程-ftok()函数详解

服务器
linux环境编程-ftok()函数详解

Linux 和 Unix 安全编程:环境变量

编程语言 网络编程
Linux 和 Unix 安全编程:环境变量

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

Linux C 函数参考(环境变量 终端控制)

Linux Linux命令 Linux安装 Linux编程 Linux桌面 Linux软件 Linux内核 Linux管理
Linux C 函数参考(环境变量 终端控制)

Linux/Unix环境下的make命令详解

Linux Linux命令 Linux安装 Linux编程 Linux桌面 Linux软件 Linux内核 Linux管理
Linux/Unix环境下的make命令详解

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

Linux系统中C语言编程创建函数fork()执行解析

编程语言 网络编程
Linux系统中C语言编程创建函数fork()执行解析

函数式编程(javascirpt)

Web开发
函数式编程(javascirpt)

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

一键保存网页中的全部图片

一键保存网页中的全部图片

Microsoft Edge游览器怎么设置主页

Microsoft Edge游览器怎么设置主页
下拉加载更多内容 ↓