快速业务通道

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

作者 佚名技术 来源 Linux系统 浏览 发布时间 2012-05-03
包括我自己在内,很多人对内核,进程,线程同步都不是很清楚,下面稍微总结一下: 内核同步: 主要是防止多核处理器同时访问修改某段代码,或者在对设备驱动程序进行临界区保护.主要有一下几种方式: 1. Mutex(互斥) 头文件: #include <linux/mutex.h> 初始化方法: DEFINE_MUTEX(name);或者 void mutex_init(struct mutex *lock); 使用方法: void mutex_lock (struct mutex *lock);
Tries to lock the mutex, sleeps otherwise.
Caution: can''t be interrupted, resulting in processes you cannot kill!
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 available.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. 拥有帝国一切,皆有可能。欢迎访问phome.net3. Spinlocks 自旋锁 初始化: Static
spinlock_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 <sys/sem.h>
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 <sys/shm.h>
void *shmat(int shm_id, const void *shm_addr, int shmflg);
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 <sys/msg.h>
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
int msgget(key_t key, int msgflg);
int msgrcv(int msqid, void *msg_ptr

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号