Преглед изворни кода

implemented thread fork? kinda???

tarfeef101 пре 6 година
родитељ
комит
c6f66f8d4f
5 измењених фајлова са 45 додато и 5 уклоњено
  1. 1 0
      kern/include/proc.h
  2. 1 0
      kern/include/syscall.h
  3. 1 1
      kern/lib/list.c
  4. 7 0
      kern/proc/proc.c
  5. 35 4
      kern/syscall/proc_syscalls.c

+ 1 - 0
kern/include/proc.h

@@ -104,6 +104,7 @@ void proc_bootstrap(void);
 
 /* Create a fresh process for use by runprogram(). */
 struct proc *proc_create_runprogram(const char * name);// get and return a pid for the process
+
 int assignpid(struct proc * proc);
 
 // returns the child process if it is a child, otherwise null

+ 1 - 0
kern/include/syscall.h

@@ -63,6 +63,7 @@ int sys_write(int fdesc,userptr_t ubuf,unsigned int nbytes,int * retval);
 void sys__exit(int exitcode);
 int sys_getpid(pid_t *  retval);
 int sys_waitpid(pid_t pid, userptr_t status, int options, pid_t * retval);
+int sys_fork(struct trapframe * tf, int * retval);
 
 #endif // UW
 

+ 1 - 1
kern/lib/list.c

@@ -5,7 +5,7 @@ if (!(newnode)) return ENOMEM;#include <lib.h>
 list * newlist(void)
 {
   list * ret = kmalloc(sizeof(list));
-  if (!(ret)) return ENOMEM;
+  if (!(ret)) return NULL;
   ret->front = NULL;
   ret->len = 0;
   return ret;

+ 7 - 0
kern/proc/proc.c

@@ -98,6 +98,13 @@ static struct proc * proc_create(const char * name)
 		kfree(proc);
 		return NULL;
 	}
+	
+	proc->kids = newlist();
+	if (!(proc->kids))
+	{
+	  kfree(proc);
+	  return NULL;
+	}
 
 	threadarray_init(&proc->p_threads);
 	spinlock_init(&proc->p_lock);

+ 35 - 4
kern/syscall/proc_syscalls.c

@@ -50,12 +50,11 @@ void sys__exit(int exitcode)
 }
 
 
-/* stub handler for getpid() system call                */
+// basically, return an error code, and put the actual result in retval
 int sys_getpid(pid_t * retval)
 {
-  /* for now, this is just a stub that always returns a PID of 1 */
-  /* you need to fix this to make it work properly */
-  *retval = 1;
+  if (!(curproc)) return 1;
+  *retval = curproc->pid;
   return(0);
 }
 
@@ -90,3 +89,35 @@ int sys_waitpid(pid_t pid, userptr_t status, int options, pid_t * retval)
   return(0);
 }
 
+int sys_fork(struct trapframe * tf, int * retval)
+{
+  // create new process' memory space
+  struct proc * child = proc_create_runprogram("childproc");
+  struct addrspace * new_as;
+  struct trapframe * new_tf = kmalloc(sizeof(*tf));
+  
+  if ((!child) return ENOMEM;
+  as_copy(curproc_getas(), &childAddrspace);
+  
+  if (!(new_as) || !(new_tf))
+  {
+    proc_destroy(child);
+    kfree(new_as);
+    kfree(new_tf);
+    return ENOMEM;
+  }
+  
+  // might want to handle inserting PID into overall struct here. perhaps a setpid function or something in proc_create_runprogram...
+  // set PIDs, etc. copy data in to the new space
+  child->p_addrspace = new_as;
+  child->parent = curproc;
+  *new_tf = *tf;
+  
+  // start new thread
+  thread_fork("childproc", child, enter_forked_process, new_tf, 0);
+  
+  // return correct values
+  if (curproc->pid == child->pid) *retval = 0;
+  else *retval = child->pid;
+  return 0;
+}