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