linux 多核线程同步,Linux内核同步,进程,线程同步

本文详细介绍了Linux内核同步的各种机制,包括Mutex、Reader/Writer信号量和自旋锁,以及进程同步的Semaphore、共享内存和消息队列。此外,还提到了线程同步中的信号量、Mutex和读写锁的使用方法,帮助读者理解这些关键的同步原语。
摘要由CSDN通过智能技术生成

From:http://buaadallas.blog.51cto.com/399160/168011/

作者:buaadallas

关于作者的基本信息,请登陆作者的博客:

http://home.51cto.com/index.php?s=/space/399160

原文:

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。

否则将追究法律责任。http://buaadallas.blog.51cto.com/399160/168011

包括我自己在内,很多人对内核,进程,线程同步都不是很清楚,下面稍微总结一下:

内核同步:

主要是防止多核处理器同时访问修改某段代码,或者在对设备驱动程序进行临界区保护。主要有一下几种方式:

1、Mutex(互斥)

头文件:

#include

初始化方法:

DEFINE_MUTEX(name);

或者

void mutex_init (struct mutex *lock);

使用方法:

void mutex_lock (struct mutex *lock);

尝试得到互斥量,否则进入睡眠,不能被中断,否则会导致进程无法杀死。

int mutex_lock_interruptible(struct mutex *lock);

Same, but can be interrupted. If interrupted, returns a non zero value

and doesn't hold the lock.

Test the return value!!!

可以被中断

int mutex_trylock (struct mutex *lock);

Never waits. Returns a non zero value if the mutex is not avaiable.

无等待

int mutex_is_locked(struct mutex *lock);

Just tells whether the mutex is locked or not.

void mutex_unlock(struct mutex *lock);

Releases the lock. Make sure you do it as quickly as possible!

2、Reader/writer semphopres 读写信号量

Allow shared access by unlimited readers, or by only 1 writer. Writers get priority.

允许有限数量的读访问,但是只能有一个写访问。

void init_rwsem(struct rw_semaphore *sem);

void down_read(struct rw_semaphore *sem);

int down_read_trylock(struct rw_semaphore *sem);

int up_read(struct rw_semaphore *sem);

void down_write(struct rw_semaphore *sem);

int down_write_trylock(struct rw_semaphore *sem);

int up_write(struct rw_semaphore * sem);

Well suited for rare writes, holding the semaphore briefly.

Otherwise, readers get starved, waiting too long for the semaphore to be released.

3、Sinlocks自旋锁

初始化:

Static

sinlock_t my_lock = SPIN_LOCK_UNLOCKED;

Dynamic

void spin_lock_init(spinlock_t * lock);

使用:

void spin_[un]lock(spinlock_t * lock);

Doesn't disable interrupts.

Used for locking in process context

(critical sections in which you do not want to sleep).

void spin_lock_irqsave / spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags);

Disables / restores IRQs on the local CPU.

Typically used when the lock can be accessed in both process and interrupt context,

to prevent preemption by interrupts.

进程同步/通信

1、Semaphore信号量

semaphore sv = 1;

loop forever {

P(sv);

critical code section;

V(sv);

noncritical code section;

}

头文件以及函数:

#include

int semctl(int sem_id, int sem_num, int command, ...);

int semget(key_t key, int num_sems, int sem_flags);

int semop(int sem_id, struct sembuf *sem_ops, size_t num_sem_ops);

2、Share Memory共享内存

头文件以及函数

#include

void * shmat(int shm_id, const void * shm_addr, int shmflag);

int shmctl(int shm_id, int cmd, struct shmid_ds *buf);

int shmdt(const void * shm_addr);

int shmget(key_t key, size_t size, int shmflg);

3、Message Queues消息队列

头文件以及函数

#include

int msgctl(int msqid, int cmd, struct msqid_ds *buf);

int msgget(key_t key, int msgflg);

int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg);

int msgnd(int msgid, const void *msg_ptr, size_t msg_sz, int msgflg);

线程同步

1、semophore信号量

简单用法:

#include

sem_t bin_sem;

res = sem_init(&bin_sem, 0, 0);

sem_wait(&bin_sem);

sem_post(&bin_sem);

sem_destory(&bin_sem);

2、Mutex互斥

头文件以及函数

#include

int pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t * mutexattr);

int pthread_mutex_lock(pthread_mutex_t * mutex);

int pthread_mutex_unlock(pthread_mutex_t * mutex);

int pthread_mutex_destroy(pthread_mutex_t * mutex);

3、读写锁

头文件以及函数

#include

int pthread_rwlock_init(pthread_rwlock_t * restrict rwlock, const pthread_rwlockattr_t * restrict attr);

int pthread_rwlock_destroy(pthread_rwlock_t * rwlock);

int pthread_rwlock_rdlock(pthread_rwlock_t * rwlock);

int pthread_rwlock_wrlock(pthread_rwlock_t * rwlock);

int pthread_rwlock_unlock(pthread_rwlock_t * rwlock);

int pthread_rwlock_tryrdlock(pthread_rwlock_t * rwlock);

int pthread_rwlock_trywrlock(pthread_rwlock_t * rwlock);

4、条件变量

#include

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int pthread_cond_init(pthread_cond_t * cond, pthread_condattr_t * cond_attr);

int pthread_cond_signal(pthread_cond_t * cond);

int pthread_cond_broadcast(pthread_cond_t * cond);

int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex);

int pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, const struct timespec * abstime);

int pthread_cond_destroy(pthread_cond_t * cond);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值