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. elasticsearch安装之各种坑

    我用的是centos6.5,安装elasticsearch5.2.0 首先不说了,安装JDK1.8,下载elasticsearch5.2.0 https://www.elastic.co/downlo ...

  2. springMVC 拦截器源码解析

    前言:这两天学习了代理模式,自然想到了 springmvc 的 aop 使用的就是动态代理,拦截器使用的就是 jdk 的动态代理.今天看了看源码,记录一下.转载请注明出处:https://www.cn ...

  3. 教你分分钟搞定Python之Flask框架

    用最短的时间开发一个数据操作接口,Python是王道! 一.安装pip .首先检查linux有没有安装python-pip包,终端执行 pip -V [root@ network-scripts]# ...

  4. 重定向,/dev/null, 1>, 2>什么意思?

    文件描述符我们常见的就是系统预留的0,1和2这三个,他们的意义分别有如下对应关系: 0 —— stdin(标准输入) 1 —— stdout (标准输出) 2 —— stderr (标准错误) 其中, ...

  5. php面向对象的接口和APP接口区别

    以下是php面向对象的接口:

  6. CSS字体超出两行省略

    text-overflow: -o-ellipsis-lastline;overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-w ...

  7. 怎样制作爽心的 dashboard ?

    在目前的大数据趋势中,数据的大屏可视化成为大家所推崇的一种互动展示模式.如果我们能够早一些了解和掌握这方面的技术,相信对我们的未来将会非常有帮助! 我们知道,通过报表工具实现大屏展示可以通过单张报表. ...

  8. Core Animation-2:寄宿图

    #寄宿图 >图片胜过千言万语,界面抵得上千图片  ——Ben Shneiderman 我们在第一章『图层树』中介绍了CALayer类并创建了一个简单的有蓝色背景的图层.背景颜色还好啦,但是如果它 ...

  9. vs下开发windows服务程序

    一. VS2012下开发Windows服务 1. 打开VS2012,新建项目,选择Windows服务,此处我以开发一个定时自动发送邮件的服务来做介绍,如下图: 2. 创建好后,编译器会自动创建一些文件 ...

  10. Linux下postgresql数据库部署与配置

    1.检查postgresql是否已经安装:rpm -qa | grep postgres 2.检查PostgreSQL 安装位置:rpm -qal | grep postgres 3.卸载Postgr ...