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 filename
以下为测试程序:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>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 1024extern 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);
}
输出为:
linux ftok函数相关推荐
- linux ftok函数的使用
ftok API #include <sys/types.h> #include <sys/ipc.h> key_t ftok(const char *pathname, in ...
- Linux -- ftok函数
翻译自Ubuntu 19版本下的Linux Programmer's Manual 函数名:ftok – 将一个路径名和一个对象标识符转换为System V IPC键值. 概要: #include & ...
- ftok file php,Linux和PHP中的ftok函数返回值不一致问题跟踪
在IPC中中,我们经常事情ftok函数来获取key,来作为获取消息队列id.共享存储标识和信号量ID.在项目中使用了php进程和linux进程通信,采用了消息队列的方式,但是结果表现为php中的fto ...
- linux C -- ftok函数
链接: linux C学习目录 原文链接:https://blog.csdn.net/u013485792/article/details/50764224 关于ftok函数,先不去了解它的作用来先说 ...
- linux中ftok的作用,Unix/Linux编程之ftok函数用法
linux 中ftok函数的用法 1.函数作用: 系统建立IPC通讯(如消息队列.共享内存时)必须指定一个ID值.通常情况下,该id值通过ftok函数得到 2.函数原型: #include #incl ...
- Linux下的ftok()函数
linux ftok()函数 - 清清飞扬 - 博客园 (cnblogs.com) 系统建立IPC通讯(如消息队列.共享内存时)必须指定一个ID值.通常情况下,该id值通过ftok函数得到. ftok ...
- linux ftok()函数
linux ftok()函数 系统建立IPC通讯(如消息队列.共享内存时)必须指定一个ID值.通常情况下,该id值通过ftok函数得到. ftok原型如下: key_t ftok( char * fn ...
- linux 共享内存函数封装,linux ftok()函数 --多进程IPC之共享内存
系统创建IPC通信(如消息队列.共享内存时)必须指定一个ID值.一般状况下,该id值经过ftok函数获得. ftok原型以下: key_t ftok( char * fname, int id ) f ...
- linux环境编程-- ftok()函数
系统建立IPC通讯(如消息队列.共享内存时)必须指定一个ID值.通常情况下,该id值通过ftok函数得到. ftok原型如下: key_t ftok( char * fname, int id ) f ...
最新文章
- CSMA/CD在全双工和半双工模式下的区别
- LeetCode 448. Find All Numbers Disappeared in an Array 442. Find All Duplicates in an Array
- postgresql(pg)数据库简介
- gitlab的升级【二】旧数据的备份和新数据的恢复
- 慢查询优化,我终于在生产踩到了这个坑!!
- Spring boot转发请求
- Oracle 查看 对象 持有锁的情况
- spring async_Spring Async和Java的8 CompletableFuture
- 无法解析 uafxcw.lib_二级建造师《实务科目》推荐知识点习题,附中业网校答案解析...
- 支付宝推出“轻会员”;iPhone11 或将主动禁用双向无线充电;Java 13 发布 | 极客头条...
- 特殊权限及SUID,facl及Linux的终端
- synchronize——对象锁和类锁
- Linux中用两个网卡同时上内外网
- 利用PS进行图片格式及位数变换
- Android FDE 加密过程
- Lateral View Outer
- C++ 缺省参数 详解
- 函数的基本使用,切克闹
- 网易测试开发岗面试经历
- 2016年8月19日 星期五 --出埃及记 Exodus 16:20
热门文章
- 计算机组织活动的意义,信息学院计算机09-1班团支部关于“向榜样学习,向优秀看齐”主题班团会活动总结...
- 一文简单了解互联网流量变现
- 《Java SE实战指南》10:特性修饰符
- Latex排版[1]:输入矩阵(latex如何输入矩阵、对角阵、方程组)
- winfrom 实现条形码批量打印以及将条形码信息生成PDF文件
- 系统设定工具(网络、打印机)与硬件侦测
- repmat()函数用法
- 竞速游戏任务系统设计
- Workbook 对象 应用示例
- 配置网站的快捷方式图标及收藏图标