Skip to content

Instantly share code, notes, and snippets.

@ksss
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksss/7a898e3e3189966314e1 to your computer and use it in GitHub Desktop.
Save ksss/7a898e3e3189966314e1 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'socket'
case ARGV[0]
when 'server'
sockfile = "/tmp/test.sock"
File.unlink(sockfile) if File.exists?(sockfile)
UNIXServer.open(sockfile) do |serv|
File.chmod(0777, sockfile)
serv.close_on_exec = false
fd = serv.fileno
3.times do
fork do
exec "ruby #{__FILE__} worker #{fd}", close_others: false
end
end
Process.waitall
end
when 'worker'
fd = ARGV[1].to_i
serv = UNIXServer.for_fd(fd)
loop do
sock = serv.accept
sleep 1
sock.write("hello #{Process.pid}")
sock.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment