Mutex Locks
- Previous solutions are complicated and generally inaccessible to application programmers
- OS designers build software tools to solve critical section problem
- Simplest is mutex lock
- Boolean variable indicating if lock is available or not
- Protect a critical section by
- First acquire() a lock
- Then release() the lock
- Calls to acquire() and release() must be atomic
- Usually implemented via hardware atomic instructions such as compare-and-swap.
- But this solution requires busy waiting
- This lock therefore called a spinlock
最简单的是互斥锁
- 表示锁是否可用的布尔变量
- 通过以下方法保护关键部分
- 首先获取()一个锁
- 然后释放锁
- 对 acquire() 和 release() 的调用必须是原子调用
- 通常通过硬件原子指令(如比较和交换)来实现。
- 但这种解决方案需要忙碌的等待
- 因此这种锁被称为自旋锁
Semaphore
- Synchronization tool that provides more sophisticated ways (than Mutex locks) for processes to synchronize their activities.
- Semaphore S – integer variable
- Can only be accessed via two indivisible (atomic) operations
- wait() and signal()
- Originally called P() and V()
- wait() and signal()
- Synchronization tool that provides more sophisticated ways (than Mutex locks) for processes to synchronize their activities.
- Semaphore S – integer variable
- Can only be accessed via two indivisible (atomic) operations
- wait() and signal()
- Originally called P() and V()
- wait() and signal()
同步工具,为进程同步其活动提供比互斥锁更复杂的方法。
- Semaphore S - 整数变量
- 只能通过两个不可分割的(原子)操作访问
- wait() 和 signal()
- 最初称为 P() 和 V()
- 同步工具,为进程同步其活动提供比互斥锁更复杂的方法。
- Semaphore S - 整数变量
- 只能通过两个不可分割的(原子)操作访问
- wait() 和 signal()
- 最初称为 P() 和 V()
- A mutex lock provides mutual exclusion by requiring that a process acquire a lock before entering a critical section and release the lock on exiting the critical section.
- Semaphores, like mutex locks, can be used to provide mutual exclusion. However, whereas a mutex lock has a binary value that indicates if the lock is available or not, a semaphore has an integer value and can therefore be used to solve a variety of synchronization problems.
互斥锁要求进程在进入临界区段前获得锁,并在退出临界区段时释放锁,从而实现互斥。
- 信号量和互斥锁一样,也可以用来提供互斥功能。不过,互锁有一个二进制值,表示锁是否可用,而代用锁则有一个整数值,因此可用于解决各种同步问题。