keos_project3/
process.rs

1//! The process model for project3.
2//!
3//! This file defines the process model of the project3.
4
5use keos::{KernelError, thread::Current};
6use keos_project1::{file_struct::FileStruct, syscall::SyscallAbi};
7use keos_project2::mm_struct::MmStruct;
8
9use crate::lazy_pager::LazyPager;
10
11/// A process state of project 3, which contains file struct and mm struct.
12pub struct Process {
13    pub file_struct: FileStruct,
14    pub mm_struct: MmStruct<LazyPager>,
15}
16
17impl Default for Process {
18    fn default() -> Self {
19        Self {
20            file_struct: FileStruct::new(),
21            mm_struct: MmStruct::new(),
22        }
23    }
24}
25
26impl Process {
27    /// Create a process with given [`MmStruct`].
28    pub fn from_mm_struct(mm_struct: MmStruct<LazyPager>) -> Self {
29        Self {
30            mm_struct,
31            ..Default::default()
32        }
33    }
34
35    /// Exit a process.
36    ///
37    /// This function terminates the calling thread by invoking `exit` on the
38    /// current thread. The exit code is provided as the first argument
39    /// (`arg1`) of the system call.
40    ///
41    /// # Syscall API
42    /// ```c
43    /// int exit(int status);
44    /// ```
45    /// - `status`: The thread's exit code.
46    ///
47    /// # Parameters
48    /// - `abi`: A reference to `SyscallAbi`, which holds the arguments passed
49    ///   to the system call.
50    ///
51    /// # Returns
52    /// - Never returns.
53    ///
54    /// # Notes
55    /// - This function does not return in normal execution, as it terminates
56    ///   the thread.
57    /// - If an error occurs, it returns a `KernelError`
58    pub fn exit(&self, abi: &SyscallAbi) -> Result<usize, KernelError> {
59        Current::exit(abi.arg1 as i32)
60    }
61}