xv6-k210/doc/内核原理-进程管理.md

9 lines
823 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 进程管理
## 自旋锁
讨论进程管理,就必须先搞懂自旋锁。
1. 为了避免死锁,加锁和释放锁中涉及开关中断,为了保证释放锁之后与加锁之前的中断使能标志保持一致,需要保存加锁之前关中断时的中断使能状态,在释放锁之后开中断时将中断使能恢复到之前所保存的状态。(即封装之后的push_off()和pop_off())
2. 由于现代编译器的代码优化和指令重排以及现代CPU的乱序执行(Out-of-Order Execution)在上锁期间需要额外的操作来保证读写操作的顺序一致性在RISC-V中可以用__sync_synchronize指令来保证严格的执行顺序。关于这个问题可以具体参考[这篇文章](https://www.0xffffff.org/2017/02/21/40-atomic-variable-mutex-and-memory-barrier/)