标签:code bec ted system int its try problem err
Race condition can often lead to system crashes, memory leak,corrupted data,or security problem as well
#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);
#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);
#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);
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);
to make you locking work properly,you have to write some functions with the assumption that their caller has already acquired the relevant lock
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.
大粒度的锁会造成系统很大的性能下降(比如 big kernel lock),而小粒度的锁,会造成更多的bug,则两者之间,需要一个权衡
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
The kernel provides an atomic integer type called atomic_t,defined in asm/atomic
asm/bitops.h
Linux kernel Programming - Concurrency and Race Conditions
标签:code bec ted system int its try problem err
原文地址:https://www.cnblogs.com/r1ng0/p/9708800.html