keos_project2/
process.rs

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