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的更多相关文章

  1. Linux kernel Programming - Allocating Memory

    kmalloc #include <linux/slab.h> void *kmalloc(size_t size,int flags); void kfree(void *addr); ...

  2. 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 ...

  3. Linux kernel Programming - Advanced Char Driver Operations

    ioctl //user space int ioctl(int fd,unsigned long cmd,...); //kernel space int (*ioctl)(struct inode ...

  4. Concurrency and Race Conditions

    1.当多个线程访问共享硬件或软件资源的任何时候,由于线程之间可能产生对资源的不一致观察,所以必须显式管理对资源的访问. 2.内核中的并发管理设施: (1). 信号量: P操作将信号量的值减 1 ,判断 ...

  5. Linux kernel memory-faq.txt

    ## Linux kernel memory-faq.txt What is some existing documentation on Linux memory management? Ulric ...

  6. Linux Kernel中断子系统来龙去脉浅析【转】

    转自:http://blog.csdn.net/u011461299/article/details/9772215 版权声明:本文为博主原创文章,未经博主允许不得转载. 一般来说,在一个device ...

  7. Linux Kernel的Makefile与Kconfig文件的语法

    https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt Introduction ------------ The c ...

  8. 从基本理解到深入探究 Linux kernel 通知链(notifier chain)【转】

    转自:https://blog.csdn.net/u014134180/article/details/86563754 版权声明:本文为博主原创文章,未经博主允许不得转载.——Wu_Being ht ...

  9. Linux Kernel C语言编程范式

    介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...

随机推荐

  1. 浏览器能正常访问的url,superagent不能正常访问

    在写音乐播放器的过程中,我需要获取qq音乐排行榜的信息,于是我向以前一样,在后台的MusicController中添加一个getTopList方法 然后写下以下代码 // 获取排行 async get ...

  2. SQL Server中的数据类型

    参考 SQL Server 2012编程入门经典(第4版) SQL Server 自带的数据类型 整型: 货币 近似小数 日期/时间 特殊数字 字符 Unicode 二进制 其他

  3. Python十讲 - 第一讲:从零开始学Python

    之后慢慢添加... Python语言的背景知识

  4. Python全栈学习_day011作业

    1,写函数,传入n个数,返回字典{‘max’:最大值,’min’:最小值}例如:min_max(2,5,7,8,4) 返回:{‘max’:8,’min’:2}(此题用到max(),min()内置函数) ...

  5. vue2.X的路由

    以 / 开头的嵌套路径会被当作根路径. <router-link> 在vue-router1.X中是以<a v-link=""></a>存在的 ...

  6. CSS格式化排版--排版

    1.文字排版--字体:利用font-family设置字体,注意设置的字体必须是本地电脑中存在的字体. 例子:class="MicrosoftYahei"的h1标签的字体设置为 宋体 ...

  7. 搜狐eHR团队-曾经一起奋斗过的~

    昨天所有搜狐eHR团队同事再相聚(在职+离职),大家聊的都很开心,共同回顾了eHR项目从无到有的过程. 非常感谢在搜狐的工作经历,自己成长很多,目前大家分布在各个公司为eHR做着贡献,大家都注意身体~ ...

  8. 程序员Web面试之JSON

    JSON是什么? JSON(JavaScript对象表示法), 是在网络通信下,常用的一种数据表达格式,它有助于我们于一个自描述的,独立的和轻的方式呈现并交换数据.这些数据可以易于和转换为JavaSc ...

  9. Flume Channel Selector

    Flume 基于Channel Selector可以实现扇入.扇出. 同一个数据源分发到不同的目的,如下图. 在source上可以定义channel selector: 1 2 3 4 5 6 7 8 ...

  10. HashTree【转】

    http://blog.csdn.net/yang_yulei/article/details/46337405 在各种数据结构(线性表.树等)中,记录在结构中的相对位置是随机的.因此在机构中查找记录 ...