MIT6.828_HW7_xv6 locking
Don’t do this
Make sure you understand what would happen if the xv6 kernel executed the following code snippet:
struct spinlock lk;
initlock(&lk, "test lock");
acquire(&lk);
acquire(&lk);
查看acquire
源码,可以发现若连续两次对同一变量进行acquire,会进入panic
。
Interrupts in ide.c
在acquire
中,会关中断,实现原子操作。pushcli(); // disable interrupts to avoid deadlock.
让我们看看如果我们在持有ide锁时打开中断会发生什么。 在ide.c中的iderw中,在acquire()之后添加对sti()的调用,并在release()
之前调用cli()
。rebulid 内核并在QEMU中启动它。 内核在启动后很快就会panic
; 如果没有,多尝试启动QEMU几次。
acquire(&idelock); //DOC:acquire-lock
// sti();
// Append b to idequeue.
b->qnext = 0;
f