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. webpack4 系列教程(一): 打包JS

    webpack 本身就是为了打包js所设计,作为第一节,介绍怎么打包js. 1. 检验webpack规范支持 webpack支持es6, CommonJS, AMD. 创建vendor文件夹,其中mi ...

  2. Java执行sh等

    1.通过java代码,调用bat.shell等脚本或者命令 1)使用Runtime的exec()方法,会返回一个用于管理操作系统进程的Process对象 Process process =null; ...

  3. 可视化接口管理工具RAP,模拟数据,校验接口

    最近看到一个不错的接口管理的工具,分享一下 RAP ppt介绍:http://www.imooc.com/video/11060 RAP是一个可视化接口管理工具 通过分析接口结构,动态生成模拟数据,校 ...

  4. Git 学习一

    刚刚接触git,学习现骨干操作并记录一下过程中的小问题(Windows下) 1.新建git目录 创建一个目录,使用命令 git init 2.添加文件  git add a.txt 3.提交文件  g ...

  5. 【redis专题(5)】命令语法介绍之sets

    标签(空格分隔): Redis 关于 redis的无序集合有三个特点: 无序性, 确定性(描述准确) , 唯一性: 有点类似于数据容器: 增 SADD key member1 [member2] 作用 ...

  6. [20170617]vim中调用sqlplus.txt

    [20170617]vim中调用sqlplus.txt --//以前写过一篇emacs下调用sqlplus的文章,一直想学emacs,受限制自己掌握vim,对学习它没有兴趣,原链接如下:--//htt ...

  7. char/varchar/nvarchar的区别

    原文:https://blog.csdn.net/w516162189/article/details/78914035 我们在设计数据库的时候,需要根据需求场景选择合适的字段类型,对数据的执行效率有 ...

  8. sql server自定义函数学习笔记

    sql server中函数分别有:表值函数.标量函数.聚合函数.系统函数.这些函数中除系统函数外其他函数都需要用户进行自定义. 一.表值函数 简单表值函数 创建 create function fu_ ...

  9. Hadoop2.7.6_04_HDFS的Shell操作与常见问题

    1. HDFS的shell操作 1.1. 支持的命令及参数 [yun@mini05 zhangliang]$ hadoop fs Usage: hadoop fs [generic options] ...

  10. asp.net mvc项目使用spring.net发布到IIS后,在访问提示错误 Could not load type from string value 'DALMsSql.DBSessionFactory,DALMsSql'.

    asp.net mvc项目使用spring.net发布到IIS后,在访问提示错误 Could not load type from string value 'DALMsSql.DBSessionFa ...