包括我自己在內,很多人對核心,程式,執行緒同步都不是很清楚,下面稍微總結一下:
 
核心同步:
主要是防止多核處理器同時訪問修改某段程式碼,或者在對裝置驅動程式進行臨界區保護。主要有一下幾種方式:
1. Mutex(互斥)
標頭檔案:
#include <linux/mutex.h>
初始化方法:
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 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.
 
3. 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, size_t msg_sz, long int msgtype, int msgflg);
int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg);

 
執行緒同步
1. semophore 訊號量
簡單用法:
#include <semaphore.h>

sem_t bin_sem;

res = sem_init(&bin_sem, 0, 0);
sem_wait(&bin_sem);
sem_post(&bin_sem);
sem_destroy(&bin_sem);
 
2. Mutex 互斥
標頭檔案以及函式
#include <pthread.h>
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 <pthread.h>
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.h>
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);