Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C02RP0G1G8WM:pipe noah.gibbs$ diff -c thread_test.rb fork_test.rb
*** thread_test.rb 2019-08-30 19:04:45.000000000 -0700
--- fork_test.rb 2019-08-30 16:15:08.000000000 -0700
***************
*** 8,14 ****
RESPONSE_TEXT = "OK".freeze
if ARGV.size != 2
! STDERR.puts "Usage: ./thread_test <num_workers> <num_requests>"
exit 1
end
--- 8,14 ----
RESPONSE_TEXT = "OK".freeze
if ARGV.size != 2
! STDERR.puts "Usage: ./fork_test <num_workers> <num_requests>"
exit 1
end
***************
*** 39,47 ****
readable_idx_for[r] = i
end
! puts "Setting up threads..."
NUM_WORKERS.times do |i|
! t = Thread.new do
# Worker code
NUM_REQUESTS.times do |req_num|
q = worker_read[i].read(QUERY_TEXT.size)
--- 39,47 ----
readable_idx_for[r] = i
end
! puts "Setting up processes..."
NUM_WORKERS.times do |i|
! pid = fork do
# Worker code
NUM_REQUESTS.times do |req_num|
q = worker_read[i].read(QUERY_TEXT.size)
***************
*** 51,57 ****
worker_write[i].print(RESPONSE_TEXT)
end
end
! workers.push t
end
### Master code ###
--- 51,57 ----
worker_write[i].print(RESPONSE_TEXT)
end
end
! workers.push pid
end
### Master code ###
***************
*** 98,104 ****
end
puts "Done, waiting for workers..."
! workers.each { |t| t.join }
puts "Done."
if pending_write_msgs.any? { |p| p != 0 } || pending_read_msgs.any? { |p| p != 0}
--- 98,104 ----
end
puts "Done, waiting for workers..."
! workers.each { |pid| Process.waitpid(pid) }
puts "Done."
if pending_write_msgs.any? { |p| p != 0 } || pending_read_msgs.any? { |p| p != 0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.