Class: Forkr

Inherits:
Object
  • Object
show all
Defined in:
lib/forkr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(forklet, num_kids = 1) ⇒ Forkr

Returns a new instance of Forkr

Parameters:

  • forklet (Object)

    the worker object

  • num_kids (Integer) (defaults to: 1)

    how many children to spawn


18
19
20
21
22
23
24
# File 'lib/forkr.rb', line 18

def initialize(forklet, num_kids = 1)
  @worker_client = forklet 
  @master_pid = $$
  @children = []
  @child_count = num_kids
  @in_shutdown = false
end

Instance Attribute Details

#child_countInteger (readonly)

The number of children I should maintain This can be adjusted up or down with the TTIN and TTOU signals.

Returns:

  • (Integer)

10
11
12
# File 'lib/forkr.rb', line 10

def child_count
  @child_count
end

#childrenArray<Fixnum> (readonly)

Child process pids.

Returns:

  • (Array<Fixnum>)

14
15
16
# File 'lib/forkr.rb', line 14

def children
  @children
end

#master_pidFixnum (readonly)

The PID of the Forkr master

Returns:

  • (Fixnum)

5
6
7
# File 'lib/forkr.rb', line 5

def master_pid
  @master_pid
end

Instance Method Details

#runnil

Start the master, and spawn workers

Returns:

  • (nil)

28
29
30
31
32
33
34
35
36
37
# File 'lib/forkr.rb', line 28

def run
  @inbound, @outbound = IO.pipe
  Signal.trap('CHLD') { dead_child }
  Signal.trap('INT') { interrupt }
  Signal.trap('TERM') { shutdown }
  Signal.trap('QUIT') { core_dump_quit }
  Signal.trap('TTIN') { add_worker }
  Signal.trap('TTOU') { remove_worker }
  master_loop
end