Linux kernel Programming - Concurrency and Race Conditions
Concurrency and Its Management
Race condition can often lead to system crashes, memory leak,corrupted data,or security problem as well
- avoid the use of global variables
The Linux Semaphore Implementation
Semaphore
#include <linux/semaphore.h>
void sema_init(struct semaphore *sem,int val);
void down(struct semaphore *sem);
int down_interruptible(struct semaphore *sem);
int down_trylock(struct semaphore *sem);
void up(struct semaphore *sem);
Reader/Writer Semaphore
#include <linux/rwsem.h>
void init_rwsem(struct rw_semaphore *sem);
void down_read(struct rw_semaphore *sem);
int down_read_trylock(struct rw_semaphore *sem);
void up_read(struct rw_semaphore *sem);
void down_write(struct rw_semaphore *sem);
int down_write_trylock(struct rw_semaphore *sem);
void up_write(struct rw_semaphore *sem);
void downgrade_write(struct rw_semaphore *sem);
Completions
#include <linux/completion.h>
struct completion my_completion;
init_completion(&my_completion);
void wait_for_completion(struct completion *c);
void complete(struct completion *c);
void complete_all(struct completion *c);
Spainlocks
Note that all spinlock waits are,by their nature,uninterruptible.Once you call spin_lock,you will spin until the lock becomes available.
#include <linux/spinlock.h>
spinlock_t my_lock = SPIN_LOCK_UNLOCKED;//init
void spin_lock_init(spinlock_t *lock);
void spin_lock(spinlock_t *lock);// disable kernel preemption
void spin_lock_irqsave(spinlock_t *lock,unsigned long flags)
void spin_lock_irq(spinlock_t *lock);
void spin_lock_bh(spinlock_t *lock);
void spin_unlock(spinlock_t *lock);
Reader/Writer Spainlocks
Locking Traps
Ambiguous Rules
to make you locking work properly,you have to write some functions with the assumption that their caller has already acquired the relevant lock
Lock Ordering Rules
when multiple locks must be acquired,they should always be acquired in the same order
A couple of rules of thumb can help. If you must obtain a lock that is local to your code (a device lock, say) along with a lock belonging to a more central part of the kernel, take your lock first. If you have a combination of semaphores and spinlocks,you must, of course, obtain the semaphore(s) first; calling down (which can sleep) while holding a spinlockis a serious error. But most of all, try to avoid situations where you need more than one lock.
Fine- Versus Coarse- Grained Locking
大粒度的锁会造成系统很大的性能下降(比如 big kernel lock),而小粒度的锁,会造成更多的bug,则两者之间,需要一个权衡
Alternatives to Locking
Lock-Free Algorithms
Circular buffers :The producer is the only thread that is allowed to modify the write index and the array location it points to,The reader, in turn, is the only thread that can access the read index and the value it points to
Atomic Variables
The kernel provides an atomic integer type called atomic_t,defined in asm/atomic
Bit Operations
asm/bitops.h
seqlocks
Linux kernel Programming - Concurrency and Race Conditions的更多相关文章
- Linux kernel Programming - Allocating Memory
kmalloc #include <linux/slab.h> void *kmalloc(size_t size,int flags); void kfree(void *addr); ...
- Linux Kernel Programming - Time,Delays,and Deferred Work
Measuring Time Lapses The counter and the utility functions to read it live in <linux/jiffies.h&g ...
- Linux kernel Programming - Advanced Char Driver Operations
ioctl //user space int ioctl(int fd,unsigned long cmd,...); //kernel space int (*ioctl)(struct inode ...
- Concurrency and Race Conditions
1.当多个线程访问共享硬件或软件资源的任何时候,由于线程之间可能产生对资源的不一致观察,所以必须显式管理对资源的访问. 2.内核中的并发管理设施: (1). 信号量: P操作将信号量的值减 1 ,判断 ...
- Linux kernel memory-faq.txt
## Linux kernel memory-faq.txt What is some existing documentation on Linux memory management? Ulric ...
- Linux Kernel中断子系统来龙去脉浅析【转】
转自:http://blog.csdn.net/u011461299/article/details/9772215 版权声明:本文为博主原创文章,未经博主允许不得转载. 一般来说,在一个device ...
- Linux Kernel的Makefile与Kconfig文件的语法
https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt Introduction ------------ The c ...
- 从基本理解到深入探究 Linux kernel 通知链(notifier chain)【转】
转自:https://blog.csdn.net/u014134180/article/details/86563754 版权声明:本文为博主原创文章,未经博主允许不得转载.——Wu_Being ht ...
- Linux Kernel C语言编程范式
介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...
随机推荐
- DOM之城市二级联动
1.HTML内容 <select id="province"> <option>请选择</option> <option>山东省&l ...
- Installing Fonts programatically C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- JS模拟实现数组的map方法
昨天使用map方法的时候,突然感觉一直在直接用,也没有试试是怎么实现的,本来想直接搜一篇文章盘一下子,结果没搜到合适的,好吧,那就自己来写一下子吧 今天就来实现一个简单的map方法 首先我们来看一下m ...
- 【读书笔记】iOS-viewWillAppear:和viewDidLoad:
viewDidLoad:是视图第一次载入到内存中后调用的,viewWillApear:则是在每次视图显示到屏幕上之前调用. 参考资料:<iOS编程指南>
- filter(ele)过滤数组
filter也是一个常用的操作,它用于把Array的某些元素过滤掉,然后返回剩下的元素. 例如,在一个Array中,删掉偶数,只保留奇数,可以这么写: function remove(arr) { l ...
- 机器学习是万能的吗?AI落地有哪些先决条件?
机器学习是万能的吗?AI落地有哪些先决条件? https://mp.weixin.qq.com/s/9rNY2YA3BMpoY8NQ_rVIjQ 1.引言 入门机器学习或从事其相关工作前,不知道你思考 ...
- 《Inside C#》笔记(六) 属性、数组、索引器
一 属性 a) 属性可用于隐藏类的内部成员,对外提供可控的存取接口.属性相当于有些语言的getter.setter方法,只是使用起来更加方便一点,而且查看对应的IL码可以看到,属性的本质也确实是方法. ...
- Flutter 布局(六)- SizedOverflowBox、Transform、CustomSingleChildLayout详解
本文主要介绍Flutter布局中的SizedOverflowBox.Transform.CustomSingleChildLayout三种控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. ...
- c#中ofType的用法
原文:http://www.cnblogs.com/Janzen/p/5128749.html 该关键字主要用在非泛型到泛型之间的转化,在有些场合还是很有用的:比如:在使用非泛型的时候,想使用LINQ ...
- REPLACE函数的使用方法
Replace函数的含义~ 用新字符串替换旧字符串,而且替换的位置和数量都是指定的. replace函数的语法格式 =Replace(old_text,start_num,num_chars,new_ ...