File: fork.g

package info (click to toggle)
gap-io 4.4.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 740 kB
  • ctags: 106
  • sloc: xml: 2,857; ansic: 2,823; sh: 38; makefile: 35
file content (55 lines) | stat: -rw-r--r-- 1,216 bytes parent folder | download | duplicates (2)
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
55
# An example using fork:

LoadPackage("io");
IO_InstallSIGCHLDHandler();    # install correct signal handler

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;