File: fork2.lua

package info (click to toggle)
lua-posix 36.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,720 kB
  • sloc: ansic: 5,462; makefile: 21; sh: 6
file content (23 lines) | stat: -rw-r--r-- 653 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#! /usr/bin/env lua

local M = require 'posix.unistd'


print ('parent: my pid is: ' .. M.getpid())

local childpid, errmsg = M.fork ()

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