在这个作业中,我们将探究中断和锁之间的一些关系。
Don’t do this
当xv6
执行下面的代码时,会发生什么?
1 | struct spinlock lk; |
重复上锁,第二个锁会panic。acquire的代码如下:
1 | // Acquire the lock. |
ide.c中的中断
An acquire
ensures that interrupts are off on the local processor using the cli
instruction (via pushcli()
), and that interrupts remain off until the release
of the last lock held by that processor (at which point they are enabled using sti
).
Let’s see what happens if we turn on interrupts while holding the ide
lock. In iderw
in ide.c
, add a call to sti()
after the acquire()
, and a call to cli()
just before the release()
. Rebuild the kernel and boot it in QEMU. Chances are the kernel will panic soon after boot; try booting QEMU a few times if it doesn’t.
在iderw中加锁和释放锁部分开启中断
1 | void |
在添加了sti
和cli
后,如果编译并启动内核,系统会panic。
sti
和cli
的作用
sti
开启中断,cli
关闭中断,也就是说我们在acquire
和release
之间开启了中断。
原因分析
系统提示的错误信息如下:
1 | sb: size 1000 nblocks 941 ninodes 200 nlog 30 logstart 2 inodestart 32 bmap start 58 |
最下方一行数字为系统调用栈情况,根据调用栈,