现代操作系统有三大特性:中断处理、多任务处理和多处理器。这些特性导致当多个进程、线程或者CPU同时访问一个资源时,可能发生错误,这些错误是操作系统运行所不允许的。在操作系统中,内核需要提供并发控制机制,对共享资源进行保护。

  在操作系统中,并发是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行。并发容易导致竞争的问题。竞争就是两个或两个以上的进程同时访问一个资源,同时引起资源的错误。并发控制机制有以下几种:

1.原子变量操作:
  原子变量操作(分为原子整型操作和原子位操作)就是绝不会在执行完毕前被任何其他任务和时间打断,不会执行一半,又去执行其他代码。原子操作需要硬件的支持,因此是架构相关的,其API和原子类型的定义都在include/asm/atomic.h中,使用汇编语言实现。
  在linux中,原子变量的定义如下:

    typedef struct {        volatile int counter;    } atomic_t;

  关键字volatile用来暗示GCC不要对该类型做数据优化,所以对这个变量counte的访问都是基于内存的,不要将其缓冲到寄存器中。存储到寄存器中,可能导致内存中的数据已经改变,而寄存其中的数据没有改变。

(1)原子整型操作:
1)定义atomic_t变量: 

#define ATOMIC_INIT(i) ( (atomic_t) { (i) } )

atomic_t v = ATOMIC_INIT(0);    //定义原子变量v并初始化为0

2)设置原子变量的值:

#define atomic_set(v,i) ((v)->counter = (i))void atomic_set(atomic_t *v, int i);//设置原子变量的值为i

3)获取原子变量的值:

#define atomic_read(v) ((v)->counter + 0)atomic_read(atomic_t *v);//返回原子变量的值

4)原子变量加/减:

static __inline__ void atomic_add(int i, atomic_t * v); //原子变量增加i static __inline__ void atomic_sub(int i, atomic_t * v); //原子变量减少i

5)原子变量自增/自减:

#define atomic_inc(v) atomic_add(1, v); //原子变量加1 #define atomic_dec(v) atomic_sub(1, v); //原子变量减1

6)操作并测试:

//这些操作对原子变量执行自增,自减,减操作后测试是否为0,是返回true,否则返回false #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)static inline int atomic_add_return(int i, atomic_t *v)

 

(2)原子位操作(根据数据的每一位单独进行操作):

 

static inline void set_bit(nr, void *addr); //设置addr地址的第nr位,所谓设置位即将位写为1 static inline void clear_bit(nr,void *addr); //清除addr地址的第nr位,所谓清除位即将位写为0 static inline void change_bit(nr,void *addr); static inline void test_bit(nr, void *addr); static inline int test_and_set_bit(nr, void *addr); static inline int test_and_clear_bit(nr, void *addr); static inline int test_and_change_bit(nr, void *addr);

 


  原子操作的优点编写简单;缺点是功能太简单,只能做计数操作,保护的东西太少。

2.自旋锁
  自旋锁是专为防止多处理器并发而引入的一种锁,它应用于中断处理等部分。对于单处理器来说,防止中断处理中的并发可简单采用关闭中断的方式,不需要自旋锁。
  自旋锁最多只能被一个内核任务持有,如果一个内核任务试图请求一个已被争用(已经被持有)的自旋锁,那么这个任务就会一直进行忙循环——旋转——等待锁重新可用。要是锁未被争用,请求它的内核任务便能立刻得到它并且继续进行。自旋锁可以在任何时刻防止多于一个的内核任务同时进入临界区,因此这种锁可有效地避免多处理器上并发运行的内核任务竞争共享资源。
(1)自旋锁的使用:

spinlock_t spin; //定义自旋锁spin_lock_init(lock); //初始化自旋锁spin_lock(lock); //成功获得自旋锁立即返回,否则自旋在那里直到该自旋锁的保持者释放      spin_trylock(lock); //成功获得自旋锁立即返回真,否则返回假,而不是像上一个那样"在原地打转"spin_unlock(lock);//释放自旋锁

使用代码:

spinlock_t lock; spin_lock_init(&lock); spin_lock (&lock); ....//临界资源区 spin_unlock(&lock);

(2)注意事项:
  1)自旋锁是一种忙等待。它是一种适合短时间锁定的轻量级的加锁机制。
  2)自旋锁不能递归使用。自旋锁被设计成在不同线程或者函数之间同步。这是因为,如果一个线程在已经持有自旋锁时,其处于忙等待状态,则已经没有机会释放自己持有的锁了。如果这时再调用自身,则自旋锁永远没有执行的机会了。

3.信号量
  linux中,提供了两种信号量:一种用于内核程序中,一种用于应用程序中。这里只讲属前者。
  信号量和自旋锁的使用方法基本一样。与自旋锁相比,信号量只有当得到信号量的进程或者线程时才能够进入临界区,执行临界代码。信号量和自旋锁的最大区别在于:当一个进程试图去获得一个已经锁定的信号量时,进程不会像自旋锁一样在远处忙等待。
  信号量是一种睡眠锁。如果有一个任务试图获得一个已被持有的信号量时,信号量会将其推入等待队列,然后让其睡眠。这时处理器获得自由去执行其它代码。当持有信号量的进程将信号量释放后,在等待队列中的一个任务将被唤醒,从而便可以获得这个信号量。

(1)信号量的实现:
  在linux中,信号量的定义如下:

struct semaphore {    spinlock_t        lock;      //用来对count变量起保护作用。    unsigned int        count;     //    大于0,资源空闲;等于0,资源忙,但没有进程等待这个保护的资源;小于0,资源不可用,并至少有一个进程等待资源。    struct list_head    wait_list; //存放等待队列链表的地址,当前等待资源的所有睡眠进程都会放在这个链表中。};

(2)信号量的使用:

 

1)定义信号量:

 struct semaphore sem;

 

2)初始化信号量 :

static inline void sema_init(struct semaphore *sem, int val); //设置sem为val#define init_MUTEX(sem) sema_init(sem, 1) //初始化一个用户互斥的信号量sem设置为1#define init_MUTEX_LOCKED(sem) sema_init(sem, 0) //初始化一个用户互斥的信号量sem设置为0

定义和初始化可以一步完成:

DECLARE_MUTEX(name); //该宏定义信号量name并初始化1DECLARE_MUTEX_LOCKED(name); //该宏定义信号量name并初始化0

   当信号量用于互斥时(即避免多个进程同是在一个临界区运行),信号量的值应初始化为1。这种信号量在任何给定时刻只能由单个进程或线程拥有。在这种使用模式下,一个信号量有事也称为一个“互斥体(mutex)”,它是互斥(mutual exclusion)的简称。Linux内核中几乎所有的信号量均用于互斥。

  使用信号量,内核代码必须包含<asm/semaphore.h> 。

 

3)获取(锁定)信号量:

void down(struct semaphore *sem);

//该函数会导致睡眠,所以不能在中断上下文中使用; * * down - acquire the semaphore * @sem: the semaphore to be acquired * * Acquires the semaphore. If no more tasks are allowed to acquire the * semaphore, calling this function will put the task to sleep until the * semaphore is released. * * Use of this function is deprecated, please use down_interruptible() or * down_killable() instead. */

int down_interruptible(struct semaphore *sem);

 

/***该函数进入睡眠后,可以被信号唤醒;* down_interruptible - acquire the semaphore unless interrupted* @sem: the semaphore to be acquired** Attempts to acquire the semaphore. If no more tasks are allowed to* acquire the semaphore, calling this function will put the task to sleep.* If the sleep is interrupted by a signal, this function will return -EINTR.* If the semaphore is successfully acquired, this function returns 0.*/

 

int down_killable(struct semaphore *sem);

 

 /** * down_killable - acquire the semaphore unless killed * @sem: the semaphore to be acquired * * Attempts to acquire the semaphore.  If no more tasks are allowed to * acquire the semaphore, calling this function will put the task to sleep. * If the sleep is interrupted by a fatal signal, this function will return * -EINTR.  If the semaphore is successfully acquired, this function returns * 0. */

 

 

4)释放信号量

void up(struct semaphore *sem); //释放信号量sem,唤醒等待者

 

4.完成量
  它用于一个执行单元等待另一个执行单元执行完某事;

struct completion {    unsigned int done;    //大于0,表示完成量的函数可以立即执行,不要要等待;等于0,将拥有完成量的线程置于等待状态。    wait_queue_head_t wait;};

  使用方法:

1)定义完成量:

struct completion com;

2)初始化:

init_completion(&com); //要是觉得这两步麻烦,就再给你来个宏即定义又初始化DECLARE_COMPLETION(com);

3)等待完成量:

void __sched wait_for_completion(struct completion *x); //等待一个completion被唤醒

 

 

/** * wait_for_completion: - waits for completion of a task * @x:  holds the state of this particular completion * * This waits to be signaled for completion of a specific task. It is NOT * interruptible and there is no timeout. * * See also similar routines (i.e. wait_for_completion_timeout()) with timeout * and interrupt capability. Also see complete(). */

int __sched wait_for_completion_interruptible(struct completion *x);//可中断的wait_for_completion

 

/*** wait_for_completion_interruptible: - waits for completion of a task (w/intr)* @x: holds the state of this particular completion** This waits for completion of a specific task to be signaled. It is* interruptible.*/

unsigned long __sched wait_for_completion_timeout(struct completion *x, unsigned long timeout);//带超时处理的wait_for_completion

 

/*** wait_for_completion_timeout: - waits for completion of a task (w/timeout)* @x: holds the state of this particular completion* @timeout: timeout value in jiffies** This waits for either a completion of a specific task to be signaled or for a* specified timeout to expire. The timeout is in jiffies. It is not* interruptible.*/

4)唤醒完成量

void complete(struct completion *x); //只唤醒一个等待的进程或线程。void complete_all(struct completion *x); //唤醒所有等待这个完成量的进程或者线程。

 

后记:除了上述几种广泛使用的的并发控制机制外,还有中断屏蔽顺序锁(seqlock)、RCU(Read-Copy-Update)等等,做个简单总结如下图:

©著作权归作者所有:来自51CTO博客作者wx607823dfcf6a9的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 手把手教Linux驱动7-内核互斥锁
  2. ipcs、ipcrm、sysresv、kernel.shmmax
  3. 多线程安全-iOS开发要注意咯!
  4. Java中的锁
  5. 常见的Java锁总结:公平锁,可重入锁,独享锁,互斥锁,乐观锁,分段锁,偏向锁
  6. java中的锁介绍
  7. FreeRTOS例程3-串口中断接收不定长的数据与二值信号量的使用
  8. FreeRTOS例程4-串口DMA收发不定长数据
  9. Android跨进程通信IPC之2——Linux的跨进程通信详解

随机推荐

  1. Android定制出厂默认输入法
  2. Android 让你的 EditText 只接受指定字符
  3. Android 呼吸灯流程分析(二)
  4. android书籍
  5. android:向手机卡上写入文件时总是不成功,
  6. Android -- Dialog(AlertDialog)
  7. android的ITC
  8. 2011.09.23(2)——— android sample之Note
  9. Ubuntu 18.04 配置android 源码开发/编译
  10. android 按钮按下效果(文字颜色和按钮同时