I replicated the following code from a python program to nim to create
processes via fork. But how to do to kill all processes (parent and child's) at
once?
import posix
import os
proc parent_child(p = 0) =
while true:
echo "Process: ", p
sleep(1000)
proc main() =
var children: seq[Pid]
let forks = 3
for f in 0 .. forks - 1:
let pid = fork()
if pid < 0:
# error forking a child
quit(QuitFailure)
elif pid > 0:
let p = getpid()
echo "In parent process: ", int(p)
children.add(pid)
else:
let p = getpid()
echo "In child process: ", int(p)
parent_child(f)
quit()
for child in children:
var status: cint
discard waitpid(child, status, 0)
main()
Run