当前位置:网站首页>[mit 6.s081] LEC 10: multiprocessors and locking notes
[mit 6.s081] LEC 10: multiprocessors and locking notes
2022-07-27 18:27: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
Why locks are needed

- Multi core operation improves performance
- Sharing data while reading and writing will generate race conditions (race condition)
Lock object
- API:
acquire(&lcok): Only one process can acquire a lock at any timerelease(&lock): Release the lock , Other processes need to wait until the lock is released to obtain .
- acquire And release The code snippets between them are called critical areas , The overall execution of code fragments is atomic
- Usually, the operating system will have multiple locks , Make different system calls use different locks , Improve parallelism .
When to use the lock
If two processes access a shared data structure , And one of the processes will update the shared data structure , Then you need to lock the shared data structure .
This rule is sometimes strict : Some scenes can work without locking , Programs that do not lock are called lockless programming (lock-free program).
Sometimes it's loose , For some unshared data, the output of atoms is still needed , Such as printf() Output .
The function of lock
- Avoid losing updates
- Make multi-step operation atomic
- Maintain the invariance of shared data structures

Deadlock
- Deadlock caused by reentry : A process obtains a lock and tries to obtain the same lock after entering the critical area , Locks can cause deadlocks if they are not reentrant .
- deadly embrace: Multiple processes hold multiple locks , Each other needs to get each other's lock
A solution : Yes ( In common use ) Lock sorting , Acquire locks in sequence
Lock implementation
Generally with the help of Hardware instructions test-and-set, Such as XV6 Atomic exchange operation in (Atomic Memory Swap) amoswap addr, r1, r2, Memory address can be atomically addr The value of is written to the register r2, Register r1 Write the value of addr.
notes : store Instructions are not necessarily atomic operations .
// 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)
;
Above is XV6 In spin lock test-and-set operation , if lk->locked==0, Then write 1 And back to 0, Loop exit , Indicates that the lock has been obtained . if lk->locked==1, It is also written 1 But it also returns 1, Will continue to cycle , until lk->locked Field is 0 Then you can get the lock .__sync_synchronize() The function will generate fence Instructions for memory barrier , Prevent command rearrangement .
stay acquire and release Operation requires Turn off interrupt , Prevent atomic operations from being interrupted or lock reentry .
边栏推荐
- @Convert 注解在jpa中进行查询的注意事项
- Technology sharing | quick intercom integrated dispatching system
- 1. opencv图片基础操作
- Redis网红高频面试题三连:缓存穿透?缓存击穿?缓存雪崩?
- Buffer的只读模式
- 宣布收购文晔30%股份,大联大意欲何为?
- NVIDIA released the world's smallest edge AI supercomputing: 21tops computing power, power consumption is only 10W!
- 浅谈AI深度学习的模型训练和推理
- [learning notes] the implementation principle of the ordered set Zset in redis - skip table
- [MIT 6.S081] Lab 6: Copy-on-Write Fork for xv6
猜你喜欢

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

1. opencv图片基础操作

动态链表3队列的链式存储结构(LinkedQueue实现)

数据库的常用命令2
![[MIT 6.S081] Lab 7: Multithreading](/img/f4/26e513fb8678a88cfba29c1a636b37.png)
[MIT 6.S081] Lab 7: Multithreading

Deep learning: stgcn learning notes

How do corporate giants such as Schneider Electric and L'Oreal make open innovation? Uncover secrets of demo World Innovation Summit

GIS数据漫谈(五)— 地理坐标系统

文件路径读取

Deep learning: a survey of behavior recognition
随机推荐
Super practical! After reading the kubernetes study notes hidden by Alibaba P9, call NB directly
Collection! 0 basic open source data visualization platform flyfish large screen development guide
深度学习:安装包记录
[MIT 6.S081] Lec 1: Introduction and examples 笔记
Add music to the program interface and load background photos.
Announcing the acquisition of 30% shares of Wenye, what is the general intention of Dalian United?
Marvell announced the roadmap of its arm server chip, and the performance of the next generation will be twice that of thunderx2
Common commands of database 2
[MIT 6.S081] Lec 8: Page faults 笔记
[MIT 6.S081] Lab 3: page tables
"Who is Huawei" documentary film series landing on BBC: exposing a large number of Ren Zhengfei's unknown experience
Marvell公布旗下Arm服务器芯片路线图,下一代性能将比ThunderX2高两倍
软件安装相关
[MIT 6.S081] Lab 9: file system
multi-table query
江苏华存首发PCIe 5.0 SSD主控:台积电12nm工艺,2020年量产
Salesforce Dynamic Forms
Common commands of database 1
2. 改变颜色空间及颜色检测
收藏!0 基础开源数据可视化平台 FlyFish 大屏开发指南