File: fork2.lua

package info (click to toggle)
lua-posix 31-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,604 kB
  • ctags: 1,346
  • sloc: sh: 14,309; ansic: 4,794; perl: 76; makefile: 41
file content (20 lines) | stat: -rw-r--r-- 578 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
require "posix"

print ("parent: my pid is: " .. posix.getpid ("pid"))

local pid = posix.fork ()

if pid == -1 then
  print ("parent: The fork failed.")
elseif pid == 0 then
  print ("child: Hello World! I am pid: " .. posix.getpid ("pid"))
  print ("child: I'll sleep for 1 second ... ")
  posix.sleep (1)
  print ("child: Good bye");
else
  print ("parent: While the child sleeps, I'm still running.")
  print ("parent: waiting for child (pid:" .. pid .. ") to die...")
  posix.wait (pid)
  print ("parent: child died, but I'm still alive.")
  print ("parent: Good bye")
end