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
|
#! /usr/bin/env lua
local M = require 'posix.unistd'
local function pipe()
local r, w = M.pipe()
assert(r ~= nil, w)
return r, w
end
local stdout_r, stdout_w = pipe()
local stderr_r, stderr_w = pipe()
local pid, errmsg = M.fork()
assert(pid ~= nil, errmsg)
if pid == 0 then
-- Child Process:
M.close(stdout_r)
M.close(stderr_r)
M.dup2(stdout_w, M.STDOUT_FILENO)
M.dup2(stderr_w, M.STDERR_FILENO)
-- Exec() a subprocess here instead if you like --
io.stdout:write 'output string'
io.stderr:write 'oh noes!'
os.exit(42)
else
-- Parent Process:
M.close(stdout_w)
M.close(stderr_w)
local function read(msg, fd)
local outs, errmsg = M.read(fd, 1024)
assert(outs ~= nil, errmsg)
print(msg, outs)
end
read('STDOUT:', stdout_r)
read('STDERR:', stderr_r)
local childpid, reason, status = require 'posix.sys.wait'.wait(pid)
assert(childpid ~= nil, reason)
print('child ' .. reason, status)
end
|