On 05/11/2011 01:32 PM, Dan King wrote:
> I expected the script below to do the following:
>
> (1) spawn a new thread
> (2) connect to a server
> (3) read the data transmitted by the server
> (4) close the connection.
> (5) repeat 1-4
>
> While the script works, it does NOT spawn a new thread for each
> connection. Does anyone why that is the case? Thanks.
>
> require 'socket'
>
> threads = []
>
> 500.times do
>    threads<<  Thread.new do
>      socket = TCPSocket.open('127.0.0.1', 23)
>      pid = Process.pid
>      while line = socket.gets
>        print "Client thread (#{pid})\t" + line.chomp + "\n"
>      end
>      socket.close
>    end
> end
>
> threads.each { |t| t.join }
>

All the threads are running in one process, hence there is only one pid.