* @brief 测试等待同进程组的子进程 */
| 221 | * @brief 测试等待同进程组的子进程 |
| 222 | */ |
| 223 | void test_wait_process_group(void* /*arg*/) { |
| 224 | klog::Info("=== Wait Process Group Test ==="); |
| 225 | |
| 226 | auto& task_mgr = TaskManagerSingleton::instance(); |
| 227 | auto* current = task_mgr.GetCurrentTask(); |
| 228 | |
| 229 | if (!current) { |
| 230 | klog::Err("Wait Process Group Test: Cannot get current task"); |
| 231 | sys_exit(1); |
| 232 | } |
| 233 | |
| 234 | // 创建同进程组的子进程 |
| 235 | auto child1 = kstd::make_unique<TaskControlBlock>("PgChild1", 10, child_work, |
| 236 | reinterpret_cast<void*>(1)); |
| 237 | child1->aux->parent_pid = current->pid; |
| 238 | child1->aux->pgid = current->aux->pgid; // 同一进程组 |
| 239 | Pid child1_pgid = child1->aux->pgid; |
| 240 | auto* child1_raw = child1.get(); |
| 241 | task_mgr.AddTask(std::move(child1)); |
| 242 | Pid child1_pid = child1_raw->pid; |
| 243 | |
| 244 | auto child2 = kstd::make_unique<TaskControlBlock>("PgChild2", 10, child_work, |
| 245 | reinterpret_cast<void*>(2)); |
| 246 | child2->aux->parent_pid = current->pid; |
| 247 | child2->aux->pgid = 9999; // 不同进程组 |
| 248 | Pid child2_pgid = child2->aux->pgid; |
| 249 | auto* child2_raw = child2.get(); |
| 250 | task_mgr.AddTask(std::move(child2)); |
| 251 | Pid child2_pid = child2_raw->pid; |
| 252 | |
| 253 | klog::Info("Parent: created child1 (pgid={}) and child2 (pgid={})", |
| 254 | child1_pgid, child2_pgid); |
| 255 | |
| 256 | // 等待同进程组的子进程 (pid = 0) |
| 257 | int status = 0; |
| 258 | Pid result = task_mgr.Wait(0, &status, false, false).value_or(0); |
| 259 | |
| 260 | if (result == child1_pid) { |
| 261 | klog::Info("Parent: correctly waited for same-pgid child {}", result); |
| 262 | klog::Info("Wait Process Group Test: PASS"); |
| 263 | } else { |
| 264 | klog::Err("Wait Process Group Test: FAIL - got PID={}, expected {}", result, |
| 265 | child1_pid); |
| 266 | } |
| 267 | |
| 268 | // 清理另一个子进程 |
| 269 | result = task_mgr.Wait(child2_pid, &status, false, false).value_or(0); |
| 270 | klog::Info("Parent: cleaned up child2 PID={}", result); |
| 271 | |
| 272 | g_tests_completed++; |
| 273 | sys_exit(0); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * @brief 测试僵尸进程回收 |