当前位置:网站首页>[MIT 6.S081] Lec 10: Multiprocessors and locking 笔记
[MIT 6.S081] Lec 10: Multiprocessors and locking 笔记
2022-07-27 16:09:00 【PeakCrosser】
Lec 10: Multiprocessors and locking
- Ref: https://github.com/huihongxiao/MIT6.S081/tree/master/lec10-multiprocessors-and-locking
- Preparation: xv6 book Chapter 6
为什么需要锁

- 多核运行提升性能
- 共享数据同时读写会产生竞态条件(race condition)
锁对象
- API:
acquire(&lcok): 任何时间只有一个进程能获取锁release(&lock): 释放锁, 其他进程需要等待至锁被释放才能获取.
- acquire 与 release 之间的代码片段称之为临界区, 代码片段整体执行具有原子性
- 通常操作系统会有多把锁, 使得不同系统调用使用不同的锁, 提高并行性.
使用锁的时机
如果两个进程访问了一个共享的数据结构, 并且其中一个进程会更新共享的数据结构, 那么就需要对这个共享数据结构加锁.
这一规则有时是严格的: 某些场景不加锁也可正常工作, 不加锁的程序称之为无锁编程(lock-free program).
有时又是宽松的, 对于一些非共享数据仍需要原子的输出, 如 printf() 的输出.
锁的作用
- 避免丢失更新
- 使得多步操作具有原子性
- 维护共享数据结构的不变性

死锁
- 重入引发的死锁: 一个进程获得锁进入临界区后又尝试获取同一个锁, 锁在非重入的情况下会引发死锁.
- deadly embrace: 多个进程持有多个锁, 彼此需要获取对方的锁
一种解决方案: 对(共同使用的)锁排序, 按序获取锁
锁实现
一般借助硬件指令 test-and-set, 如 XV6 中的原子交换操作(Atomic Memory Swap) amoswap addr, r1, r2, 可以原子的将内存地址 addr 的值写入寄存器 r2, 将寄存器 r1 的值写入 addr.
注: store 指令不一定是原子操作.
// On RISC-V, sync_lock_test_and_set turns into an atomic swap:
// a5 = 1
// s1 = &lk->locked
// amoswap.w.aq a5, a5, (s1)
while(__sync_lock_test_and_set(&lk->locked, 1) != 0)
;
如上是 XV6 自旋锁中的 test-and-set 操作, 若 lk->locked==0, 那么就写入 1 并返回 0, 循环退出, 表示获得到了锁. 若 lk->locked==1, 则同样是写入 1 但返回的也是 1, 则会继续循环, 直到 lk->locked 字段为 0 后能获取锁.__sync_synchronize() 函数会生成 fence 指令进行内存屏障, 防止指令重排.
在 acquire 和 release 操作时都需要关闭中断, 防止原子操作被打断或出现锁重入情况.
边栏推荐
- MySQL solves the problem of insert failure caused by duplicate unique indexes
- 动态链表2栈的链表存储结构(LinkedStack实现)
- You can't specify target table 'table name' for update in from clause error resolution in MySQL
- Here comes the first 5g SOC of MediaTek! A77+g77+apu3.0, officially released on November 26!
- 又一个时代的终结!
- SQL Server连接到服务器无效的解决办法
- WPF makes login interface
- Huawei mate30 Pro 5g disassembly: self developed chips account for more than half, and American chips still exist!
- Resolve merge fields in salesforce
- 深度学习:GCN图分类案例
猜你喜欢

WPF makes login interface

深度学习:STGCN学习笔记

XStream reports an error abstractreflectionconverter$unknownfield exception when parsing XML

多表查询

技术分享| 快对讲综合调度系统

深度学习-视频行为识别:论文阅读——双流网络(Two-stream convolutional networks for action recognition in videos)

Localization within Communities

Salesforce Dynamic Forms

WPF做登陆界面

EF框架简介
随机推荐
The first PCIe 5.0 SSD master of Jiangsu Huacun: TSMC 12NM process, mass production in 2020
You can't specify target table 'table name' for update in from clause error resolution in MySQL
联发科首款5G SoC来了!A77+G77+APU3.0,11月26日正式发布!
美团二面:为什么Redis会有哨兵?
Redis网红高频面试题三连:缓存穿透?缓存击穿?缓存雪崩?
国巨斥资18亿美元收购竞争对手Kemet,交易或在明年下半年完成
黑客用激光攻击,百米外就能激活语音助手
英伟达发布全球最小边缘AI超算:算力21TOPS,功耗仅10W!
CFA exam registration instructions
Please ask the great God a question, flinkcdc, the datetime field in synchronous MySQL will become a timestamp. Has anyone ever encountered it
On recommend mechanism in distributed training
Local development using LWC in salesforce
Multi thread implementation loop
Error launching IDEA
After being "expelled" from bitland, the Jank group said for the first time: it will return as soon as possible through legal channels!
嘉楠耘智已完成预路演,预计11月20日登陆纳斯达克
The end of another era!
同心向前,Google Play 十周年啦!
发布自己的npm组件库
二叉树概念