Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/syscall/_exit.c
2093 views
1
#include <types.h>
2
#include <lib.h>
3
#include <proc.h>
4
#include <file.h>
5
#include <filedesc.h>
6
#include <thread.h>
7
#include <current.h>
8
#include <syscall.h>
9
10
/**
11
* exit will perform the following tasks:
12
* 1. close all open files (through file_close_all())
13
* 2. set the given exit code inside the proc structure.
14
* 3. mark each child process as orphan.
15
* 4. if orphan, will destroy the proc associated with the current thread.
16
* 4.1 if not orphan, will signal the parent regarding our death.
17
* 5. call thread_exit() so we become a zombie.
18
*/
19
void
20
sys__exit( int code ) {
21
struct proc *p = NULL;
22
int err;
23
24
KASSERT( curthread != NULL );
25
KASSERT( curthread->td_proc != NULL );
26
27
p = curthread->td_proc;
28
29
//close all open files.
30
err = file_close_all( p );
31
if( err )
32
panic( "problem closing a file." );
33
34
//lock so we can adjust the return value.
35
PROC_LOCK( p );
36
p->p_retval = code;
37
p->p_is_dead = true;
38
39
//if we are orphans ourselves, no one is interested
40
//in our return code, so we simply destroy ourselves.
41
if( p->p_proc == NULL ) {
42
PROC_UNLOCK( p );
43
proc_destroy( p );
44
}
45
else {
46
//signal that we are done.
47
V( p->p_sem );
48
PROC_UNLOCK( p );
49
}
50
51
//all that is left now is to kill our thread.
52
thread_exit();
53
}
54
55