| 1 |
import core.memory; |
| 2 |
import core.stdc.stdio : printf; |
| 3 |
import core.sys.posix.sys.wait; |
| 4 |
import core.sys.posix.unistd; |
| 5 |
|
| 6 |
void main() |
| 7 |
{ |
| 8 |
printf("[parent] Creating garbage...\n"); |
| 9 |
foreach (n; 0 .. 1_000) |
| 10 |
new uint[10_000]; |
| 11 |
printf("[parent] Collecting garbage...\n"); |
| 12 |
GC.collect(); |
| 13 |
printf("[parent] Forking...\n"); |
| 14 |
auto i = fork(); |
| 15 |
if (i < 0) |
| 16 |
assert(false, "Fork failed"); |
| 17 |
if (i == 0) |
| 18 |
{ |
| 19 |
printf("[child] In fork.\n"); |
| 20 |
printf("[child] Creating garbage...\n"); |
| 21 |
foreach (n; 0 .. 1_000) |
| 22 |
new uint[10_000]; |
| 23 |
printf("[child] Collecting garbage...\n"); |
| 24 |
GC.collect(); |
| 25 |
printf("[child] Exiting fork.\n"); |
| 26 |
} |
| 27 |
else |
| 28 |
{ |
| 29 |
printf("[parent] Waiting for fork (PID %d).\n", i); |
| 30 |
int status; |
| 31 |
i = waitpid(i, &status, 0); |
| 32 |
printf("[parent] Fork %d exited (%d).\n", i, status); |
| 33 |
if (status != 0) |
| 34 |
assert(false, "child had errors"); |
| 35 |
} |
| 36 |
} |