test_thread_sched_pipe.rb

Eric Wong, 07/08/2014 08:37 PM

Download (1.01 KB)

 
1
#puts "definindo taskset -c -p 1 #{Process.pid}"
2
system("taskset -c -p 2 #{Process.pid}")
3

    
4
inicio_geral = Time.now
5
t1 = Thread.new do
6
  Thread.current[:target] = 0
7
  while true
8
    Thread.current[:target] += 1
9
  end
10
end
11

    
12
10.downto(0) do |i|
13
  inicio = Time.now
14
  Kernel.sleep(1)
15
  puts "faltando #{i} segundos (#{Time.now - inicio})"
16
end
17

    
18
puts "zerando thread com contagem em #{t1[:target]}"
19
t1[:target] = 0
20

    
21
t2 = Thread.new do
22
  Thread.current[:target] = 0
23
  rd, wr = IO.pipe
24
  buf = "."
25
  while true
26
    wr.syswrite(buf)
27
    rd.readpartial(1, buf)
28
    Thread.current[:target] += 1
29
  end
30
end
31

    
32
t3 = Thread.new do
33
  Thread.current[:target] = 0
34
  while true
35
    Thread.current[:target] += 1
36
  end
37
end
38
10.times do
39
    puts "first #{t1[:target]}"
40
    puts "second #{t2[:target]}"
41
    puts "third #{t3[:target]}"
42
    puts "\n\n"
43
    sleep(1)
44
end
45
total = t1[:target].to_i + t2[:target].to_i + t3[:target].to_i
46
puts "total #{total}"
47
puts "media #{total/(Time.now - inicio_geral).to_i}"
48
puts "\n\n"
49
puts "time #{Time.now - inicio_geral}"
50