在Ruby中使用Unix進程控制方法

[英]Using Unix Process Control Methods in Ruby


Ryan Tomayko touched off quite a fire storm with this post about using Unix process control commands.

Ryan Tomayko用這篇關於使用Unix進程控制命令的文章引發了一場火災風暴。

We should be doing more of this. A lot more of this. I'm talking about fork(2), execve(2), pipe(2), socketpair(2), select(2), kill(2), sigaction(2), and so on and so forth. These are our friends. They want so badly just to help us.

我們應該做更多這方面的事情。更多的這一點。我在談論fork(2),execve(2),pipe(2),socketpair(2),select(2),kill(2),sigaction(2)等等。這些是我們的朋友。他們非常想要幫助我們。

I have a bit of code (a delayed_job clone for DataMapper that I think would fit right in with this, but I'm not clear on how to take advantage of the listed commands. Any Ideas on how to improve this code?

我有一些代碼(我認為DataMapper的delayed_job克隆適合於此,但我不清楚如何利用列出的命令。有關如何改進此代碼的任何想法?

def start
  say "*** Starting job worker #{@name}"
  t = Thread.new do
    loop do
      delay = Update.work_off(self)
      break if $exit
      sleep delay
      break if $exit
    end
    clear_locks
  end

  trap('TERM') { terminate_with t }
  trap('INT')  { terminate_with t }

  trap('USR1') do
    say "Wakeup Signal Caught"
    t.run
  end
end

2 个解决方案

#1


3  

Ahh yes... the dangers of "We should do more of this" without explaining what each of those do and in what circumstances you'd use them. For something like delayed_job you may even be using fork without knowing that you're using fork. That said, it really doesn't matter. Ryan was talking about using fork for preforking servers. delayed_job would use fork for turning a process into a daemon. Same system call, different purposes. Running delayed_job in the foreground (without fork) vs in the background (with fork) will result in a negligible performance difference.

啊,是的......“我們應該做更多的事情”的危險,而不解釋每個人做了什么,以及你在什么情況下使用它們。對於像delayed_job這樣的東西,你甚至可能在不知道你正在使用fork的情況下使用fork。那就是說,這沒關系。 Ryan談論使用fork來預備服務器。 delayed_job將使用fork將進程轉換為守護進程。相同的系統調用,不同的目的。在前台(沒有fork)和后台(使用fork)中運行delayed_job將導致可忽略的性能差異。

However, if you write a server that accepts concurrent connections, now Ryan's advice is right on the money.

但是,如果你編寫一個接受並發連接的服務器,那么現在Ryan的建議是正確的。

  • fork: creates a copy of the original process
  • fork:創建原始進程的副本
  • execve: stops executing the current file and begins executing a new file in the same process (very useful in rake tasks)
  • execve:停止執行當前文件並開始在同一進程中執行新文件(在rake任務中非常有用)
  • pipe: creates a pipe (two file descriptors, one for read, one for write)
  • pipe:創建一個管道(兩個文件描述符,一個用於讀取,一個用於寫入)
  • socketpair: like a pipe, but for sockets
  • socketpair:像管道一樣,但是用於套接字
  • select: let's you wait for one or more of multiple file descriptors to be ready with a timeout
  • select:讓你等待多個文件描述符中的一個或多個准備好超時
  • kill: used to send a signal to a process
  • kill:用於向進程發送信號
  • sigaction: lets you change what happens when a process receives a signal
  • sigaction:允許您更改進程收到信號時發生的情況

#2


1  

5 months later, you can view my solution at http://github.com/antarestrader/Updater. Look at lib/updater/fork_worker.rb

5個月后,您可以在http://github.com/antarestrader/Updater上查看我的解決方案。查看lib / updater / fork_worker.rb


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/10/22/720d97693801be1e2f013d03b7200948.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com