gpt4 book ai didi

c - 如何在 xv6 中为内核级线程实现克隆

转载 作者:太空宇宙 更新时间:2023-11-04 00:01:42 25 4
gpt4 key购买 nike

在尝试测试我的克隆系统调用(下面的代码)时,我不断收到以下错误:

pid 4 thread_test: trap 13 err 0 on cpu 1 eip 0xc54 addr 0x0--kill proc
pid 5 thread_test: trap 14 err 4 on cpu 1 eip 0x0 addr 0x28ec83e5--kill proc

分别对应一般保护错误和页面错误。有谁知道可能导致新线程在创建后立即被杀死的原因吗?

int clone(void *(*func) (void *), void *arg, void *stack)
{
int i,pid;
struct proc *np;

// Allocate process.
if((np = allocproc()) == 0)
return -1;

np->state = UNUSED;
np->sz = proc->sz;
np->parent = proc;
*np->tf = *proc->tf;
np->pgdir = proc->pgdir;

np->tf->eax = 0; // Clear %eax so that fork returns 0 in the child.
np->tf->eip = (int)func; //change eip to new function
np->kstack = stack; //use given stack

for(i = 0; i < NOFILE; i++)
if(proc->ofile[i])
np->ofile[i] = filedup(proc->ofile[i]);
np->cwd = idup(proc->cwd);

np->tf->esp = (uint)(stack+PGSIZE-4); //put esp to right spot on stack
*((uint*)(np->tf->esp)) = (uint)arg; //arg to function
*((uint*)(np->tf->esp)-4) = 0xFFFFFFFF; //return to nowhere
np->tf->esp =(np->tf->esp) -4;

safestrcpy(np->name, proc->name, sizeof(proc->name));
pid = np->pid;

acquire(&ptable.lock); //lock so writes last
np->state = RUNNABLE;
release(&ptable.lock);

return pid;
}

最佳答案

我找到了解决方案,我尝试使用传入的堆栈作为线程的 kstack。我需要在 proc.h 中创建一个单独的堆栈,然后使用以下方法将堆栈分配给线程:

np->stack = (int)stack;

关于c - 如何在 xv6 中为内核级线程实现克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40623201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com