소스 검색

NOT WORKING: as all the printfs would tell you. basically, the palin test is trying to exit from a null or kernel thread, and the onefork test hangs. this is because a seemingly valid parent address has a DEADBEEF exitcode, which makes us skip the deletion of our thread

tarfeef101 6 년 전
부모
커밋
2a4d22c8a4
7개의 변경된 파일200개의 추가작업 그리고 79개의 파일을 삭제
  1. 32 30
      kern/compile/ASST2/.depend
  2. BIN
      kern/compile/ASST2/kernel
  3. 1 1
      kern/compile/ASST2/vers.c
  4. 1 1
      kern/compile/ASST2/version
  5. 4 1
      kern/include/proc.h
  6. 28 13
      kern/proc/proc.c
  7. 134 33
      kern/syscall/proc_syscalls.c

+ 32 - 30
kern/compile/ASST2/.depend

@@ -287,7 +287,7 @@ uio.o: ../../lib/uio.c ../../include/types.h ../../include/kern/types.h \
   ../../include/thread.h ../../include/array.h ../../include/threadlist.h \
   includelinks/machine/thread.h ../../include/setjmp.h \
   includelinks/kern/machine/setjmp.h ../../include/limits.h \
-  ../../include/kern/limits.h ../../include/list.h \
+  ../../include/kern/limits.h ../../include/synch.h ../../include/list.h \
   ../../include/current.h includelinks/machine/current.h \
   ../../include/copyinout.h
 proc.o: ../../proc/proc.c ../../include/types.h \
@@ -298,11 +298,12 @@ proc.o: ../../proc/proc.c ../../include/types.h \
   ../../include/array.h ../../include/lib.h opt-noasserts.h \
   ../../include/threadlist.h includelinks/machine/thread.h \
   ../../include/setjmp.h includelinks/kern/machine/setjmp.h \
-  ../../include/limits.h ../../include/kern/limits.h ../../include/list.h \
-  ../../include/current.h includelinks/machine/current.h \
-  ../../include/addrspace.h ../../include/vm.h includelinks/machine/vm.h \
-  ../../include/vnode.h ../../include/vfs.h ../../include/synch.h \
-  ../../include/kern/fcntl.h ../../include/kern/errno.h
+  ../../include/limits.h ../../include/kern/limits.h \
+  ../../include/synch.h ../../include/list.h ../../include/current.h \
+  includelinks/machine/current.h ../../include/addrspace.h \
+  ../../include/vm.h includelinks/machine/vm.h ../../include/vnode.h \
+  ../../include/vfs.h ../../include/kern/fcntl.h \
+  ../../include/kern/errno.h
 main.o: ../../startup/main.c ../../include/types.h \
   ../../include/kern/types.h includelinks/kern/machine/types.h \
   includelinks/machine/types.h ../../include/kern/errno.h \
@@ -313,12 +314,12 @@ main.o: ../../startup/main.c ../../include/types.h \
   includelinks/machine/spinlock.h ../../include/threadlist.h \
   includelinks/machine/thread.h ../../include/setjmp.h \
   includelinks/kern/machine/setjmp.h ../../include/proc.h \
-  ../../include/limits.h ../../include/kern/limits.h ../../include/list.h \
-  ../../include/current.h includelinks/machine/current.h \
-  ../../include/synch.h ../../include/vm.h includelinks/machine/vm.h \
-  ../../include/mainbus.h ../../include/vfs.h ../../include/device.h \
-  ../../include/syscall.h ../../include/test.h ../../include/version.h \
-  autoconf.h
+  ../../include/limits.h ../../include/kern/limits.h \
+  ../../include/synch.h ../../include/list.h ../../include/current.h \
+  includelinks/machine/current.h ../../include/vm.h \
+  includelinks/machine/vm.h ../../include/mainbus.h ../../include/vfs.h \
+  ../../include/device.h ../../include/syscall.h ../../include/test.h \
+  ../../include/version.h autoconf.h
 menu.o: ../../startup/menu.c ../../include/types.h \
   ../../include/kern/types.h includelinks/kern/machine/types.h \
   includelinks/machine/types.h ../../include/kern/errno.h \
@@ -330,7 +331,7 @@ menu.o: ../../startup/menu.c ../../include/types.h \
   includelinks/machine/spinlock.h ../../include/threadlist.h \
   includelinks/machine/thread.h ../../include/setjmp.h \
   includelinks/kern/machine/setjmp.h ../../include/proc.h \
-  ../../include/list.h ../../include/synch.h ../../include/vfs.h \
+  ../../include/synch.h ../../include/list.h ../../include/vfs.h \
   ../../include/sfs.h ../../include/fs.h ../../include/vnode.h \
   ../../include/kern/sfs.h ../../include/syscall.h ../../include/test.h \
   opt-synchprobs.h opt-sfs.h opt-net.h
@@ -346,7 +347,7 @@ file_syscalls.o: ../../syscall/file_syscalls.c ../../include/types.h \
   ../../include/threadlist.h includelinks/machine/thread.h \
   ../../include/setjmp.h includelinks/kern/machine/setjmp.h \
   ../../include/proc.h ../../include/limits.h ../../include/kern/limits.h \
-  ../../include/list.h
+  ../../include/synch.h ../../include/list.h
 loadelf.o: ../../syscall/loadelf.c ../../include/types.h \
   ../../include/kern/types.h includelinks/kern/machine/types.h \
   includelinks/machine/types.h ../../include/kern/errno.h \
@@ -356,7 +357,7 @@ loadelf.o: ../../syscall/loadelf.c ../../include/types.h \
   ../../include/thread.h ../../include/array.h ../../include/threadlist.h \
   includelinks/machine/thread.h ../../include/setjmp.h \
   includelinks/kern/machine/setjmp.h ../../include/limits.h \
-  ../../include/kern/limits.h ../../include/list.h \
+  ../../include/kern/limits.h ../../include/synch.h ../../include/list.h \
   ../../include/current.h includelinks/machine/current.h \
   ../../include/addrspace.h ../../include/vm.h includelinks/machine/vm.h \
   ../../include/vnode.h ../../include/elf.h includelinks/machine/elf.h
@@ -365,15 +366,15 @@ proc_syscalls.o: ../../syscall/proc_syscalls.c ../../include/types.h \
   includelinks/machine/types.h ../../include/kern/errno.h \
   ../../include/kern/unistd.h ../../include/kern/wait.h \
   includelinks/mips/trapframe.h ../../include/lib.h ../../include/cdefs.h \
-  opt-noasserts.h ../../include/syscall.h ../../include/current.h \
+  opt-noasserts.h ../../include/vfs.h ../../include/array.h \
+  ../../include/vnode.h ../../include/syscall.h ../../include/current.h \
   includelinks/machine/current.h ../../include/thread.h \
-  ../../include/array.h ../../include/spinlock.h \
-  includelinks/machine/spinlock.h ../../include/threadlist.h \
-  includelinks/machine/thread.h ../../include/setjmp.h \
-  includelinks/kern/machine/setjmp.h ../../include/proc.h \
-  ../../include/limits.h ../../include/kern/limits.h ../../include/list.h \
-  ../../include/addrspace.h ../../include/vm.h includelinks/machine/vm.h \
-  ../../include/copyinout.h ../../include/synch.h
+  ../../include/spinlock.h includelinks/machine/spinlock.h \
+  ../../include/threadlist.h includelinks/machine/thread.h \
+  ../../include/setjmp.h includelinks/kern/machine/setjmp.h \
+  ../../include/proc.h ../../include/limits.h ../../include/kern/limits.h \
+  ../../include/synch.h ../../include/list.h ../../include/addrspace.h \
+  ../../include/vm.h includelinks/machine/vm.h ../../include/copyinout.h
 runprogram.o: ../../syscall/runprogram.c ../../include/types.h \
   ../../include/kern/types.h includelinks/kern/machine/types.h \
   includelinks/machine/types.h ../../include/kern/errno.h \
@@ -383,7 +384,7 @@ runprogram.o: ../../syscall/runprogram.c ../../include/types.h \
   ../../include/array.h ../../include/threadlist.h \
   includelinks/machine/thread.h ../../include/setjmp.h \
   includelinks/kern/machine/setjmp.h ../../include/limits.h \
-  ../../include/kern/limits.h ../../include/list.h \
+  ../../include/kern/limits.h ../../include/synch.h ../../include/list.h \
   ../../include/current.h includelinks/machine/current.h \
   ../../include/addrspace.h ../../include/vm.h includelinks/machine/vm.h \
   ../../include/vfs.h ../../include/syscall.h ../../include/test.h
@@ -499,10 +500,11 @@ thread.o: ../../thread/thread.c ../../include/types.h \
   ../../include/thread.h includelinks/machine/thread.h \
   ../../include/setjmp.h includelinks/kern/machine/setjmp.h \
   ../../include/threadprivate.h ../../include/proc.h \
-  ../../include/limits.h ../../include/kern/limits.h ../../include/list.h \
-  ../../include/current.h includelinks/machine/current.h \
-  ../../include/synch.h ../../include/addrspace.h ../../include/vm.h \
-  ../../include/mainbus.h ../../include/vnode.h opt-synchprobs.h
+  ../../include/limits.h ../../include/kern/limits.h \
+  ../../include/synch.h ../../include/list.h ../../include/current.h \
+  includelinks/machine/current.h ../../include/addrspace.h \
+  ../../include/vm.h ../../include/mainbus.h ../../include/vnode.h \
+  opt-synchprobs.h
 threadlist.o: ../../thread/threadlist.c ../../include/types.h \
   ../../include/kern/types.h includelinks/kern/machine/types.h \
   includelinks/machine/types.h ../../include/lib.h ../../include/cdefs.h \
@@ -536,7 +538,7 @@ vfscwd.o: ../../vfs/vfscwd.c ../../include/types.h \
   ../../include/thread.h ../../include/array.h ../../include/threadlist.h \
   includelinks/machine/thread.h ../../include/setjmp.h \
   includelinks/kern/machine/setjmp.h ../../include/limits.h \
-  ../../include/kern/limits.h ../../include/list.h \
+  ../../include/kern/limits.h ../../include/synch.h ../../include/list.h \
   ../../include/current.h includelinks/machine/current.h \
   ../../include/vfs.h ../../include/fs.h ../../include/vnode.h
 vfslist.o: ../../vfs/vfslist.c ../../include/types.h \
@@ -642,7 +644,7 @@ dumbvm.o: ../../arch/mips/vm/dumbvm.c ../../include/types.h \
   ../../include/thread.h ../../include/array.h ../../include/threadlist.h \
   includelinks/machine/thread.h ../../include/setjmp.h \
   includelinks/kern/machine/setjmp.h ../../include/limits.h \
-  ../../include/kern/limits.h ../../include/list.h \
+  ../../include/kern/limits.h ../../include/synch.h ../../include/list.h \
   ../../include/current.h includelinks/machine/current.h \
   includelinks/mips/tlb.h ../../include/addrspace.h ../../include/vm.h \
   includelinks/machine/vm.h

BIN
kern/compile/ASST2/kernel


+ 1 - 1
kern/compile/ASST2/vers.c

@@ -1,3 +1,3 @@
 /* This file is automatically generated. Edits will be lost.*/
-const int buildversion = 16;
+const int buildversion = 58;
 const char buildconfig[] = "ASST2";

+ 1 - 1
kern/compile/ASST2/version

@@ -1 +1 @@
-16
+58

+ 4 - 1
kern/include/proc.h

@@ -39,6 +39,7 @@
 #include <spinlock.h>
 #include <thread.h> /* required for struct threadarray */
 #include <limits.h> // using to restrict array size of procs
+#include <synch.h>
 #include <list.h>
 
 struct addrspace;
@@ -57,6 +58,8 @@ struct proc
 	struct proc * parent; // again, pretty damn obvious
 	struct list * kids; // list of kid processes (by pid)
 	int exitcode; // exitcode if exited, -1 if running
+	struct cv * waiting; // parents can wait on this CV
+	struct lock * waitlock; // waitlock for the CV
 	struct spinlock p_lock;		/* Lock for this structure */
 	struct threadarray p_threads;	/* Threads in this process */
 
@@ -81,7 +84,7 @@ struct proc
 // struct that contains all the processes in the system
 struct procs
 {
-  struct proc  * pids[PID_MAX - PID_MIN + 1]; // total # of valid PIDs
+  struct proc  * pids[PID_MAX + 1]; // total # of valid PIDs
   int lastpid; // last pid issued
 };
 

+ 28 - 13
kern/proc/proc.c

@@ -110,6 +110,8 @@ static struct proc * proc_create(const char * name)
 
 	threadarray_init(&proc->p_threads);
 	spinlock_init(&proc->p_lock);
+	proc->waitlock = lock_create("threadlock");
+	proc->waiting = cv_create("threadcv");
 
 	/* VM fields */
 	proc->p_addrspace = NULL;
@@ -124,6 +126,7 @@ static struct proc * proc_create(const char * name)
   // My additions
   proc->pid =0;
   proc->parent = NULL;
+  proc->exitcode = -1;
 
 	return proc;
 }
@@ -139,7 +142,7 @@ static procs * create_procs(void)
 struct proc * getChild(struct proc * p, int pid)
 {
   int result = listearch(p->kids, pid);
-  
+
   if (!(result)) return NULL;
   return processes->pids[pid];
 }
@@ -168,12 +171,12 @@ int assignpid(struct proc * proc)
     if (i == processes->lastpid)
     {
       lock_release(proclock);
-      return ENPROC;
+      return 0;
     }
   }
   
   lock_release(proclock);
-  return ENPROC;
+  return 0;
 }
 
 void add_child(struct proc * parent, int pid)
@@ -210,7 +213,7 @@ void proc_destroy(struct proc * proc)
 	 * reference to this structure. (Otherwise it would be
 	 * incorrect to destroy it.)
 	 */
-
+  kprintf("TRYING TO DESTROY CWD\n");
 	/* VFS fields */
 	if (proc->p_cwd)
 	{
@@ -218,7 +221,7 @@ void proc_destroy(struct proc * proc)
 		proc->p_cwd = NULL;
 	}
 
-
+  kprintf("DESTROYED CWD\n");
 #ifndef UW  // in the UW version, space destruction occurs in sys_exit, not here
 	if (proc->p_addrspace)
 	{
@@ -240,22 +243,30 @@ void proc_destroy(struct proc * proc)
 	}
 #endif // UW
 
+  kprintf("DESTROYED AS AGAIN\n");
+/*
 #ifdef UW
 	if (proc->console)
 	{
 	  vfs_close(proc->console);
 	}
-#endif // UW
-
-	threadarray_cleanup(&proc->p_threads);
-	spinlock_cleanup(&proc->p_lock);
+#endif // UW*/
 
+  kprintf("DESTROYED VFS\n");
+	//threadarray_cleanup(&proc->p_threads);
+	//spinlock_cleanup(&proc->p_lock);
+  kprintf("CLEANUP DONE\n");
   lock_acquire(proclock);
   processes->pids[proc->pid] = NULL; // update available PIDs
   lock_release(proclock);
-  listelete(proc->kids);
+  kprintf("PID RELEASED\n");
+  //listelete(proc->kids);
+  lock_destroy(proc->waitlock);
+  cv_destroy(proc->waiting);
+  kprintf("CV DESTROYED\n");
 	kfree(proc->p_name);
 	kfree(proc);
+	kprintf("KFREES DONE\n");
 
 #ifdef UW
 	/* decrement the process count */
@@ -274,7 +285,7 @@ void proc_destroy(struct proc * proc)
 	V(proc_count_mutex);
 #endif // UW
 	
-
+  kprintf("DESTROYS DONE\n");
 }
 
 /*
@@ -302,7 +313,7 @@ void proc_bootstrap(void)
 #endif // UW
   
   processes = create_procs();
-  int processesLen = PID_MAX - PID_MIN + 1;
+  int processesLen = PID_MAX;
   for (int i = 0; i < processesLen; i++)
   {
     processes->pids[i] = NULL;
@@ -374,7 +385,11 @@ struct proc * proc_create_runprogram(const char * name)
            are created using a call to proc_create_runprogram  */
 	P(proc_count_mutex);
 	proc_count++;
-	assignpid(proc);
+	if (!(assignpid(proc)))
+	{
+	  kfree(proc);
+	  return NULL;
+	}
 	V(proc_count_mutex);
 #endif // UW
 

+ 134 - 33
kern/syscall/proc_syscalls.c

@@ -4,6 +4,8 @@
 #include <kern/wait.h>
 #include <mips/trapframe.h>
 #include <lib.h>
+#include <vfs.h>
+#include <vnode.h>
 #include <syscall.h>
 #include <current.h>
 #include <proc.h>
@@ -15,37 +17,132 @@
   /* this implementation of sys__exit does not do anything with the exit code */
   /* this needs to be fixed to get exit() and waitpid() working properly */
 
-void sys__exit(int exitcode)
+/*
+logic for waiting:
+
+on exit: broadcast. then
+if you can (parent is exited, check exitcode), DELETE
+if not, this means you are waiting for a parent to die or call wait on you
+  so, if they call wait, they can call the death for you
+  if they just kill themselves, they can do that then kill you too
+
+on call of waitpid:
+wait on CV of child, it will broadcast when exiting
+when you wake up within waitpid, kid has exited. delete the kid.
+continue on your merry day
+*/
+
+/*
+void waitpid()
 {
+  cv_wait(kid); // if valid thing to wait on, ofc
+  fullDelete(kid);
+}*/
 
-  struct addrspace * as;
-  struct proc * p = curproc;
-  /* for now, just include this to keep the compiler from complaining about
-     an unused variable */
-  (void)exitcode;
+/*
+void fullDelete() // this should just be delete_proc or whatever
+{
+  
+}*/
+
+
+/*
+void(exit)
+{
+  // at this point, all unneeded shit is gone
+  cv_broadcast(this->waiting);
+  if (parent->exitcode >= 0) fullDelete(this);
+  else return; // parent will clean you up
+}*/
 
+////////////////////////////////////////////////////////////////////////////
+
+/*void sys__exit(int exitcode)
+{
+  //struct addrspace * as;
+  struct proc * p = curproc;
+  p->exitcode = exitcode;
   DEBUG(DB_SYSCALL,"Syscall: _exit(%d)\n",exitcode);
+  //KASSERT(curproc->p_addrspace != NULL);
+  //as_deactivate();
+  //as = curproc_setas(NULL);
+  //as_destroy(as);
+
+  //kprintf("DESTROYED AS\n");
+  // detach this thread from its process
+  // note: curproc cannot be used after this call
+  proc_remthread(curthread);
+  //listelete(p->kids);
+  kprintf("DESTROYED KIDS\n");
+  //threadarray_cleanup(&p->p_threads);
+	//spinlock_cleanup(&p->p_lock);
+	lock_acquire(p->waitlock);
+  cv_broadcast(p->waiting, p->waitlock);
+  lock_release(p->waitlock);
+  kprintf("BROADCASTED\n");
+  
+  if (!(p->parent)) proc_destroy(p);
+  if (p->parent->exitcode >= 0) proc_destroy(p);
+  kprintf("EXITED THREAD\n");
+  thread_exit();
+  // thread_exit() does not return, so we should never get here
+  panic("return from thread_exit in sys_exit\n");
+}*/
 
+void sys__exit(int exitcode)
+{
+  struct addrspace * as;
+  struct proc * p = curproc;
+  p->exitcode = exitcode;
+  DEBUG(DB_SYSCALL,"Syscall: _exit(%d)\n",exitcode);
   KASSERT(curproc->p_addrspace != NULL);
+  kprintf("TRYING TO DESTROY CWD\n");
+	/* VFS fields */
+	if (p->p_cwd)
+	{
+		VOP_DECREF(p->p_cwd);
+		p->p_cwd = NULL;
+	}
+
+  kprintf("DESTROYED CWD\n");
   as_deactivate();
-  /*
-   * clear p_addrspace before calling as_destroy. Otherwise if
-   * as_destroy sleeps (which is quite possible) when we
-   * come back we'll be calling as_activate on a
-   * half-destroyed address space. This tends to be
-   * messily fatal.
-   */
   as = curproc_setas(NULL);
   as_destroy(as);
+  
+  if (p->console)
+	{
+	  vfs_close(p->console);
+	}
 
   /* detach this thread from its process */
   /* note: curproc cannot be used after this call */
   proc_remthread(curthread);
 
+  listelete(p->kids);
+  threadarray_cleanup(&p->p_threads);
+	spinlock_cleanup(&p->p_lock);
+	
+	lock_acquire(p->waitlock);
+  cv_broadcast(p->waiting, p->waitlock);
+  lock_release(p->waitlock);
+  
+  kprintf("This is the parent: %p\n", p->parent);
+  if (p->parent)
+  {
+    kprintf("This is the exitcode: %d\n", p->parent->exitcode);
+  }
+  if (!(p->parent)) proc_destroy(p);
+  else if (p->parent->exitcode >= 0)
+  {
+    kprintf("This is the exitcode: %d\n", p->parent->exitcode);
+    proc_destroy(p);
+  }
+  
   /* if this is the last user process in the system, proc_destroy()
      will wake up the kernel menu thread */
-  proc_destroy(p);
+  //proc_destroy(p);
   
+  kprintf("Exiting\n");
   thread_exit();
   /* thread_exit() does not return, so we should never get here */
   panic("return from thread_exit in sys_exit\n");
@@ -60,34 +157,38 @@ int sys_getpid(pid_t * retval)
   return(0);
 }
 
+/*
+void waitpid()
+{
+  cv_wait(kid); // if valid thing to wait on, ofc
+  fullDelete(kid);
+}*/
+
 /* stub handler for waitpid() system call                */
 int sys_waitpid(pid_t pid, userptr_t status, int options, pid_t * retval)
 {
-  int exitstatus;
-  int result;
-
-  /* this is just a stub implementation that always reports an
-     exit status of 0, regardless of the actual exit status of
-     the specified process.
-     In fact, this will return 0 even if the specified process
-     is still running, and even if #include <kern/wait.h>it never existed in the first place.
-
-     Fix this!
-  */
-
+  kprintf("Waiting on: %d\n", pid);
+  (void)status;
   if (options != 0)
   {
     return(EINVAL);
   }
-  /* for now, just pretend the exitstatus is 0 */
-  exitstatus = 0;
-  result = copyout((void *)&exitstatus,status,sizeof(int));
-  if (result)
+  
+  if (pid < PID_MIN || pid > PID_MAX) return ESRCH;
+  struct proc * target = getChild(curproc, pid);
+  
+  if (!(target)) return ECHILD;
+  if (target->exitcode >= 0)
   {
-    return(result);
+    *retval = target->exitcode;
+    return 0;
   }
-  *retval = pid;
-  return(0);
+  
+  lock_acquire(target->waitlock);
+  cv_wait(target->waiting, target->waitlock);
+  lock_release(target->waitlock);
+  proc_destroy(target);
+  return 0;
 }
 
 int sys_fork(struct trapframe * tf, int * retval)