螺纹在Linux内核模块的本地数据 [英] Thread local data in linux kernel module
问题描述
是否有可能一个Linux内核模块中创建线程局部数据?
Is it possible to create thread local data in a linux kernel module?
我需要存储一些数据,为每个进程/线程调用我的模块。
有没有使用线程本地数据的一种简单的方法,或者我不得不求助于
写它采用当前进程的PID作为一个关键的哈希地图吗?
I need to store some data for each process/thread calling my module. Is there an easy way of using thread local data, or do I have to resort to writing a hash map which uses the pid of the current process as a key?
推荐答案
假设你的内核模块的接口字符设备驱动程序,那么你必须在文件结构一个private_data的字段(类似于用户空间的文件描述符)正是为。
Assuming the interface to you kernel module is a character device driver, then you have a private_data field in the file struct (which is analogous to user space file descriptor) exactly for that.
刚分配,并在打开文件操作分配一个指针到您选择的结构吧。
Just allocate and assign a pointer to your structure of choice at the open file operation to it.
这不完全线程或进程的地方,但在大多数情况下,一个文件描述到您的过程中的映射是真实的,它可能是配不上你。
It's not exactly thread or process local, but in most cases a mapping of one file descriptor to your process is true and it might be good enough for you.
这篇关于螺纹在Linux内核模块的本地数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!