Concurrency and Race Condition in Linux 2.6

This document is a brief summary of reading Concurrent and Race Condition chapter of Linux Device Driver.

1 Mutual exclusion Operations
1.1 Semaphores
Semaphores in Linux system is used for mutual exclusion operations.
It needs to get semaphore firstly that thread want to come into the critical section protected by semaphore. If semaphore has held by another thread, thread want to come into critical section will to go to sleep and this thread will be woke up by kernel later.

To use semaphores, code need include .

1.1.1 Create a Semaphore
struct semaphore sem;
void sema_init(struct semaphore *sem, int val)
void init_MUTEX(struct semaphore *sem)
void init_MUTEX_LOCKED(struct semaphore *sem)

DECLARE_MUTEX(name)
DECLARE_MUTEX_LOCKED(name)

1.1.2 Hold a Semaphore
void down(struct semaphore *sem)
It can be broke by interruption, when it is blocking for hold the “Semaphore”.

void down_interruptible(struct semaphore *sem)
It can not be broke by interruption, when it is blocking for hold the “Semaphore”.

void down_trylock(struct semaphore *sem)
It is not be blocked when request to hold a semaphore.

1.1.3 Release a Semaphore
void up(struct semaphore *sem)

1.2 Spin Lock
“Spinlock” is a lock that is used to mutual exclusion. It is different from “Semaphore” that using “Spinlock” will not bring thread into sleeping. It will run like a loop to check the lock status.

To use “Spinlock”, we need to include .

1.2.1 Create Spinlock
spinlock_t my_lock = SPIN_LOCK_UNLOCKED;

void spin_lock_init(spinlock_t *lock);
1.2.2 Hold a Spinlock
void spin_lock(spinlock_t *lock);
It cannot be broke by interruption during blocking for hold a lock. The procedure in critical section that protected by Spinlock should not be broke by interruption or be forced to go to sleep for any blocking operations.

spin_lock_irqsave
Before into the critical section, it disables the IRQ and save the status of current IRQ.
spin_lock_irq
Before into the critical section, it disables the IRQ but not save the status of current IRQ.
spin_lock_bh
Before into the critical section, it disables the software IRQ but still enable the hardware IRQ.

spin_trylock
spin_trylock_bh
These two APIs are not block during the requesting of hold Spinlock.

1.2.3 Release a Spinlock
Void spin_unlock(spinlock_t *lock);

spin_unlock_irqrestore
spin_unlock_irq
spin_unlock_bh

2 Synchronization Operations
2.1 Completions
“Completions” in Linux is used to synchronize tasks between threads.
To use “Completions”, we need to include .

2.1.1 Create a “Completions”
DECLARE_COMPLETION(my_completion)

Struct completion my_completion;
Init_completion(&my_completion);

2.1.2 Wait for Completed
void wait_for_completion(struct completion *c)
It cannot be broke by interruption during the waiting for “Completion”.

Flowing APIs defined in Linux kernel version more than 2.6.11.
unsigned long wait_for_completion_timeout(struct completion *x, unsigned long timeout)
int wait_for_completion_interruptible(struct completion *x)
unsigned long wait_for_completion_interruptible_timeout(struct completion *x, unsigned long timeout)

2.1.3 Wake up waited threads
void complete(struct completion *c)
Wake up one thread that is waiting for.

void complete_all(struct completion *c)
Wake up all threads that are waiting for.
After call complete_all API, we need to reinitialize the “Completion” using INIT_COMPLETION.

2.2 Wait Queue
If want a process or thread go to sleep for waiting to get a special condition, we can use “Wait Queue”. What difference between “Wait Queue” and “Completion” is “Wait Queue” can check the condition before or after sleep.

2.2.1 Create a “Wait Queue”
DECLARE_WAIT_QUEUE_HEAD(name)

wait_queue_head_t my_queue;
init_waitqueue_head(&my_queue);

2.2.2 Wait for expected condition
wait_event(queue, condition)
“queue” is the head of “Wait Queue”. “condition” is an expression used to evaluate the condition.

wait_event_interruptable(queue, condition)
It can be broke during sleeping.

wait_event_timeout(queue, condition, timeout)
wait_event_interruptable_timeout(queue, condition, timeout)

2.2.3 Wake up waiting thread of process
wake_up(wait_queue_head_t *queue)
wake_up_interruptable(wait_queue_head_t *queue)

 

 

 原文地址 http://kerneltrap.org/node/7070

并发控制和数据库系统恢复是数据库管理系统中两个重要的方面。并发控制主要涉及到在多个用户同时访问数据库时如何保证数据的一致性和完整性。 在数据库系统中,多个用户可以同时执行不同的事务,这可能导致读取和写入冲突。并发控制的目标是确保事务并发执行时数据库的一致性不会受到破坏。为了达到这个目标,数据库系统采取了多种技术和机制,比如锁机制、并发调度算法和事务隔离级别。 锁机制是实现并发控制的一种常用方法。当一个事务需要对数据库中的某个数据进行读取或写入时,它必须先获取相应的锁。如果其他事务已经获得了相同的锁,那么当前事务就需要等待,直到其他事务释放锁。通过锁机制,可以确保同一时间只有一个事务能够修改某个数据,从而保证数据的一致性。 另一个重要的方面是数据库系统的恢复机制。当数据库遭受到意外故障导致数据损失或不一致时,恢复机制会负责将数据库恢复到一致的状态。数据库系统通常会使用事务日志来记录每个事务的操作,包括读取和写入操作。如果发生故障,可以使用事务日志来重演事务,并恢复到故障之前的状态。其中一种常见的恢复技术是Aries算法,它使用了WAL(Write-Ahead Logging)和差量恢复的机制,可以有效地恢复数据库系统。 综上所述,并发控制和恢复是数据库系统中非常重要的两个方面。并发控制保证了多个事务同时访问数据库时的数据一致性,恢复机制则确保在故障发生时能够将数据库恢复到一致的状态。这些技术和机制的应用使得数据库系统能够提供高效可靠的数据访问和处理能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值