From 91bb4d0238fd594b4b5d63c92ed9bdac19167a30 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Mon, 16 Dec 2024 14:28:51 -0500 Subject: [PATCH] libroot: Implement a "partial" vfork. The original meaning of vfork is "fork, sharing virtual memory" (until exec). We don't implement that, and may never do so. However, since calling any functions besides exec() in a vfork'ed child is "undefined behavior", we can take advantage of that fact at least by not calling any of the pre- and post-fork hooks, saving a lot of page faults from copy-on-write. On one run of the "compile HaikuDepot and the mime_db" benchmark with -j4, the total waits count on the top two VMCaches by contention dropped from 62125 and 58927, to 52034 and 41225. musl apparently does more or less this same thing (vfork() is fork() but without calling any of the hooks.) --- src/system/libroot/posix/unistd/fork.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/system/libroot/posix/unistd/fork.c b/src/system/libroot/posix/unistd/fork.c index feb32e23e7..333b65bf64 100644 --- a/src/system/libroot/posix/unistd/fork.c +++ b/src/system/libroot/posix/unistd/fork.c @@ -185,6 +185,11 @@ fork(void) pid_t vfork(void) { - return fork(); + thread_id thread = _kern_fork(); + if (thread < 0) { + // something went wrong + __set_errno(thread); + thread = -1; + } + return thread; } -