#include <types.h>1#include <lib.h>2#include <proc.h>3#include <file.h>4#include <filedesc.h>5#include <thread.h>6#include <current.h>7#include <syscall.h>89/**10* exit will perform the following tasks:11* 1. close all open files (through file_close_all())12* 2. set the given exit code inside the proc structure.13* 3. mark each child process as orphan.14* 4. if orphan, will destroy the proc associated with the current thread.15* 4.1 if not orphan, will signal the parent regarding our death.16* 5. call thread_exit() so we become a zombie.17*/18void19sys__exit( int code ) {20struct proc *p = NULL;21int err;2223KASSERT( curthread != NULL );24KASSERT( curthread->td_proc != NULL );2526p = curthread->td_proc;2728//close all open files.29err = file_close_all( p );30if( err )31panic( "problem closing a file." );3233//lock so we can adjust the return value.34PROC_LOCK( p );35p->p_retval = code;36p->p_is_dead = true;3738//if we are orphans ourselves, no one is interested39//in our return code, so we simply destroy ourselves.40if( p->p_proc == NULL ) {41PROC_UNLOCK( p );42proc_destroy( p );43}44else {45//signal that we are done.46V( p->p_sem );47PROC_UNLOCK( p );48}4950//all that is left now is to kill our thread.51thread_exit();52}535455