Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ericfranz opened this issue Jun 16, 2020 · 0 comments
Open

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

ericfranz opened this issue Jun 16, 2020 · 0 comments

Comments

@ericfranz
Copy link
Owner

$ 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant