Skip to main content

os/grading/
chapter6_process.rs

1//! Chapter 6 Grader: Process Management
2//!
3//! Tests: ProcessId, ProcessState, ProcessContext, PCB,
4//! scheduler operations, fork/exec/wait/exit.
5
6#![allow(clippy::vec_init_then_push)]
7
8use super::*;
9use crate::process::{ProcessContext, ProcessControlBlock, ProcessId, ProcessState};
10use crate::syscall::SyscallId;
11
12pub struct Chapter6Grader {
13    results: Vec<TaskResult>,
14}
15
16impl Chapter6Grader {
17    pub fn new() -> Self {
18        Self {
19            results: Vec::new(),
20        }
21    }
22
23    fn add(&mut self, task_id: usize, name: &'static str, points: u32, details: Vec<GradeDetail>) {
24        self.results.push(TaskResult {
25            task_id,
26            name,
27            points,
28            details,
29        });
30    }
31}
32
33impl Default for Chapter6Grader {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39impl Grade for Chapter6Grader {
40    fn chapter_number(&self) -> u32 {
41        6
42    }
43    fn chapter_name(&self) -> &'static str {
44        "Process Management"
45    }
46    fn total_tasks(&self) -> usize {
47        10
48    }
49    fn total_points(&self) -> u32 {
50        80
51    }
52
53    fn check_all(&mut self) {
54        self.results.clear();
55        self.check_pid_allocator();
56        self.check_process_state();
57        self.check_process_context();
58        self.check_pcb_basics();
59        self.check_pcb_children();
60        self.check_scheduler_create();
61        self.check_scheduler_add();
62        self.check_fork_exists();
63        self.check_exec_wait_exit_exist();
64        self.check_integration();
65    }
66
67    fn results(&self) -> &[TaskResult] {
68        &self.results
69    }
70}
71
72impl Chapter6Grader {
73    // Task 1: PID allocator
74    fn check_pid_allocator(&mut self) {
75        let p1 = ProcessId::new();
76        let p2 = ProcessId::new();
77        let p3 = ProcessId::new();
78
79        let mut details = Vec::new();
80        details.push(check_condition(
81            p1 != p2 && p2 != p3 && p1 != p3,
82            "PID allocator returns unique IDs",
83            "PIDs should be unique",
84        ));
85        // PIDs start from a counter — newer PIDs should be larger
86        details.push(pass(alloc::format!(
87            "Allocated PIDs: {}, {}, {}",
88            p1,
89            p2,
90            p3
91        )));
92
93        self.add(1, "PID allocator uniqueness", 6, details);
94    }
95
96    // Task 2: ProcessState transitions
97    fn check_process_state(&mut self) {
98        let mut details = Vec::new();
99
100        details.push(check_condition(
101            ProcessState::Ready.can_schedule(),
102            "Ready state can be scheduled",
103            "Ready.can_schedule() should be true",
104        ));
105        details.push(check_condition(
106            !ProcessState::Running.can_schedule(),
107            "Running state cannot be scheduled",
108            "Running.can_schedule() should be false",
109        ));
110        details.push(check_condition(
111            !ProcessState::Zombie.can_schedule(),
112            "Zombie state cannot be scheduled",
113            "Zombie.can_schedule() should be false",
114        ));
115
116        // Valid transitions
117        details.push(check_condition(
118            ProcessState::Ready.can_transition_to(ProcessState::Running),
119            "Ready → Running transition valid",
120            "Ready → Running should be valid",
121        ));
122        details.push(check_condition(
123            ProcessState::Running.can_transition_to(ProcessState::Zombie),
124            "Running → Zombie transition valid",
125            "Running → Zombie should be valid",
126        ));
127        details.push(check_condition(
128            ProcessState::Blocked.can_transition_to(ProcessState::Ready),
129            "Blocked → Ready transition valid",
130            "Blocked → Ready should be valid",
131        ));
132
133        self.add(2, "ProcessState transitions", 8, details);
134    }
135
136    // Task 3: ProcessContext
137    fn check_process_context(&mut self) {
138        let mut ctx = ProcessContext::new();
139        ctx.set_ra(0x80200000);
140        ctx.set_sp(0x80300000);
141
142        let mut details = Vec::new();
143        details.push(check_condition(
144            ctx.ra() == 0x80200000,
145            "RA stored and retrieved correctly",
146            "RA mismatch",
147        ));
148        details.push(check_condition(
149            ctx.sp() == 0x80300000,
150            "SP stored and retrieved correctly",
151            "SP mismatch",
152        ));
153        details.push(pass("ProcessContext fields accessible"));
154
155        self.add(3, "ProcessContext read/write", 6, details);
156    }
157
158    // Task 4: PCB basics
159    fn check_pcb_basics(&mut self) {
160        let pid = ProcessId::new();
161        let pcb = ProcessControlBlock::new(pid, "test_proc", None);
162
163        let mut details = Vec::new();
164        details.push(check_condition(
165            pcb.name() == "test_proc",
166            "PCB name stored correctly",
167            "PCB name mismatch",
168        ));
169        details.push(check_condition(
170            pcb.children().is_empty(),
171            "New PCB has no children",
172            "New PCB should have empty children",
173        ));
174        details.push(pass(alloc::format!(
175            "PCB created: PID={} name='{}'",
176            pid,
177            pcb.name()
178        )));
179
180        self.add(4, "PCB creation and fields", 8, details);
181    }
182
183    // Task 5: PCB parent-child relationships
184    fn check_pcb_children(&mut self) {
185        let parent_pid = ProcessId::new();
186        let child_pid = ProcessId::new();
187        let mut parent = ProcessControlBlock::new(parent_pid, "parent", None);
188        parent.add_child(child_pid);
189
190        let mut details = Vec::new();
191        details.push(check_condition(
192            parent.children().len() == 1,
193            "Child added to parent",
194            "Parent should have 1 child",
195        ));
196        details.push(pass(alloc::format!(
197            "Parent PID={} has child PID={}",
198            parent_pid,
199            child_pid
200        )));
201
202        self.add(5, "PCB parent-child relationship", 8, details);
203    }
204
205    // Task 6: Scheduler creation
206    fn check_scheduler_create(&mut self) {
207        use crate::process::RoundRobinScheduler;
208        let sched = RoundRobinScheduler::new(10);
209
210        let mut details = Vec::new();
211        // New scheduler should have no current task
212        details.push(pass("RoundRobinScheduler created with time_slice=10"));
213        details.push(check_condition(
214            sched.current().is_none(),
215            "New scheduler has no current task",
216            "New scheduler should start empty",
217        ));
218
219        core::mem::drop(sched);
220        self.add(6, "Scheduler initialisation", 8, details);
221    }
222
223    // Task 7: Add processes to scheduler
224    fn check_scheduler_add(&mut self) {
225        use crate::process::RoundRobinScheduler;
226        let mut sched = RoundRobinScheduler::new(10);
227        let p1 = ProcessId::new();
228        let p2 = ProcessId::new();
229        let proc1 = alloc::boxed::Box::new(ProcessControlBlock::new(p1, "p1", None));
230        let proc2 = alloc::boxed::Box::new(ProcessControlBlock::new(p2, "p2", None));
231
232        let r1 = sched.add_process(proc1);
233        let r2 = sched.add_process(proc2);
234
235        let mut details = Vec::new();
236        details.push(check_condition(
237            r1.is_ok(),
238            "Process 1 added",
239            "Failed to add process 1",
240        ));
241        details.push(check_condition(
242            r2.is_ok(),
243            "Process 2 added",
244            "Failed to add process 2",
245        ));
246        details.push(pass("Two processes registered in scheduler"));
247
248        self.add(7, "Scheduler add_process", 8, details);
249    }
250
251    // Task 8: fork mechanism exists
252    fn check_fork_exists(&mut self) {
253        let mut details = Vec::new();
254        // Verify fork syscall ID is defined
255        details.push(check_condition(
256            SyscallId::from(220) == SyscallId::Fork,
257            "fork syscall ID 220 defined",
258            "fork syscall ID missing",
259        ));
260        details.push(pass(
261            "fork_current() entry point available in process/mod.rs",
262        ));
263        self.add(8, "fork system call presence", 8, details);
264    }
265
266    // Task 9: exec/wait/exit mechanisms exist
267    fn check_exec_wait_exit_exist(&mut self) {
268        let mut details = Vec::new();
269        details.push(check_condition(
270            SyscallId::from(221) == SyscallId::Exec,
271            "exec syscall ID 221 defined",
272            "exec syscall ID missing",
273        ));
274        details.push(check_condition(
275            SyscallId::from(260) == SyscallId::Wait,
276            "wait syscall ID 260 defined",
277            "wait syscall ID missing",
278        ));
279        details.push(check_condition(
280            SyscallId::from(93) == SyscallId::Exit,
281            "exit syscall ID 93 defined",
282            "exit syscall ID missing",
283        ));
284        details.push(pass("exec/exit/wait entry points in process/mod.rs"));
285        self.add(9, "exec/wait/exit presence", 8, details);
286    }
287
288    // Task 10: Integration check
289    fn check_integration(&mut self) {
290        let mut details = Vec::new();
291        details.push(pass("Process management module compiles without errors"));
292        details.push(pass("SCHEDULER static is accessible"));
293        details.push(pass(
294            "All syscall IDs (fork/exec/wait/exit/getpid) registered",
295        ));
296        self.add(10, "Module integration", 12, details);
297    }
298}