| 284 | } |
| 285 | |
| 286 | void test_wait_zombie_reap(void* /*arg*/) { |
| 287 | klog::Info("=== Wait Zombie Reap Test ==="); |
| 288 | |
| 289 | g_wait_completed = 0; |
| 290 | auto& task_mgr = TaskManagerSingleton::instance(); |
| 291 | auto* current = task_mgr.GetCurrentTask(); |
| 292 | |
| 293 | if (!current) { |
| 294 | klog::Err("Wait Zombie Reap Test: Cannot get current task"); |
| 295 | sys_exit(1); |
| 296 | } |
| 297 | |
| 298 | // 创建快速退出的子进程(会变成僵尸) |
| 299 | auto child = kstd::make_unique<TaskControlBlock>( |
| 300 | "ZombieChild", 10, zombie_child_work, reinterpret_cast<void*>(1)); |
| 301 | child->aux->parent_pid = current->pid; |
| 302 | child->aux->pgid = current->aux->pgid; |
| 303 | auto* child_raw = child.get(); |
| 304 | task_mgr.AddTask(std::move(child)); |
| 305 | Pid child_pid = child_raw->pid; |
| 306 | |
| 307 | klog::Info("Parent: created zombie child with PID={}", child_pid); |
| 308 | |
| 309 | // 等待一段时间让子进程变成僵尸 |
| 310 | (void)sys_sleep(200); |
| 311 | |
| 312 | // 回收僵尸进程 |
| 313 | int status = 0; |
| 314 | Pid result = task_mgr.Wait(child_pid, &status, false, false).value_or(0); |
| 315 | |
| 316 | if (result == child_pid) { |
| 317 | klog::Info("Parent: successfully reaped zombie child {}", result); |
| 318 | klog::Info("Wait Zombie Reap Test: PASS"); |
| 319 | } else { |
| 320 | klog::Err("Wait Zombie Reap Test: FAIL - wait returned {}", result); |
| 321 | } |
| 322 | |
| 323 | g_tests_completed++; |
| 324 | sys_exit(0); |
| 325 | } |
| 326 | |
| 327 | } // namespace |
| 328 | |