我现在正在尝试在 MIT 6.828(2018)关于操作系统工程的课程上取得进展,并且非常喜欢它,它既有趣又具有挑战性,我从中学到了很多基本的 OS 知识,现在我正在努力应对这种细粒度的锁定挑战:https://pdos.csail.mit.edu/6.828/2018/labs/lab4/
但是,当我尝试运行:make run-primes-nox CPUS=4
,我得到失败时,分叉的孩子,我怀疑它是内核堆栈数据在调度过程中被损坏或替换。
父级有时无法从fork
系统调用中恢复
在调度程序中,在进行循环之前,获取一些锁(lock_scheduler();
)以防止其他 CPU 访问进程列表。
int i = 1, curpos = -1, k = 0;
if (curenv)
curpos = ENVX(curenv->env_id);
lock_scheduler();
for (; i < NENV; i++)
{
k = (i + curpos) % NENV; // in a circular way
if (envs[k].env_status == ENV_RUNNABLE)
{
env_run(&envs[k]);
}
}
if (curenv != NULL && curenv->env_status == ENV_RUNNING)
{
env_run(curenv);
}
// sched_halt never returns
sched_halt();
在sched_halt
或即将env_run
期间,我们释放锁。
if (kernel_lock.locked && kernel_lock.cpu == thiscpu)
unlock_kernel();
if (scheduler_lock.locked && scheduler_lock.cpu == thiscpu)
unlock_scheduler();
当从中断或系统调用(明确地使用int $0x30
)陷入内核时,我们用原始的大内核锁(BKL)锁定内核,在退出陷阱之前,例如通过env_run
,我们释放内核锁。
void
trap(struct Trapframe *tf)
{
// The environment may have set DF and some versions
// of GCC rely on DF being clear
asm volatile("cld" ::: "cc");
// Halt the CPU if some other CPU has called panic()
extern char *panicstr;
if (panicstr)
asm volatile("hlt");
// Re-acqurie the big kernel lock if we were halted in
// sched_yield()
xchg(&thiscpu->cpu_status, CPU_STARTED);
// Check that interrupts are disabled. If this assertion
// fails, DO NOT be tempted to fix it by inserting a "cli" in
// the interrupt path.
assert(!(read_eflags() & FL_IF));
// only apply in trap
lock_kernel();
......
目前:
当从用户空间陷入内核时,我保留 kernel_lock
在分配或释放内存时,我使用 page_lock 来保护 page_free_list
当进入sched_yield
方法时,我获取 scheduler_lock,在运行任何用户进程(env_pop_tf
)之前解锁它
对不起,信息可能不是很充分,我已经在 Github 上上传了我的工作区:
https://github.com/k0Iry/6.828_2018_mit_jos这里包含我从 lab1 到 lab4 的所有实现。感谢您的!
重现该问题的方式:
git clone https://github.com/k0Iry/6.828_2018_mit_jos.git &&cd 6.828_2018_mit_jos
wget https://raw.githubusercontent.com/k0Iry/xv6-jos-i386-lab/master/labs/0001-trying-with-fine-grained-locks.patch
git apply 0001-trying-with-fine-grained-locks.patch
make run-primes-nox CPUS=4
你在进程的分叉过程中得到了错误
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(46条)