| 95 | void noop_work(void* /*arg*/) { sys_exit(0); } |
| 96 | |
| 97 | void test_clone_process(void* /*arg*/) { |
| 98 | klog::Info("=== Clone Process Test ==="); |
| 99 | |
| 100 | bool passed = true; |
| 101 | g_process_done = 0; |
| 102 | g_child1_pid = 0; |
| 103 | g_child1_tgid = 0; |
| 104 | g_child1_parent_pid = 0; |
| 105 | g_child2_pid = 0; |
| 106 | g_child2_tgid = 0; |
| 107 | g_child2_parent_pid = 0; |
| 108 | |
| 109 | auto& task_mgr = TaskManagerSingleton::instance(); |
| 110 | auto* self = task_mgr.GetCurrentTask(); |
| 111 | Pid my_pid = self->pid; |
| 112 | |
| 113 | auto child1 = kstd::make_unique<TaskControlBlock>( |
| 114 | "CloneChild1", 10, child_process_work, reinterpret_cast<void*>(1)); |
| 115 | child1->aux->parent_pid = my_pid; |
| 116 | auto* child1_raw = child1.get(); |
| 117 | task_mgr.AddTask(std::move(child1)); |
| 118 | Pid child1_pid = child1_raw->pid; |
| 119 | |
| 120 | auto child2 = kstd::make_unique<TaskControlBlock>( |
| 121 | "CloneChild2", 10, child_process_work, reinterpret_cast<void*>(2)); |
| 122 | child2->aux->parent_pid = my_pid; |
| 123 | auto* child2_raw = child2.get(); |
| 124 | task_mgr.AddTask(std::move(child2)); |
| 125 | Pid child2_pid = child2_raw->pid; |
| 126 | |
| 127 | (void)sys_sleep(200); |
| 128 | |
| 129 | Pid c1_pid = g_child1_pid.load(); |
| 130 | Pid c1_tgid = g_child1_tgid.load(); |
| 131 | Pid c2_pid = g_child2_pid.load(); |
| 132 | Pid c2_tgid = g_child2_tgid.load(); |
| 133 | |
| 134 | if (c1_pid == 0 || c2_pid == 0) { |
| 135 | klog::Err("Child processes did not start"); |
| 136 | passed = false; |
| 137 | } |
| 138 | |
| 139 | if (passed && (c1_tgid != c1_pid || c2_tgid != c2_pid)) { |
| 140 | klog::Err("Child tgid != pid: c1 tgid={} pid={}, c2 tgid={} pid={}", |
| 141 | c1_tgid, c1_pid, c2_tgid, c2_pid); |
| 142 | passed = false; |
| 143 | } |
| 144 | |
| 145 | if (passed && (g_child1_parent_pid.load() != my_pid || |
| 146 | g_child2_parent_pid.load() != my_pid)) { |
| 147 | klog::Err("Parent-child relationship incorrect"); |
| 148 | passed = false; |
| 149 | } |
| 150 | |
| 151 | if (passed && g_process_done.load() < 2) { |
| 152 | klog::Err("Not all child processes completed"); |
| 153 | passed = false; |
| 154 | } |