Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. int
  2. fork(void)
  3. {
  4. int i, pid;
  5. struct proc *np;
  6. struct proc *p = myproc();
  7.  
  8. // Allocate process.
  9. if((np = allocproc()) == 0){
  10. return -1;
  11. }
  12.  
  13. // Copy user memory from parent to child.
  14. if(uvmcopy(p->pagetable, np->pagetable, p->sz) < 0){
  15. freeproc(np);
  16. release(&np->lock);
  17. return -1;
  18. }
  19. np->sz = p->sz;
  20.  
  21. np->parent = p;
  22.  
  23. // copy saved user registers.
  24. *(np->tf) = *(p->tf);
  25.  
  26. // Cause fork to return 0 in the child.
  27. np->tf->a0 = 0;
  28.  
  29. // increment reference counts on open file descriptors.
  30. for(i = 0; i < NOFILE; i++)
  31. if(p->ofile[i])
  32. np->ofile[i] = filedup(p->ofile[i]);
  33. np->cwd = idup(p->cwd);
  34.  
  35. safestrcpy(np->name, p->name, sizeof(p->name));
  36.  
  37. pid = np->pid;
  38.  
  39. np->state = RUNNABLE;
  40.  
  41. release(&np->lock);
  42.  
  43. return pid;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement