mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 08:12:28 +00:00
Get fork() working on Windows
This is done without using Microsoft's internal APIs. MAP_PRIVATE mappings are copied to the subprocess via a pipe, since Microsoft doesn't want us to have proper COW pages. MAP_SHARED mappings are remapped without needing to do any copying. Global variables need copying along with the stack and the whole heap of anonymous mem. This actually improves the reliability of the redbean http server although one shouldn't expect 10k+ connections on a home computer that isn't running software built to serve like Linux or FreeBSD.
This commit is contained in:
parent
aea89fe832
commit
db33973e0a
105 changed files with 1476 additions and 912 deletions
|
@ -7,18 +7,20 @@
|
|||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||
╚─────────────────────────────────────────────────────────────────*/
|
||||
#endif
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/nt/nt/process.h"
|
||||
#include "libc/rand/rand.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
noinline void dostuff(void) {
|
||||
int i, us;
|
||||
srand(rand64()); /* seeds rand() w/ intel rdrnd, auxv, etc. */
|
||||
for (unsigned i = 0; i < 5; ++i) {
|
||||
int us = rand() % 500000;
|
||||
for (i = 0; i < 5; ++i) {
|
||||
us = rand() % 500000;
|
||||
usleep(us);
|
||||
printf("%s%u%s%u [%s=%d]\n", "hello no. ", i, " from ", getpid(), "us", us);
|
||||
fflush(stdout);
|
||||
|
@ -26,9 +28,8 @@ noinline void dostuff(void) {
|
|||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
fprintf(stderr, "%p\n", RtlCloneUserProcess);
|
||||
int child;
|
||||
if ((child = fork()) == -1) perror("fork"), exit(1);
|
||||
int rc, child, wstatus;
|
||||
CHECK_NE(-1, (child = fork()));
|
||||
if (!child) {
|
||||
/* child process */
|
||||
dostuff();
|
||||
|
@ -37,8 +38,7 @@ int main(int argc, char *argv[]) {
|
|||
/* parent process */
|
||||
dostuff();
|
||||
/* note: abandoned children become zombies */
|
||||
int rc, wstatus;
|
||||
if ((rc = wait(&wstatus)) == -1) perror("wait"), exit(1);
|
||||
CHECK_NE(-1, (rc = wait(&wstatus)));
|
||||
return WEXITSTATUS(wstatus);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue