File: fork.lua

package info (click to toggle)
lua-posix 36.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,720 kB
  • sloc: ansic: 5,462; makefile: 21; sh: 6
file content (30 lines) | stat: -rw-r--r-- 623 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
24
25
26
27
28
29
30
#! /usr/bin/env lua

local M = require 'posix.unistd'


local r,w = M.pipe()
local childpid = M.fork()
if childpid == 0 then
   -- child reads from pipe
   M.close(w)  -- close unused write end

   local b = M.read(r, 1)
   while #b == 1 do
      -- got a byte from the pipe, write it to stdout
      io.write(b)
      b = M.read(r, 1)
   end
   M.close(r)
   M._exit(0)

else
   -- parent writes to pipe
   M.close(r) -- close unused read end

   -- write the bytes for the child process to the pipe
   M.write(w,'hello dolly\n')
   M.close(w)
   -- wait for child to finish
   require 'posix.sys.wait'.wait(childpid)
end