Main Page | Directories | File List | Globals

fork.c

Go to the documentation of this file.
00001 /*
00002  * kernel_sys/fork.c
00003  *
00004  * Copyright (c) 2003-2004 Lukasz Dembinski <dembol@nasa.com.pl>
00005  * All Rights Reserved
00006  * 
00007  * Date:        2004/01
00008  * Author:      Lukasz Dembinski
00009  * Info:        fork.c core file
00010  * Contact:     mailto: <dembol@nasa.com.pl>
00011  *
00012  */
00013 
00014 #include <agnix/agnix.h>
00015 #include <asm/types.h>
00016 #include <agnix/sched.h>
00017 #include <asm/tss.h>
00018 #include <agnix/memory.h>
00019 #include <agnix/tasks.h>
00020 #include <agnix/fs.h>
00021 #include <agnix/list.h>
00022 #include <agnix/data_structures.h>
00023 #include <agnix/spinlock.h>
00024 #include <agnix/errno.h>
00025 #include <agnix/console.h>
00026 
00027 extern struct list_head tasks_list;
00028 extern spinlock_t tasks_list_lock;
00029 extern struct list_head running_tasks_list;
00030 extern spinlock_t running_list_lock;
00031 
00032 int fork_hashd;
00033 static int last_pid = 0;
00034 
00035 int fork_add_hash_entry(int pid, struct task_s *task) 
00036 {
00037     return hash_entry_add(fork_hashd, (u32)pid, (u32)task);
00038 }
00039 
00040 struct task_s *fork_find_hash_entry(int pid) 
00041 {
00042     u32 task_ptr;
00043     
00044     task_ptr = hash_entry_find(fork_hashd, (u32)pid);
00045     
00046     return (struct task_s *)task_ptr;
00047 }
00048 
00049 int get_free_pid(void)
00050 {
00051     return ++last_pid;
00052 }
00053 
00054 void put_free_pid(int pid)
00055 {
00056     return;
00057 }
00058 
00059 int do_sys_fork(struct regs_s *regs)
00060 {
00061     struct task_s *cur_task = current_task;
00062     struct task_s *new_task;
00063     struct tss_wrap_s *new_tss_wrap;
00064     int pid;
00065     int ret;
00066 
00067     new_task = (struct task_s *)get_free_pages(1);
00068 
00069     ret = -EAGAIN;
00070     if ((pid = get_free_pid()) < 0)
00071         goto do_sys_fork_error;
00072         
00073     new_task->t_pid = pid;
00074 
00075     ret = -ENOMEM;
00076     if ((new_tss_wrap = task_fork(regs, (u32)new_task + 0x1000)) == 0)
00077         goto do_sys_fork_error;
00078         
00079     new_task->tss_wrap  = new_tss_wrap;    
00080     new_task->t_state   = TASK_STAT_STARTING;
00081     new_task->t_count   = INIT_COUNT;
00082     new_task->term      = current_task->term;
00083     new_tss_wrap->task  = new_task;
00084 
00085     if (files_fork(cur_task, new_task) != 0)
00086         goto do_sys_fork_error;
00087 
00088     if (signals_fork(cur_task, new_task) != 0)
00089         goto do_sys_fork_error;
00090 
00091     if (fork_add_hash_entry(pid, new_task) == -1) {
00092         kernel_panic("Cannot add task's pid to hash table!\n");
00093         return -1;
00094     }
00095 
00096     INIT_LIST_HEAD(&(new_task->task_list));
00097     
00098     sched_add_task(new_task);    
00099     sched_activate_task(new_task);
00100 
00101     ret = 0;
00102     
00103 do_sys_fork_error:
00104 
00105     return ret;
00106 }
00107 
00108 int do_task_exit(struct task_s *task, int exit_code)
00109 {
00110     u32 flags;
00111 
00112     spin_lock_irqsave(&running_list_lock, flags);
00113     list_del(&(task->task_list));
00114     spin_unlock_irqrestore(&running_list_lock, flags);
00115 
00116     put_free_pid(task->t_pid);
00117 
00118     task_exit(task->tss_wrap);
00119 
00120     return 0;
00121 }
00122 
00123 int do_sys_exit(int exit_code)
00124 {
00125     do_task_exit(current_task, exit_code);
00126 
00127     return 0;
00128 }
00129 
00130 int fork_init(void)
00131 {
00132     if ((fork_hashd = register_hash_table("fork", 1024, NULL)) < 0) {
00133         kernel_panic("Can not create hash table for fork!\n");
00134         
00135         return -1;
00136     }
00137 
00138     fork_add_hash_entry(0, current_task);
00139 
00140     return 0;
00141 }
Dokumentacje wygenerowano programem Doxygen 1.4.2 dla projektu Agnix