定义在头文件linux/mutex.h中;

互斥体(mutex)是Linux系统提供的另外一种互斥机制;在Linux内核中是真实存在的;

1).定义互斥体

struct mutex my_mutex;    //定义互斥体

mutex_init(&my_mutex);    //初始化互斥体

2).获取和释放互斥体:

void mutex_lock(struct mutex* lock);               //获取互斥体,不可被信号中断

void mutex_lock_interruptible(struct mutex* lock); //获取互斥体,可被信号打断

int mutex_trylock(struct mutex* lock);             //尝试获取互斥体

void mutex_unlock(struct mutex* lock);             //释放互斥体

3).其它函数:

int mutex_is_locked(struct mutex* lock):

该函数检查互斥锁lock是否处于锁定状态;返回1,表示已锁定;返回0,表示未锁定;

int mutex_lock_interruptible(struct mutex* lock);  //该函数可被信号打断

int mutex_lock_killable(struct mutex* lock);       //该函数可被kill信号打断

4).互斥体的使用模式:

struct mutex my_mutex;   //定义互斥体

mutex_init(&my_mutex);   //初始化互斥体

mutex_lock(&my_mutex);   //获取互斥体

......                   //临界区代码

mutex_unlock(&my_mutex); //释放互斥体

例子:

#include

#include

#include

#include

#include

#include

//这三个头文件与内核线程的使用有关;

#include

#include

#include

//互斥量相关

#include

MODULE_LICENSE("GPL");

MODULE_AUTHOR("*************");

MODULE_VERSION("2.6.35.000");

static int sleep_time = (1*10*HZ);

static int shared_res = 0;

//STEP1:定义互斥量

struct mutex my_mutex;

//STEP5:实现线程函数

static int thread_process1(void* param)

{

//int val = 0, ret = 0;

while(1)

{

set_current_state(TASK_UNINTERRUPTIBLE);

if(kthread_should_stop())

{

printk("kernel thread '%s' should stop;file:%s;line:%d\n", __FUNCTION__, __FILE__, __LINE__);

break;

}

//STEP3:对临界区加锁

mutex_lock(&my_mutex);

shared_res++;

//STEP4:对临界区解锁

mutex_unlock(&my_mutex);

mdelay(sleep_time);

}

return 12;

};

static int thread_process2(void* param)

{

//int val = 0, ret = 0;

while(1)

{

set_current_state(TASK_UNINTERRUPTIBLE);

if(kthread_should_stop())

{

printk("kernel thread '%s' should stop;file:%s;line:%d\n", __FUNCTION__, __FILE__, __LINE__);

break;

}

//STEP3:对临界区加锁

mutex_lock(&my_mutex);

shared_res++;

//STEP4:对临界区解锁

mutex_unlock(&my_mutex);

msleep(sleep_time);

}

return 34;

};

static int thread_process3(void* param)

{

int val = 0;//, ret = 0;

while(1)

{

set_current_state(TASK_UNINTERRUPTIBLE);

if(kthread_should_stop())

{

printk("kernel thread '%s' should stop;file:%s;line:%d\n", __FUNCTION__, __FILE__, __LINE__);

break;

}

//STEP3:对临界区加锁

mutex_lock(&my_mutex);

val = shared_res;

printk("%s: shared resource = %d;\n%s", __FUNCTION__, val, ((val % 3) ? "" : "\n"));

//STEP4:对临界区解锁

mutex_unlock(&my_mutex);

msleep(sleep_time);

}

return 56;

};

static int thread_process4(void* param)

{

int val = 0;//, ret = 0;

while(1)

{

set_current_state(TASK_UNINTERRUPTIBLE);

if(kthread_should_stop())

{

printk("kernel thread '%s' should stop;file:%s;line:%d\n", __FUNCTION__, __FILE__, __LINE__);

break;

}

//STEP3:对临界区加锁

mutex_lock(&my_mutex);

val = shared_res;

printk("%s: shared resource = %d;\n%s", __FUNCTION__, val, ((val % 3) ? "" : "\n"));

//STEP4:对临界区解锁

mutex_unlock(&my_mutex);

msleep(sleep_time);

}

return 78;

};

static struct task_struct* my_thread1 = NULL;

static struct task_struct* my_thread2 = NULL;

static struct task_struct* my_thread3 = NULL;

static struct task_struct* my_thread4 = NULL;

static int __init study_init(void)

{

int err = 0;

printk("%s\n", __PRETTY_FUNCTION__);

//STEP2:初始化互斥量

mutex_init(&my_mutex);

printk("init mutex ok\n");

my_thread1 = kthread_create(thread_process1, NULL, "my_thread1");

if(IS_ERR(my_thread1))

{

err = PTR_ERR(my_thread1);

my_thread1 = NULL;

printk(KERN_ERR "unable to start kernel thread1:%d\n", err);

return err;

}

my_thread2 = kthread_create(thread_process2, NULL, "my_thread2");

if(IS_ERR(my_thread2))

{

err = PTR_ERR(my_thread2);

my_thread2 = NULL;

printk(KERN_ERR "unable to start kernel thread2:%d\n", err);

return err;

}

my_thread3 = kthread_create(thread_process3, NULL, "my_thread3");

if(IS_ERR(my_thread3))

{

err = PTR_ERR(my_thread3);

my_thread3 = NULL;

printk(KERN_ERR "unable to start kernel thread3:%d\n", err);

return err;

}

my_thread4 = kthread_create(thread_process4, NULL, "my_thread4");

if(IS_ERR(my_thread4))

{

err = PTR_ERR(my_thread4);

my_thread4 = NULL;

printk(KERN_ERR "unable to start kernel thread4:%d\n", err);

return err;

}

wake_up_process(my_thread1);

wake_up_process(my_thread2);

wake_up_process(my_thread3);

wake_up_process(my_thread4);

printk("%s:all kernel thread start;\n", __FUNCTION__);

return 0;

}

static void __exit study_exit(void)

{

int ret = -1;

printk("%s\n",__PRETTY_FUNCTION__);

if(my_thread1)

{

ret = kthread_stop(my_thread1);

my_thread1 = NULL;

printk("kernel thread1 stop,exit code is %d;\n",ret);

}

if(my_thread2)

{

ret = kthread_stop(my_thread2);

my_thread2 = NULL;

printk("kernel thread2 stop,exit code is %d;\n",ret);

}

if(my_thread3)

{

ret = kthread_stop(my_thread3);

my_thread3 = NULL;

printk("kernel thread3 stop,exit code is %d;\n",ret);

}

if(my_thread4)

{

ret = kthread_stop(my_thread4);

my_thread4 = NULL;

printk("kernel thread4 stop,exit code is %d;\n",ret);

}

printk("%s:all kernel thread stop;\n", __FUNCTION__);

}

module_init(study_init);

module_exit(study_exit);

linux 内核互斥体,内核并发控制---互斥量相关推荐

  1. linux 内核互斥体,Linux 内核同步(六):互斥体(mutex)

    互斥体 互斥体是一种睡眠锁,他是一种简单的睡眠锁,其行为和 count 为 1 的信号量类似.(关于信号量参考:Linux 内核同步(四):信号量 semaphore). 互斥体简洁高效,但是相比信号 ...

  2. Linux并发与竞争介绍(原子操作、自旋锁、信号量、互斥体)

    目录 并发与竞争 并发与竞争简介 保护内容是什么 原子操作 原子操作简介 原子整形操作API函数(atomic_t 结构体) 原子位操作API 函数 自旋锁 自旋锁简介 自旋锁API函数 线程与线程 ...

  3. 信号量、互斥体和自旋锁

    一.信号量 信号量又称为信号灯,它是用来协调不同进程间的数据对象的,而最主要的应用是共享内存方式的进程间通信.本质上,信号量是一个计数器,它用来记录对某个资源(如共享内存)的存取状况.一般说来,为了获 ...

  4. .Net CLR 中的同步机制(一): 互斥体

    随着软硬件技术的发展,无论是在Web服务或者云计算,还是单一的应用程序,串行方式编写的软件越来越少,我们总是可以看见并行的存在.但是并行并不是适合于每一种场景,也完全不是将工作扔到线程池中排队运行那么 ...

  5. Windows事件等待学习笔记(四)—— 事件信号量互斥体

    Windows事件等待学习笔记(四)-- 事件&信号量&互斥体 要点回顾 事件 实验:验证SignalState 第一步:编译并运行以下代码 第二步:观察结果 第三步:修改代码并执行 ...

  6. linux 两个驱动 竞态,第7章 Linux设备驱动中的并发控制之一(并发与竞态)

    本章导读 Linux设备驱动中必须解决的一个问题是多个进程对共享资源的并发访问,并发的访问会导致竞态(竞争状态). Linux提供了多种解决竞态问题的方式,这些方式适合不同的应用场景. 7.1讲解了并 ...

  7. 内核并发控制---互斥量(来自网易)

    定义在头文件linux/mutex.h中; 互斥体(mutex)是Linux系统提供的另外一种互斥机制;在Linux内核中是真实存在的; 1).定义互斥体 struct mutex my_mutex; ...

  8. linux内核futex快速用户空间互斥体简介

    futex内核同步 futex快速用户空间互斥体,用来给上层应用构建更高级别的同步机制,是实现信号量和锁的基础. 进程间通信,管道.消息队列.信号量.共享内存.套接字.信号. 使用信号量(semget ...

  9. Linux进程管理:内核中的优先级继承互斥(rtmutex.h):防止优先级反转

    目录 Priority inheritance in the kernel 译文 Priority inheritance in the kernel https://lwn.net/Articles ...

最新文章

  1. 文章3:车载LIDAR点云数据中杆状地物自动提取与分类
  2. ClamAV病毒库增加特征码
  3. SpringBoot(六)_AOP统一处理请求
  4. 彻底弄懂dalvik字节码【一】
  5. 《大数据分析原理与实践》一一第3章 关联分析模型
  6. java aop注解拦截_Spring AOP 拦截指定注解标识的类或方法
  7. “指向指针的指针”的作用和应用
  8. java openresty 调用_玩转 OpenResty 协程 API
  9. (04)FPGA学习基础
  10. matlab 把区间等分分,MATLAB数学实验报告 定积分的近似计算
  11. Win32++ Home Page
  12. Mac系统如何安装Eclipse并搭建Android开发环境
  13. Oracle DB audit
  14. 输入数字N,然后输入N个数,计算这N个数的和。
  15. springboot一键启动
  16. 大学生应知道50件事
  17. 怎样截图翻译?试试这几个软件吧
  18. 华为云14天鸿蒙设备开发-Day1源码获取
  19. miflash 刷机超过1000s还未完成
  20. ADPCM音频编解码

热门文章

  1. 程序员面试金典——番外篇之约瑟夫问题1
  2. Leetcode 235.二叉搜索树的最近公共祖先
  3. 有限项加和的极限求解思路定式
  4. 严版快速排序Partion方法
  5. [转]『TensorFlow』读书笔记_TFRecord学习
  6. hibernate.validator验证参数
  7. MyBatis运行动态sql及存储过程
  8. [转]jQuery的each方法的几种常用的用法
  9. 报表性能优化方案之善用参数注入
  10. java如何用异或符号实现两个变量值的交换