25. Fallacies
The network is reliable
Latency is zero
Bandwidth is infinite
The network is secure
Topology doesn't change
There is one administrator
Transport cost is zero
The network is homogeneous
74. # http://bogomips.org/ruby_posix_mq/
require 'posix_mq'
class Producer
attr_reader :mq
def initialize(mq_name)
@mq = POSIX_MQ.new("/foo", :rw)
end
def send(message, prio = 0)
puts "Send: #{message}. Priority #{prio}"
mq.send("#{message} #{prio}", prio)
end
end
p = Producer.new("/test_ipc")
p.send("Hello from #{Process.pid}", 10)
p.send("Hello from #{Process.pid}", 2)
p.send("Hello from #{Process.pid}", 0)
p.send("Hello from #{Process.pid}", 1)
p.send("Hello from #{Process.pid}", 20)
# ruby posix_mq/producer.rb
#
# Send: Hello from 12635. Priority 10
# Send: Hello from 12635. Priority 2
# Send: Hello from 12635. Priority 0
# Send: Hello from 12635. Priority 1
# Send: Hello from 12635. Priority 20
75. # http://bogomips.org/ruby_posix_mq/
require 'posix_mq'
class Consumer
attr_reader :mq
def initialize(mq_name)
@mq = POSIX_MQ.new("/foo", :rw)
end
def receive
mq.receive.first
end
def receive_non_block
mq.nonblock = true
begin
receive
rescue Errno::EAGAIN
mq.nonblock = false
puts "Nothing"
end
end
def shift
mq.tryshift
end
end
c = Consumer.new("/test_ipc")
while m = c.shift
puts "got: #{m}"
end
# ruby posix_mq/consumer.rb
# got: Hello from 12635 10
# got: Hello from 12635 20
# got: Hello from 12635 2
# got: Hello from 12635 1
# got: Hello from 12635 0
76. # https://github.com/pmahoney/process_shared
require 'process_shared'
mutex = ProcessShared::Mutex.new
mem = ProcessShared::SharedMemory.new(:int)
mem.put_int(0, 0)
pid1 = fork do
puts "in process 1 (#{Process.pid})"
10.times do
sleep 0.01
mutex.synchronize do
value = mem.get_int(0)
sleep 0.01
puts "process 1 (#{Process.pid}) incrementing"
mem.put_int(0, value + 1)
end
end
end
pid2 = fork do
puts "in process 2 (#{Process.pid})"
10.times do
sleep 0.01
mutex.synchronize do
value = mem.get_int(0)
sleep 0.01
puts "process 2 (#{Process.pid}) decrementing"
mem.put_int(0, value - 1)
end
end
end
Process.wait(pid1)
Process.wait(pid2)
# ruby shm.rb
#
# in process 1 (8038)
# in process 2 (8041)
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# process 1 (8038) incrementing
# process 2 (8041) decrementing
# value should be zero: 0
77. rd_child, wr_parent = IO.pipe
rd_parent, wr_child = IO.pipe
pid = fork do
rd_parent.close
wr_parent.close
wr_child.puts "sent from child process"
puts rd_child.gets
end
rd_child.close
wr_child.close
wr_parent.write "sent from parent process"
puts rd_parent.gets
# ruby pipes/pipes.rb
#
# sent from child process
# sent from parent process
78. require 'eventmachine'
module UnixServer
def receive_data(data)
puts data
EM.stop if data.chomp == "exit"
send_data("Server #{Process.pid}: Got #{data.chomp} from you")
close_connection_after_writing
end
end
EM.run do
puts "Started UNIX socket server on /tmp/sock"
EM::start_unix_domain_server("/tmp/sock", UnixServer)
end
# ruby server.rb
#
# Started UNIX socket server on /tmp/sock
#
# HELLO! My pid is 13847