I implement clone, join system call in sysproc.c.
int
sys_clone(void)
{
void (*fcn)(void*, void*);
void *arg1, *arg2, *stack;
if (argptr(0, (void*)&fcn, sizeof(fcn)) < 0 ||
argptr(1, (void*)&arg1, sizeof(arg1)) < 0 ||
argptr(2, (void*)&arg2, sizeof(arg2)) < 0 ||
argptr(3, (void*)&stack, sizeof(stack)) < 0)
return -1;
return clone(fcn, arg1, arg2, stack);
}
int
sys_join(void)
{
void **stack;
if (argptr(0, (void*)&stack, sizeof(stack)) < 0)
return -1;
return join(&stack);
}
and make clone, join function in proc.c.
int
clone(void(*fcn)(void *, void *), void *arg1, void *arg2, void *stack)
{
int tid;
struct proc *np;
struct proc *curproc = myproc();
if((np = allocproc()) == 0)
return -1;
np->pgdir = curproc->pgdir;
np->sz = curproc->sz;
np->parent = curproc;
*np->tf = *curproc->tf;
np->tf->esp = (uint)stack + 4096;
np->tf->eip = (uint)fcn;
np->tf->edx = (uint)arg1;
np->tf->ecx = (uint)arg2;
np->tf->eax = 0;
acquire(&ptable.lock);
np->state = RUNNABLE;
tid = np->pid;
release(&ptable.lock);
return tid;
}
int
join(void **stack)
{
struct proc *p;
int havekids, pid;
struct proc *curproc = myproc();
acquire(&ptable.lock);
for(;;){
havekids = 0;
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->parent != curproc)
continue;
havekids = 1;
if(p->state == ZOMBIE){
pid = p->pid;
kfree(p->kstack);
p->kstack = 0;
p->state = UNUSED;
p->parent = 0;
p->name[0] = 0;
*stack = (void *)p->tf->esp;
release(&ptable.lock);
return pid;
}
}
if(!havekids || curproc->killed){
release(&ptable.lock);
return -1;
}
sleep(curproc, &ptable.lock);
}
}
and declare them in syscall.h, ,syscall.c, usys.S, user.h.
I tried to test clone and join by test.c.
So in virtualbox ubuntu 20.04.6 xv6, I wrote ‘make qemu’.
But what i got is
sysproc.c: In function ‘sys_clone’:
sysproc.c:105:12: error: implicit declaration of function ‘clone’ [-Werror=implicit-function-declaration]
105 | return clone(fcn, arg1, arg2, stack);
| ^~~~~
sysproc.c: In function ‘sys_join’:
sysproc.c:116:12: error: implicit declaration of function ‘join’ [-Werror=implicit-function-declaration]
116 | return join(&stack);
| ^~~~
cc1: all warnings being treated as errors
make: *** [<내장>: sysproc.o] 오류 1
I declared clone and join in sysproc.c and proc.c.