【发布时间】:2009-11-16 10:50:32
【问题描述】:
我正在阅读 LKMPG (See Section 4.1.4. Unregistering A Device),我不清楚何时使用 try_module_get / module_put 函数。有些 LKMPG 示例使用它们,有些则没有。
更令人困惑的是,try_module_get 在 2.6.24 源代码的 193 个文件中出现了 282 次,但在 Linux Device Drivers ( LDD3 ) 和 Essential Linux Device Drivers 中,它们甚至没有出现在一个代码示例中。
我想他们可能是绑定到旧的register_chrdev 接口(在 2.6 中被 cdev 接口取代),但它们只一起出现在同一个文件中 8 次:
find -type f -name *.c | xargs grep -l try_module_get | sort -u | xargs grep -l register_chrdev | sort -u | grep -c .
那么什么时候适合使用这些功能?它们是否与特定界面或一组环境的使用相关联?
编辑
我从 LKMPG 加载了sched.c 示例并尝试了以下实验:
anon@anon:~/kernel-source/lkmpg/2.6.24$ tail /proc/sched -f &
Timer called 5041 times so far
[1] 14594
anon@anon:~$ lsmod | grep sched
sched 2868 1
anon@anon:~$ sudo rmmod sched
ERROR: Module sched is in use
这让我相信内核现在可以自己记账,而gets/puts可能已经过时了。任何人都可以验证这一点吗?
【问题讨论】:
-
在第 14 章,
reference count manipulation小节(第 367 页)中出现了一次try_module_get -
我的意思是我之前评论中的 LDD 书
标签: linux-kernel linux-device-driver kernel-module