Ruby/Development/Process

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия: Импорт выборки материалов по Ruby)
 

Текущая версия на 17:55, 13 сентября 2010

Содержание

Daemon Processes

def daemonize
  fork do
    Process.setsid
    exit if fork
    Dir.chdir("/")
    STDIN.reopen("/dev/null")
    STDOUT.reopen("/dev/null", "a")
    STDERR.reopen("/dev/null", "a")
    trap("TERM") { exit }
    yield
  end
end
daemonize do
  # You can do whatever you like in here and it will run in the background
 
end
puts "The daemon process has been launched!"



Process.pid and Process.ppid

proc1 = Process.pid
fork do
  if Process.ppid == proc1
    puts "proc1 is my parent"  # Prints this message
  else
    puts "What"s going on?"
  end
end



Use Process.kill

Process.kill(1,pid1)         # Send signal 1 to process pid1
Process.kill("HUP",pid2)     # Send SIGHUP to pid2
Process.kill("SIGHUP",pid2)  # Send SIGHUP to pid3
Process.kill("SIGHUP",0)     # Send SIGHUP to self
trap(1) { puts "Caught signal 1" }
sleep 2
Process.kill(1,0)  # Send to self



use Process.wait to wait for all child processes to finish before continuing.

child = fork do
  sleep 3
  puts "Child says "hi"!"
end
puts "Waiting for the child process..."
Process.wait child
puts "All done!"