MCPcopy Create free account
hub / github.com/Simple-XX/SimpleKernel / sys_fork

Function sys_fork

src/syscall.cpp:147–174  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

145}
146
147[[nodiscard]] auto sys_fork() -> int {
148 auto& task_manager = TaskManagerSingleton::instance();
149 auto current = task_manager.GetCurrentTask();
150
151 if (!current || !current->trap_context_ptr) {
152 klog::Err("[Syscall] sys_fork: Invalid current task or trap context");
153 return -1;
154 }
155
156 // fork = clone with flags=0 (完全复制,不共享任何资源)
157 auto result = task_manager.Clone(0, nullptr, nullptr, nullptr, nullptr,
158 *current->trap_context_ptr);
159
160 if (!result.has_value()) {
161 // 失败返回 -1
162 klog::Err("[Syscall] sys_fork failed: {}", result.error().message());
163 return -1;
164 }
165
166 Pid child_pid = result.value();
167 if (child_pid == 0) {
168 // 子进程返回 0
169 return 0;
170 } else {
171 // 父进程返回子进程 PID
172 return static_cast<int>(child_pid);
173 }
174}
175
176[[nodiscard]] auto sys_gettid() -> int {
177 auto current = TaskManagerSingleton::instance().GetCurrentTask();

Callers 1

syscall_dispatcherFunction · 0.85

Calls 4

ErrFunction · 0.85
GetCurrentTaskMethod · 0.80
CloneMethod · 0.80
messageMethod · 0.80

Tested by

no test coverage detected