Skip to content

Explain why mkfifo and popen work the way they do #4

Open
@ericfranz

Description

@ericfranz
$ cat foo.rb
puts "These are the args: #{ARGV.inspect}"
puts "This is STDIN: #{STDIN.read}"
$
$ cat bar.lua
print("hi")

local named_pipe = '/tmp/pun-boot-pipe-' .. math.random(0,10000000)
print("named pipe created: " .. named_pipe)
os.execute('mkfifo ' .. named_pipe)

local handle = io.popen("cat " .. named_pipe .. " | ruby foo.rb a b c", "r")

local stdin_handle = io.open(named_pipe, "w")
stdin_handle:write("1 2 3 4 5 6")
stdin_handle:close()

local output = handle:read("*a")
handle:close()

os.execute('rm ' .. named_pipe)

print("printing captured output")
print(output)
$
$ lua bar.lua
hi
named pipe created: /tmp/pun-boot-pipe-8401878
printing captured output
These are the args: ["a", "b", "c"]
This is STDIN: 1 2 3 4 5 6

First use os.tmpname(). Second, be able to explain this:

  1. local output = handle:read("*a") does not execute till stdin_handle:write("1 2 3 4 5 6") and then stdin_handle:close() are complete and then local output = handle:read("*a") would not complete until local handle = io.popen("cat " .. named_pipe .. " | ruby foo.rb a b c", "r") completed
  2. why placing local stdin_handle = io.open(named_pipe, "w") before local handle = io.popen("cat " .. named_pipe .. " | ruby foo.rb a b c", "r") causes the application to hang

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions