CPU_LWP_FORK(9)
π CPU_LWP_FORK(9) β the kernel dev girlies' manual
okay so bestie, welcome to the Kernel Developer's Manual page for CPU_LWP_FORK(9) β yes that's the real section number, we're in the big leagues now π
β¨ NAME β¨
cpu_lwp_fork, child_return, lwp_trampoline β finish a fork operation. like, the actual finishing touches, not just the vibe π₯
πΈ SYNOPSIS πΈ
#include <sys/proc.h>
void
cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack,
size_t stacksize, void (*func)(void *), void *arg);
void
child_return(void *arg);
copy that #include exactly, bestie, no cap π«Ά
π¦ DESCRIPTION π¦
Okay so cpu_lwp_fork() is the machine-dependent portion of fork1() that finishes a fork operation, with the child lwp l2 nearly set up β like, it's at 99% and we just need to hit send π
It copies and updates the PCB and the trap frame from the parent l1, making the child ready to run. literally that's the whole glow-up.
cpu_lwp_fork() rigs the child's kernel stack so that it will start in lwp_trampoline(). and lwp_trampoline() does not have a normal calling sequence β it's entered by cpu_switchto(), so don't come at it expecting a regular call, it's built different π
If an alternate user-level stack is requested β and that means non-zero values in both the stack and stacksize arguments, both of them, no half-sending β the user stack pointer is set up accordingly.
After being entered by cpu_switchto() and while running in user context (within the kernel), lwp_trampoline() will invoke the function func with the argument arg. iconic behavior honestly β¨
- If a kernel thread is being created, the return path and argument are specified with
funcandarg. - If a user process is being created,
fork1()will passchild_return()andl2tocpu_lwp_fork()asfuncandargrespectively.
This causes the newly-created child process to go directly to user level with an apparent return value of 0 from fork(2), while the parent process returns normally. so the child's like "hiii i returned 0" and the parent's like "i returned your pid" β both slaying, different paths π
π SEE ALSO π
read them, they're the supporting cast and they ate π
ngl this page is short but it's dense β every pointer matters. don't skip the "both non-zero" bit onstackandstacksize, that's the trap π
and that's the page, bestie!! this one came from Debian, dated April 26, 2018 β vintage but still serving π k byeee, go fork something responsibly π§ββοΈβ
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2002, 2005, 2006 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms