* @brief 测试基本的 wait 功能 */
| 49 | * @brief 测试基本的 wait 功能 |
| 50 | */ |
| 51 | void test_wait_basic(void* /*arg*/) { |
| 52 | klog::Info("=== Wait Basic Test ==="); |
| 53 | |
| 54 | auto& task_mgr = TaskManagerSingleton::instance(); |
| 55 | auto* current = task_mgr.GetCurrentTask(); |
| 56 | |
| 57 | if (!current) { |
| 58 | klog::Err("Wait Basic Test: Cannot get current task"); |
| 59 | sys_exit(1); |
| 60 | } |
| 61 | |
| 62 | // 创建子进程 |
| 63 | auto child = kstd::make_unique<TaskControlBlock>("WaitChild", 10, child_work, |
| 64 | reinterpret_cast<void*>(42)); |
| 65 | child->aux->parent_pid = current->pid; |
| 66 | child->aux->pgid = current->aux->pgid; |
| 67 | auto* child_raw = child.get(); |
| 68 | |
| 69 | task_mgr.AddTask(std::move(child)); |
| 70 | Pid child_pid = child_raw->pid; |
| 71 | |
| 72 | klog::Info("Parent: created child with PID={}", child_pid); |
| 73 | |
| 74 | // 等待子进程退出 |
| 75 | int status = 0; |
| 76 | Pid result = task_mgr.Wait(child_pid, &status, false, false).value_or(0); |
| 77 | |
| 78 | if (result == child_pid) { |
| 79 | klog::Info("Parent: child {} exited with status {}", result, status); |
| 80 | if (status == 42) { |
| 81 | klog::Info("Wait Basic Test: PASS"); |
| 82 | } else { |
| 83 | klog::Err( |
| 84 | "Wait Basic Test: FAIL - wrong exit status (got {}, expected " |
| 85 | "42)", |
| 86 | status); |
| 87 | } |
| 88 | } else { |
| 89 | klog::Err("Wait Basic Test: FAIL - wait returned {} (expected {})", result, |
| 90 | child_pid); |
| 91 | } |
| 92 | |
| 93 | if (result == child_pid && status == 42) { |
| 94 | klog::Info("Wait Basic Test: PASS"); |
| 95 | } else { |
| 96 | klog::Err("Wait Basic Test: FAIL"); |
| 97 | g_tests_failed++; |
| 98 | } |
| 99 | |
| 100 | g_tests_completed++; |
| 101 | sys_exit(0); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @brief 测试等待任意子进程 |