• Uncategorized

About ruby : Ruby-strange-forked-processes-behaviour-on-MacOS-vs-Debian

Question Detail

Using Ruby (tested with versions 2.6.9, 2.7.5, 3.0.3, 3.1.1) and forking processes to handle socket communication there seems to be a huge difference between MacOS OSX and a Debian Linux.

While running on Debian, the forked processes get called in a balanced manner – that mean: if having 10 tcp server forks and running 100 client calls, each fork will get 10 calls. The order of the pid call stack is also always the same even not ordered by pid (caused by load when instantiating the forks).

Doing the same on a MacOS OSX (Catalina) the forked processes will not get called balanced – that mean: “pid A” might get called 23 or whatever times while e.g. “pid G” was never used.

Sample code (originally from: https://relaxdiego.com/2017/02/load-balancing-sockets.html)

#!/usr/bin/env ruby

# server.rb

require 'socket'

# Open a socket
socket = TCPServer.open('0.0.0.0', 9999)
puts "Server started ..."

# For keeping track of children pids
wpids = []

# Forward any relevant signals to the child processes.
[:INT, :QUIT].each do |signal|
  Signal.trap(signal) {
    wpids.each { |wpid| Process.kill(:KILL, wpid) }
  }
end

5.times {
  wpids << fork do
    loop {
      connection = socket.accept
      connection.puts "Hello from #{ Process.pid }"
      connection.close
    }
  end
}

Process.waitall

Run some netcat to the server on a second terminal:

for i in {1..20}; do nc -d localhost 9999; done

As said: if running on Linux each forked process will get 4 calls – doing same on MacOS OSX its a random usage per forked process.

Any solution or correction to make it work on MacOS OSX in a balanced manner also?

Question Answer

No answer for now.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.