os/syscall/
process.rs

1use crate::loader::get_app_data_by_name;
2use crate::mm::{translated_refmut, translated_str};
3use crate::task::{
4    add_task, current_task, current_user_token, exit_current_and_run_next,
5    suspend_current_and_run_next,
6};
7use crate::timer::get_time_ms;
8use alloc::sync::Arc;
9
10pub fn sys_exit(exit_code: i32) -> ! {
11    exit_current_and_run_next(exit_code);
12    panic!("Unreachable in sys_exit!");
13}
14
15pub fn sys_yield() -> isize {
16    suspend_current_and_run_next();
17    0
18}
19
20pub fn sys_get_time() -> isize {
21    get_time_ms() as isize
22}
23
24pub fn sys_getpid() -> isize {
25    current_task().unwrap().pid.0 as isize
26}
27
28pub fn sys_fork() -> isize {
29    let current_task = current_task().unwrap();
30    let new_task = current_task.fork();
31    let new_pid = new_task.pid.0;
32    // modify trap context of new_task, because it returns immediately after switching
33    let trap_cx = new_task.inner_exclusive_access().get_trap_cx();
34    // we do not have to move to next instruction since we have done it before
35    // for child process, fork returns 0
36    trap_cx.x[10] = 0;
37    // add new task to scheduler
38    add_task(new_task);
39    new_pid as isize
40}
41
42pub fn sys_exec(path: *const u8) -> isize {
43    let token = current_user_token();
44    let path = translated_str(token, path);
45    if let Some(data) = get_app_data_by_name(path.as_str()) {
46        let task = current_task().unwrap();
47        task.exec(data);
48        0
49    } else {
50        -1
51    }
52}
53
54/// If there is not a child process whose pid is same as given, return -1.
55/// Else if there is a child process but it is still running, return -2.
56pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
57    let task = current_task().unwrap();
58    // find a child process
59
60    // ---- access current TCB exclusively
61    let mut inner = task.inner_exclusive_access();
62    if !inner
63        .children
64        .iter()
65        .any(|p| pid == -1 || pid as usize == p.getpid())
66    {
67        return -1;
68        // ---- release current PCB
69    }
70    let pair = inner.children.iter().enumerate().find(|(_, p)| {
71        // ++++ temporarily access child PCB lock exclusively
72        p.inner_exclusive_access().is_zombie() && (pid == -1 || pid as usize == p.getpid())
73        // ++++ release child PCB
74    });
75    if let Some((idx, _)) = pair {
76        let child = inner.children.remove(idx);
77        // confirm that child will be deallocated after removing from children list
78        assert_eq!(Arc::strong_count(&child), 1);
79        let found_pid = child.getpid();
80        // ++++ temporarily access child TCB exclusively
81        let exit_code = child.inner_exclusive_access().exit_code;
82        // ++++ release child PCB
83        *translated_refmut(inner.memory_set.token(), exit_code_ptr) = exit_code;
84        found_pid as isize
85    } else {
86        -2
87    }
88    // ---- release current PCB lock automatically
89}