Created
May 14, 2024 20:58
-
-
Save edgarcosta/220caee070355c5c6ef132dcf489e3b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MultiFork(n) | |
res := []; | |
for i in [1..n] do | |
if #res eq i - 1 then // only the parent gets inside of this loop | |
f := Fork(); | |
if f ne 0 then // parent branch | |
Append(~res, f); | |
else | |
return [], i; // child branch | |
end if; | |
end if; | |
end for; | |
return res, 0; | |
end function; | |
b, r := MultiFork(10); | |
print b, r; | |
if r ne 0 then | |
exit 0; | |
end if; | |
// If you remove this comment, everything goes as expected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment