Skip to content

Instantly share code, notes, and snippets.

@buty4649
Created June 5, 2020 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buty4649/6b340e4837c89505b6d336c41b82d30e to your computer and use it in GitHub Desktop.
Save buty4649/6b340e4837c89505b6d336c41b82d30e to your computer and use it in GitHub Desktop.
def exec
pids = []
pid = Process.fork do
Exec.execve_override_procname(ENV.to_hash, "sleep", "/bin/sleep", "10")
end
pids << pid
puts "sleep(pid: #{pid})"
th = SignalThread.trap(:INT, {detailed:true}) do |info|
puts "SIGINT pid: #{pid} siginfo.uid:#{info.uid} siginfo.pid:#{info.pid}"
if info.pid.zero?
begin
puts Process.kill(:INT, *pids)
rescue => e
puts e
end
end
end
Process.wait2(pid)
th.kill(:INT)
th.cancel
end
SignalThread.trap(:KILL) do
puts "SIGKILL"
end
exec
exec
#Thread.usleep(5000000)
❯ mruby signal_test.rb
sleep(pid: 239527)
^CSIGINT pid: 239527 siginfo.uid:0 siginfo.pid:0 🌟 一回目のSIGINTは飛ぶ
No such process - no such process
SIGINT pid: 239527 siginfo.uid:899402263 siginfo.pid:239525 🌟 スレッドの終了
sleep(pid: 239532)
^CSIGINT pid: 239532 siginfo.uid:0 siginfo.pid:0 🌟 二回目以降はなぜかSIGINTが飛ばない?
1
^CSIGINT pid: 239532 siginfo.uid:0 siginfo.pid:0
1
^CSIGINT pid: 239532 siginfo.uid:0 siginfo.pid:0
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment