Advertisement
iocoder

kernel/proc/fork.c

Nov 22nd, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.15 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.     /* set parent */
  53.     newproc->parent = curproc;
  54.  
  55.     /* initialize descriptors: */
  56.     newproc->plist.proc = newproc;
  57.     newproc->sched.proc = newproc;
  58.     newproc->irqd.proc  = newproc;
  59.  
  60.     /* create memory: */
  61.     if (umem_init(&(newproc->umem))) {
  62.         kfree(newproc);
  63.         return -1; /* error. */
  64.     }
  65.  
  66.     /* inherit parent's memory: */
  67.     if (umem_copy(&(curproc->umem), &(newproc->umem))) {
  68.         umem_free(&(newproc->umem));
  69.         kfree(newproc);
  70.         return -1; /* error. */
  71.     }
  72.  
  73.     /* create a new kernel stack. */
  74.     newproc->kstack = (unsigned char *) kmalloc(KERNEL_STACK_SIZE);
  75.     if (newproc->kstack == NULL) {
  76.         umem_free(&(newproc->umem));
  77.         kfree(newproc);
  78.         return -1; /* error. */
  79.     }
  80.  
  81.     /* initialize kernel stack...
  82.      * this invokes page faults to allocate memory for
  83.      * the stack early.
  84.      */
  85.     for (i = 0; i < KERNEL_STACK_SIZE; i++)
  86.         newproc->kstack[i] = 0;
  87.  
  88.     /* copy context from parent's stack to child's stack: */
  89.     copy_context(newproc);
  90.  
  91.     /* copy the set of file descriptors: */
  92.     for (i = 0; i < FD_MAX; i++) {
  93.         if (curproc->file[i] == NULL) {
  94.             newproc->file[i] = NULL;
  95.         } else {
  96.             curproc->file[i]->fcount++;
  97.             newproc->file[i] = curproc->file[i];
  98.         }
  99.     }
  100.  
  101.     /* inherit the current working directory: */
  102.     curproc->cwd->fcount++;
  103.     newproc->cwd = curproc->cwd;
  104.  
  105.     /* set pid: */
  106.     newproc->pid = ++last_pid;
  107.  
  108.     /* inform the scheduler that this is a just-forked process: */
  109.     newproc->after_fork = 1;
  110.  
  111.     /* initialize inbox */
  112.     newproc->inbox_lock = 0;
  113.     newproc->blocked_for_msg = 0;
  114.     linkedlist_init(&(newproc->inbox));
  115.  
  116.     /* children */
  117.     newproc->blocked_for_child = 0;
  118.  
  119.     /* not blocked */
  120.     newproc->blocked = 0;
  121.  
  122.     /* exit status: */
  123.     newproc->terminated = 0;
  124.     newproc->status = 0;
  125.  
  126.     /* add the new process to the list of processes: */
  127.     linkedlist_addlast((linkedlist*)&proclist, (linknode*)&(newproc->plist));
  128.  
  129.     /* add to scheduler's queue: */
  130.     linkedlist_addlast((linkedlist*)&q_ready, (linknode*)&(newproc->sched));
  131.  
  132.     /* return to the parent. */
  133.     return newproc->pid;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement