루비에서 UNIX 프로세스 제어 방법 사용
-
05-07-2019 - |
문제
Ryan Tomayko는 화재 폭풍을 쳤다 이 게시물 UNIX 프로세스 제어 명령 사용 정보.
우리는 더 많은 일을해야합니다. 더 많은 것. 나는 Fork (2), execve (2), Pipe (2), SocketPair (2), Select (2), Kill (2), Sigaction (2) 등에 대해 이야기하고 있습니다. 이들은 우리의 친구입니다. 그들은 우리를 돕기 위해 너무 심하게 원합니다.
나는 약간의 코드가 있습니다 (a delayed_job
이것에 맞는 DataMapper 용 클론이지만, 나열된 명령을 활용하는 방법은 명확하지 않습니다. 이 코드를 개선하는 방법에 대한 아이디어가 있습니까?
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
해결책
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.
However, if you write a server that accepts concurrent connections, now Ryan's advice is right on the money.
fork
: creates a copy of the original processexecve
: stops executing the current file and begins executing a new file in the same process (very useful in rake tasks)pipe
: creates a pipe (two file descriptors, one for read, one for write)socketpair
: like a pipe, but for socketsselect
: let's you wait for one or more of multiple file descriptors to be ready with a timeoutkill
: used to send a signal to a processsigaction
: lets you change what happens when a process receives a signal
다른 팁
5 months later, you can view my solution at http://github.com/antarestrader/Updater. Look at lib/updater/fork_worker.rb