Advertisement
iocoder

fork.c

Apr 12th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.86 KB | None | 0 0
  1. /*
  2.  *        +----------------------------------------------------------+
  3.  *        | +------------------------------------------------------+ |
  4.  *        | |  Quafios Kernel 1.0.2.                               | |
  5.  *        | |  -> procman: fork() system call.                     | |
  6.  *        | +------------------------------------------------------+ |
  7.  *        +----------------------------------------------------------+
  8.  *
  9.  * This file is part of Quafios 1.0.2 source code.
  10.  * Copyright (C) 2014  Mostafa Abd El-Aziz Mohamed.
  11.  *
  12.  * This program is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with Quafios.  If not, see <http://www.gnu.org/licenses/>.
  24.  *
  25.  * Visit http://www.quafios.com/ for contact information.
  26.  *
  27.  */
  28.  
  29. #include <arch/type.h>
  30. #include <sys/proc.h>
  31. #include <sys/scheduler.h>
  32. #include <sys/fs.h>
  33. #include <arch/stack.h>
  34.  
  35. /***************************************************************************/
  36. /*                                 fork()                                  */
  37. /***************************************************************************/
  38.  
  39. int32_t fork() {
  40.     /* Time to... fork!
  41.      * I had spent a long time preparing for this!
  42.      * now I am done, it's time to start working on fork().
  43.      */
  44.     int32_t i;
  45.     proc_t *newproc;
  46.  
  47.     /* create a new process structure: */
  48.     newproc = kmalloc(sizeof(proc_t));
  49.     if (newproc == NULL)
  50.         return -1;
  51.  
  52.     /* initialize descriptors: */
  53.     newproc->plist.proc = newproc;
  54.     newproc->sched.proc = newproc;
  55.     newproc->irqd.proc  = newproc;
  56.  
  57.     /* create memory: */
  58.     if (umem_init(&(newproc->umem))) {
  59.         kfree(newproc);
  60.         return -1; /* error. */
  61.     }
  62.  
  63.     /* inherit parent's memory: */
  64.     if (umem_copy(&(curproc->umem), &(newproc->umem))) {
  65.         umem_free(&(newproc->umem));
  66.         kfree(newproc);
  67.         return -1; /* error. */
  68.     }
  69.  
  70.     /* create a new kernel stack. */
  71.     newproc->kstack = (unsigned char *) kmalloc(KERNEL_STACK_SIZE);
  72.     if (newproc->kstack == NULL) {
  73.         umem_free(&(newproc->umem));
  74.         kfree(newproc);
  75.         return -1; /* error. */
  76.     }
  77.  
  78.     /* initialize kernel stack...
  79.      * this invokes page faults to allocate memory for
  80.      * the stack early.
  81.      */
  82.     for (i = 0; i < KERNEL_STACK_SIZE; i++)
  83.         newproc->kstack[i] = 0;
  84.  
  85.     /* copy context from parent's stack to child's stack: */
  86.     copy_context(newproc);
  87.  
  88.     /* copy the set of file descriptors: */
  89.     for (i = 0; i < FD_MAX; i++) {
  90.         if (curproc->file[i] == NULL) {
  91.             newproc->file[i] = NULL;
  92.         } else {
  93.             curproc->file[i]->fcount++;
  94.             newproc->file[i] = curproc->file[i];
  95.         }
  96.     }
  97.  
  98.     /* inherit the current working directory: */
  99.     curproc->cwd->fcount++;
  100.     newproc->cwd = curproc->cwd;
  101.  
  102.     /* set pid: */
  103.     newproc->pid = ++last_pid;
  104.  
  105.     /* inform the scheduler that this is a just-forked process: */
  106.     newproc->after_fork = 1;
  107.  
  108.     /* exit status: */
  109.     newproc->terminated = 0;
  110.     newproc->status = 0;
  111.  
  112.     /* add the new process to the list of processes: */
  113.     linkedlist_addlast((linkedlist*)&proclist, (linknode*)&(newproc->plist));
  114.  
  115.     /* add to scheduler's queue: */
  116.     linkedlist_addlast((linkedlist*)&q_ready, (linknode*)&(newproc->sched));
  117.  
  118.     /* return to the parent. */
  119.     return newproc->pid;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement