Fork: Difference between revisions

Content added Content deleted
({{omit from|VBA}})
Line 96: Line 96:
I am the child, my PID is:12733</pre>
I am the child, my PID is:12733</pre>


=={{header|Batch File}}==
While you cannot fork into asynchronous subroutines conventionally, there are workarounds involving the <code>start</code> command.

<lang dos>
@echo off

if "%1" neq "" goto %1 || echo Not a valid subroutine

echo Starting mySubroutine1
start "" "%~n0" mySubroutine1
echo.

echo Starting mySubroutine2 6 3
start "" "%~n0" mySubroutine2 6 3
echo.

echo Starting mySubroutine3
start "" "%~n0" mySubroutine3
echo.

:: We wait here for the subroutines to run, but they are running asynchronously
timeout /t 1

for /l %%i in (1,1,3) do (
for /f "tokens=*" %%j in (output%%i.txt) do (
set output%%i=%%j
del output%%i.txt
)
)
echo.
echo.
echo Return values
echo ----------------------------
echo mySubroutine1: %output1%
echo mySubroutine2: %output2%
echo mySubroutine3: %output3%

pause>nul
exit

:mySubroutine1
echo This is the result of subroutine1 > output1.txt
exit

:mySubroutine2
set /a result=%2+%3
echo %result% > output2.txt
exit

:mySubroutine3
echo mySubroutine1 hasn't been run > output3.txt
if exist output1.txt echo mySubroutine1 has been run > output3.txt
exit
</lang>

Output:

<pre>
Starting mySubroutine1

Starting mySubroutine2 6 3

Starting mySubroutine3


Waiting for 0 seconds, press a key to continue ...


Return values
----------------------------
mySubroutine1: This is the result of subroutine1
mySubroutine2: 9
mySubroutine3: mySubroutine1 has been run
</pre>
=={{header|C}}==
=={{header|C}}==
{{libheader|POSIX}}<lang c>#include <stdio.h>
{{libheader|POSIX}}<lang c>#include <stdio.h>