MCPcopy Create free account
hub / github.com/Simple-XX/SimpleKernel / test_orphan_exit

Function test_orphan_exit

tests/system_test/exit_test.cpp:281–345  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

279}
280
281void test_orphan_exit(void* /*arg*/) {
282 klog::Info("=== Orphan Exit Test ===");
283
284 bool passed = true;
285
286 // 1. 创建孤儿 TCB,验证 parent_pid == 0 被正确存储
287 auto* orphan = new TaskControlBlock("Orphan", 10, nullptr, nullptr);
288 orphan->pid = local_pid_counter.fetch_add(1);
289 orphan->aux->tgid = orphan->pid;
290 orphan->aux->parent_pid = 0; // 孤儿进程
291
292 if (orphan->aux->parent_pid != 0) {
293 klog::Err("test_orphan_exit: FAIL — parent_pid not stored as 0 (got {})",
294 orphan->aux->parent_pid);
295 passed = false;
296 }
297
298 // 2. 孤儿进程退出时预期进入 kExited 而非 kZombie(无父进程等待回收)
299 orphan->aux->exit_code = 0;
300 orphan->fsm.Receive(MsgSchedule{}); // kUnInit -> kReady
301 orphan->fsm.Receive(MsgSchedule{}); // kReady -> kRunning
302 orphan->fsm.Receive(MsgExit{0, false}); // kRunning -> kExited (no parent)
303 if (orphan->GetStatus() != TaskStatus::kExited) {
304 klog::Err(
305 "test_orphan_exit: FAIL — orphan status should be kExited "
306 "(got {})",
307 static_cast<int>(orphan->GetStatus()));
308 passed = false;
309 }
310 if (orphan->aux->parent_pid != 0) {
311 klog::Err("test_orphan_exit: FAIL — parent_pid changed unexpectedly");
312 passed = false;
313 }
314
315 // 3. 用实际工作函数验证孤儿任务能正常执行和退出
316 std::atomic<int> work_flag{0};
317 auto orphan_worker = kstd::make_unique<TaskControlBlock>(
318 "OrphanWorker", 10, orphan_work, reinterpret_cast<void*>(&work_flag));
319 orphan_worker->aux->parent_pid = 0; // 无父进程
320 TaskManagerSingleton::instance().AddTask(std::move(orphan_worker));
321
322 int timeout = 10;
323 while (timeout > 0 && work_flag.load() == 0) {
324 (void)sys_sleep(50);
325 timeout--;
326 }
327
328 if (work_flag.load() != 1) {
329 klog::Err("test_orphan_exit: FAIL — orphan worker did not complete");
330 passed = false;
331 } else {
332 klog::Info("test_orphan_exit: orphan worker completed");
333 }
334
335 if (passed) {
336 klog::Info("Orphan Exit Test: PASSED");
337 } else {
338 klog::Err("Orphan Exit Test: FAILED");

Callers

nothing calls this directly

Calls 7

InfoFunction · 0.85
ErrFunction · 0.85
sys_sleepFunction · 0.85
sys_exitFunction · 0.85
ReceiveMethod · 0.80
AddTaskMethod · 0.80
GetStatusMethod · 0.45

Tested by

no test coverage detected