mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 22:02:27 +00:00
Initial import
This commit is contained in:
commit
c91b3c5006
14915 changed files with 590219 additions and 0 deletions
44
examples/forkrand.c
Normal file
44
examples/forkrand.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#if 0
|
||||
/*─────────────────────────────────────────────────────────────────╗
|
||||
│ To the extent possible under law, Justine Tunney has waived │
|
||||
│ all copyright and related or neighboring rights to this file, │
|
||||
│ as it is written in the following disclaimers: │
|
||||
│ • http://unlicense.org/ │
|
||||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||
╚─────────────────────────────────────────────────────────────────*/
|
||||
#endif
|
||||
#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) {
|
||||
srand(rand64()); /* seeds rand() w/ intel rdrnd, auxv, etc. */
|
||||
for (unsigned i = 0; i < 5; ++i) {
|
||||
int us = rand() % 500000;
|
||||
usleep(us);
|
||||
printf("%s%u%s%u [%s=%d]\n", "hello no. ", i, " from ", getpid(), "us", us);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
fprintf(stderr, "%p\n", RtlCloneUserProcess);
|
||||
int child;
|
||||
if ((child = fork()) == -1) perror("fork"), exit(1);
|
||||
if (!child) {
|
||||
/* child process */
|
||||
dostuff();
|
||||
return 0;
|
||||
} else {
|
||||
/* parent process */
|
||||
dostuff();
|
||||
/* note: abandoned children become zombies */
|
||||
int rc, wstatus;
|
||||
if ((rc = wait(&wstatus)) == -1) perror("wait"), exit(1);
|
||||
return WEXITSTATUS(wstatus);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue