mutex locks and semaphone互斥锁和信号量

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()
  • 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()

同步工具,为进程同步其活动提供比互斥锁更复杂的方法。

- 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.

互斥锁要求进程在进入临界区段前获得锁,并在退出临界区段时释放锁,从而实现互斥。

- 信号量和互斥锁一样,也可以用来提供互斥功能。不过,互锁有一个二进制值,表示锁是否可用,而代用锁则有一个整数值,因此可用于解决各种同步问题。

  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值