| 174 | } |
| 175 | |
| 176 | void test_wait_no_hang(void* /*arg*/) { |
| 177 | klog::Info("=== Wait NoHang Test ==="); |
| 178 | |
| 179 | auto& task_mgr = TaskManagerSingleton::instance(); |
| 180 | auto* current = task_mgr.GetCurrentTask(); |
| 181 | |
| 182 | if (!current) { |
| 183 | klog::Err("Wait NoHang Test: Cannot get current task"); |
| 184 | sys_exit(1); |
| 185 | } |
| 186 | |
| 187 | // 创建一个慢速子进程 |
| 188 | auto child = kstd::make_unique<TaskControlBlock>( |
| 189 | "SlowChild", 10, slow_child_work, reinterpret_cast<void*>(1)); |
| 190 | child->aux->parent_pid = current->pid; |
| 191 | child->aux->pgid = current->aux->pgid; |
| 192 | auto* child_raw = child.get(); |
| 193 | task_mgr.AddTask(std::move(child)); |
| 194 | Pid child_pid = child_raw->pid; |
| 195 | |
| 196 | klog::Info("Parent: created slow child with PID={}", child_pid); |
| 197 | |
| 198 | // 立即尝试非阻塞 wait |
| 199 | int status = 0; |
| 200 | Pid result = task_mgr.Wait(child_pid, &status, true, false).value_or(0); |
| 201 | |
| 202 | if (result == 0) { |
| 203 | klog::Info("Parent: no-hang wait returned 0 (child still running)"); |
| 204 | klog::Info("Wait NoHang Test: PASS"); |
| 205 | } else { |
| 206 | klog::Err( |
| 207 | "Wait NoHang Test: FAIL - expected 0, got {} (child shouldn't have " |
| 208 | "exited)", |
| 209 | result); |
| 210 | } |
| 211 | |
| 212 | // 清理:等待子进程实际退出(阻塞) |
| 213 | result = task_mgr.Wait(child_pid, &status, false, false).value_or(0); |
| 214 | klog::Info("Parent: child finally exited with PID={}", result); |
| 215 | |
| 216 | g_tests_completed++; |
| 217 | sys_exit(0); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * @brief 测试等待同进程组的子进程 |