[英]How to implement clone for kernel level threads in xv6
While attempting to test my clone syscall (code below), I keep getting the following errors: 在尝试测试我的克隆系统调用(下面的代码)时,我不断收到以下错误:
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
Which correspond to a general protection fault and a page fault respectively. 分别对应一般保护故障和页面故障。 Does anyone have any idea what might be causing the new threads to be killed right after creation?
有没有人知道在创建之后可能导致新线程被杀的原因是什么?
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;
}
I found the solution, I was trying to use the passed in stack as the thread's kstack. 我找到了解决方案,我试图使用传入的堆栈作为线程的kstack。 I needed to create a separate stack in
proc.h
and then assign the stack to the thread using: 我需要在
proc.h
创建一个单独的堆栈,然后使用以下命令将堆栈分配给线程:
np->stack = (int)stack;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.