1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
# An example using fork:
LoadPackage("io");
pid := IO_fork();
if pid < 0 then
Error("Cannot fork!");
fi;
if pid > 0 then # the parent
Print("Did fork, now waiting for child...\n");
a := IO_WaitPid(pid,true);
Print("Got ",a," as result of WaitPid.\n");
else
# the child:
res := IO_execv("/bin/ls",["/tmp"]);
Print("execv did not work: ",res);
fi;
pid := IO_fork();
if pid < 0 then
Error("Cannot fork!");
fi;
if pid > 0 then # the parent
repeat
a := IO_WaitPid(pid,false);
Print(".\c");
until a <> false;
Print("Got ",a," as result of WaitPid.\n");
else
# the child:
e := IO_Environment();
e.myvariable := "xyz";
res := IO_execve("/usr/bin/env",[],IO_MakeEnvList(e));
Print("execve did not work: ",res);
fi;
pid := IO_fork();
if pid < 0 then
Error("Cannot fork!");
fi;
if pid > 0 then # the parent
repeat
a := IO_WaitPid(pid,false);
Print(".\c");
Sleep(1);
until a <> false;
Print("Got ",a," as result of WaitPid.\n");
else
# the child:
res := IO_execvp("sleep",["5"]);
Print("execvp did not work: ",res);
fi;
|