# File lib/rbg.rb, line 26
    def start_parent
      # Record the PID of this parent in the Master
      parent_pid = fork do
        # Clear the child process list as this fork doesn't have any children yet
        self.child_processes = Hash.new

        # Set the process name (Parent)
        $0="#{self.config.name}[Parent]"

        # Debug information
        logger.info "New parent process: #{Process.pid}"
        STDOUT.flush
        
        # Run the before_fork function
        if config.before_fork.is_a?(Proc)
          self.config.before_fork.call
        end
        
        # Fork an appropriate number of workers
        self.fork_workers(self.config.workers)
        
        # If we get a TERM, send the existing workers a TERM then exit
        Signal.trap("TERM", proc {
          # Debug output
          logger.info "Parent got a TERM."
          STDOUT.flush

          # Send TERM to workers
          kill_child_processes
          
          # Exit the parent
          Process.exit(0)
        })
        
        # Ending parent processes on INT is not useful or desirable
        # especially when running in the foreground
        Signal.trap('INT', proc {})
        Signal.trap('HUP', proc {})
        
        # Parent loop, the purpose of this is simply to do nothing until we get a signal
        # We will exit if all child processes die
        # We may add memory management code here in the future
        loop do
          sleep 2
          child_processes.dup.each do |id, opts|
            begin
              
              Process.getpgid(opts[:pid])
              
              if config.memory_limit
                # Lookup the memory usge for this PID
                memory_usage = `ps -o rss= -p #{opts[:pid]}`.strip.to_i / 1024
                if memory_usage > config.memory_limit
                  logger.info "#{self.config.name}[#{id}] is using #{memory_usage}MB of memory (limit: #{config.memory_limit}MB). It will be killed."
                  kill_child_process(id)
                end
              end
              
            rescue Errno::ESRCH
              logger.info "Child process #{config.name}[#{id}] has died (from PID #{opts[:pid]})"
              child_processes[id][:pid] = nil
              
              if config.respawn
                if opts[:started_at] > Time.now - config.respawn_limits[1]
                  if opts[:respawns] >= config.respawn_limits[0]
                    logger.info "Process #{config.name}[#{id}] has instantly respawned #{opts[:respawns]} times. It won't be respawned again."
                    child_processes.delete(id)
                  else
                    logger.info "Process has died within #{config.respawn_limits[1]}s of the last spawn."
                    child_processes[id][:respawns] += 1
                    fork_worker(id)
                  end
                else
                  logger.info "Process was started more than #{config.respawn_limits[1]}s since the last spawn. Resetting spawn counter"
                  child_processes[id][:respawns] = 0
                  fork_worker(id)
                end
              else
                child_processes.delete(id)
              end
            end
          end
          
          if child_processes.empty?
            logger.info "All child processes died, exiting parent"
            Process.exit(0)
          end
        end
      end
      
      # Store the PID for the parent
      child_processes[0] = {:pid => parent_pid, :respawns => 0}
      # Ensure the new parent is detached
      Process.detach(parent_pid)
    end