c - 为什么 mutex_unlock 可以在没有被所有者调用的情况下工作
问题描述
互斥体必须稍后由获得它的同一任务释放。
上面的行出现在 kernel/locking/mutex.c 中 mutex_lock 的定义中。
我无法使用示例代码看到相同的行为。我有什么错误吗。请检查我下面的代码和 dmesg。
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/kthread.h>
MODULE_LICENSE("GPL");
DEFINE_MUTEX(my_lock);
struct task_struct *mythread;
static int threadfn(void *data)
{
pr_info("Kernel thread running on processor:%d\n", smp_processor_id());
pr_info("Kernel thread unlocking without calling lock on processor:%d\n", smp_processor_id());
mutex_unlock(&my_lock);
pr_info("Kernel thread unlocked without calling lock on processor:%d\n", smp_processor_id());
return 0;
}
static int __init test_hello_init(void)
{
mutex_init(&my_lock);
pr_info("Init function running on processor:%d\n", smp_processor_id());
mutex_lock(&my_lock);
pr_info("Init function locked on processor:%d\n", smp_processor_id());
mythread = kthread_run(threadfn, NULL, "mythread");
msleep(8000);
pr_info("Init function completed on processor:%d\n", smp_processor_id());
return -1;
}
static void __exit test_hello_exit(void)
{
}
module_init(test_hello_init);
module_exit(test_hello_exit);
dmesg:
[27389.146151] Init function running on processor:5
[27389.146152] Init function locked on processor:5
[27389.146351] Kernel thread running on processor:2
[27389.146352] Kernel thread unlocking without calling lock on processor:2
[27389.146352] Kernel thread unlocked without calling lock on processor:2
[27397.385479] Init function completed on processor:5
insmod: ERROR: could not insert module ./hello.ko: Operation not permitted
解决方案
推荐阅读
- python - Selenium代理连接,为什么ip不改变?
- android - 为什么 Jenkins 在不同的文件夹中搜索 Android SDK 工具?
- javascript - 使用超链接后如何显示相同 id 的 tabcontent?
- docker - 如何运行/托管:使用 docker-compose 在同一台机器上运行多个环境
- dataframe - 将多个 Dataframe 行转换为一行
- c++ - 当 `std::make_index_sequence` 和 `std::index_sequence` 用于模板参数默认类型时,g++ 和 clang++ 的不同行为
- listview - 如何为 Android 电视应用程序及其侦听器创建导航菜单抽屉
- django - 如何将静态文件从 django 容器暴露给 nginx
- php - 如何在表格中显示具有多个类别的产品
- assembly - 将 ASCII 格式的大十进制数(128 位)转换为二进制(十六进制)