diff options
author | Ian Moffett <ian@osmora.org> | 2025-05-15 17:41:29 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-05-15 18:01:30 -0400 |
commit | 02ca8f9faf6b6ddd0b7a514503b223e9ecbe8fe9 (patch) | |
tree | 905c09181f3ac1026b6d800c9151af527753665e /sys/kern | |
parent | 7c40f732d616a7a2bd005c51ca86d609c2dc4788 (diff) |
Simplicity is divine, fork() may be powerful but is no longer simple. It
became a thing in the late 70s during the early days of UNIX when computing
was simple, when CPUs were only 16-bits, MMUs were not prevalent and RAM was
only 512 KB. However it isn't 1971 anymore, process management, CPUs and memory
architecture has advanced significantly since. This commit ceases work on the
fork() syscall as implementing it would only introduce unnecessary complexity,
security issues, hefty processing overhead and would perpetuate what should
now be considered legacy. The current best alternative would be providing
a form of process spawning as well as a mechanism to wait for the child
process to complete.
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/init_main.c | 2 | ||||
-rw-r--r-- | sys/kern/kern_spawn.c (renamed from sys/kern/kern_fork.c) | 55 |
2 files changed, 33 insertions, 24 deletions
diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 667bb97..911785a 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -101,7 +101,7 @@ main(void) /* Startup pid 1 */ memset(&proc0, 0, sizeof(proc0.tf)); - fork1(&proc0, 0, start_init, NULL); + spawn(&proc0, 0, start_init, NULL); /* Load all drivers */ DRIVERS_INIT(); diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_spawn.c index abb7707..6adf726 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_spawn.c @@ -27,49 +27,62 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/mman.h> -#include <sys/tree.h> -#include <sys/types.h> #include <sys/proc.h> +#include <sys/mman.h> #include <sys/errno.h> -#include <sys/sched.h> +#include <sys/syslog.h> +#include <sys/atomic.h> #include <sys/signal.h> +#include <sys/sched.h> #include <vm/dynalloc.h> #include <string.h> -static size_t nthreads = 0; +#define pr_trace(fmt, ...) kprintf("spawn: " fmt, ##__VA_ARGS__) +#define pr_error(...) pr_trace(__VA_ARGS__) + +static volatile size_t nthreads = 0; /* - * Fork1 - fork and direct a thread to 'ip' + * Spawn a new process * - * @cur: Current process. - * @flags: Flags to set. - * @ip: Location for new thread to start at. - * @newprocp: Will contain new thread if not NULL. + * @cur: Parent (current) process. + * @flags: Spawn flags. + * @ip: Location for process to start + * @newprocp: If not NULL, will contain the new process. */ int -fork1(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) +spawn(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) { struct proc *newproc; struct mmap_lgdr *mlgdr; - int status = 0; + int error; newproc = dynalloc(sizeof(*newproc)); - if (newproc == NULL) + if (newproc == NULL) { + pr_error("could not alloc proc (-ENOMEM)\n"); return -ENOMEM; + } mlgdr = dynalloc(sizeof(*mlgdr)); - if (mlgdr == NULL) + if (mlgdr == NULL) { + dynfree(newproc); + pr_error("could not alloc proc mlgdr (-ENOMEM)\n"); return -ENOMEM; + } memset(newproc, 0, sizeof(*newproc)); - status = md_fork(newproc, cur, (uintptr_t)ip); - if (status != 0) - goto done; + error = md_spawn(newproc, cur, (uintptr_t)ip); + if (error < 0) { + dynfree(newproc); + dynfree(mlgdr); + pr_error("error initializing proc\n"); + return error; + } /* Set proc output if we can */ - if (newprocp != NULL) + if (newprocp != NULL) { *newprocp = newproc; + } /* Initialize the mmap ledger */ mlgdr->nbytes = 0; @@ -79,9 +92,5 @@ fork1(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) newproc->pid = ++nthreads; signals_init(newproc); sched_enqueue_td(newproc); -done: - if (status != 0) - dynfree(newproc); - - return status; + return 0; } |