Created
September 25, 2015 04:34
-
-
Save ipoval/bdb1c7a966163bc3e971 to your computer and use it in GitHub Desktop.
Inter-process communication in UNIX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# IPC 1 - IO.pipe to set up the UNIX-pipe-based interprocess communication | |
in_r, in_w = IO.pipe; out_r, out_w = IO.pipe; err_r, err_w = IO.pipe | |
pid = spawn('cat -n', in: in_r, out: out_w, err: err_w) | |
[in_r, out_w, err_w].each(&:close) | |
in_w.puts("signal to process") | |
in_w.close | |
puts out_r.read | |
Process.waitpid(pid) | |
# IPC 2 - UNIX DOMAIN SOCKETS | |
# SERVER | |
require "socket" | |
server = UNIXServer.new('/tmp/simple.sock') | |
puts "==== Waiting for connection" | |
socket = server.accept # block executaion and wait for client connection to the socket | |
puts "==== Got Request:" | |
puts socket.readline # block execution and wait till the EOL | |
puts "==== Sending Response" | |
socket.write("I read you loud and clear, good buddy!") | |
socket.close | |
# CLIENT | |
require "socket" | |
socket = UNIXSocket.new('/tmp/simple.sock') | |
puts "==== Sending" | |
socket.write("Hello server, can you hear me?\n") | |
puts "==== Getting Response" | |
puts socket.readline | |
socket.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment