From: Jeremy Hinegardner Date: 2007-09-08T16:48:26+09:00 Subject: Re: Why don't threads get killed in this case? Hi Chad, On Sat, Sep 08, 2007 at 11:30:04AM +0900, grocery_stocker wrote: > Here is the code that is running on OpenBSD 4.2 > > #!/usr/local/bin/ruby > > threads=[] > > 4.times do |i| > threads[i]=Thread.new do > %x{/usr/libexec/getty std.19200 tty00} > end > end > > threads.each{|thr| thr.join} > > I run it as a unix background job, I get the following > > -bash-3.2$ ./whore.rb & > [1] 11801 > -bash-3.2$ ps waux | grep djdoboy > djdoboy 15158 0.0 0.3 844 1540 pi Ss 9:41PM 0:00.05 - > bash > (bash) > djdoboy 11801 0.0 0.4 996 1872 pi S 10:03PM 0:00.01 > /usr/local/bin/ruby ./whore.rb > djdoboy 4493 0.0 0.1 372 556 pi S 10:03PM 0:00.02 > /usr/libexec/getty std.19200 tty00 > djdoboy 19020 0.0 0.1 332 556 pi S 10:03PM 0:00.01 > /usr/libexec/getty std.19200 tty00 > djdoboy 456 0.0 0.1 364 564 pi S 10:03PM 0:00.02 > /usr/libexec/getty std.19200 tty00 > djdoboy 11853 0.0 0.1 260 584 pi S 10:03PM 0:00.01 > /usr/libexec/getty std.19200 tty00 > djdoboy 23871 0.0 0.1 532 408 pi R+ 10:03PM 0:00.00 ps > -waux > djdoboy 27426 0.0 0.1 220 524 pi S+ 10:03PM 0:00.00 grep > djdoboy > > Now I get and relogin > -bash-3.2$ exit > logout > Connection closed by foreign host. > [cdalten@localhost ~]$ telnet > > -bash-3.2$ ps waux | grep djdoboy > djdoboy 6724 0.0 0.3 1012 1548 p6 Ss 10:04PM 0:00.02 - > bash > (bash) > djdoboy 25303 0.0 0.1 576 448 p6 R+ 10:04PM 0:00.00 ps > -waux > djdoboy 8516 0.0 0.1 1012 408 p6 R+ 10:04PM 0:00.00 grep > djdoboy (bash) > djdoboy 11801 0.0 0.4 996 1872 pi- S 10:03PM 0:00.01 > /usr/local/bin/ruby ./whore.rb > djdoboy 11853 0.0 0.1 260 584 pi- I 10:03PM 0:00.01 > /usr/libexec/getty std.19200 tty00 > djdoboy 456 0.0 0.1 364 564 pi- I 10:03PM 0:00.02 > /usr/libexec/getty std.19200 tty00 > djdoboy 4493 0.0 0.1 372 556 pi- I 10:03PM 0:00.02 > /usr/libexec/getty std.19200 tty00 > djdoboy 19020 0.0 0.1 332 556 pi- I 10:03PM 0:00.01 > /usr/libexec/getty std.19200 tty00 > > And the threads are still there. How come ruby threads don't get > killed > on exit? And more to the point, how do I kill them on exit? > This is not a ruby isssue, this is related to your shell options. % uname -rs OpenBSD 4.0 % find ./ -type f > /dev/null & [1] 6962 % exit # log back in % ps -p 6962 PID TT STAT TIME COMMAND 6962 p4 D 0:00.95 find ./ -type f % kill 6962 % shopt huponexit huponexit off man bash: If the huponexit shell option has been set with shopt, bash sends a SIGHUP to all jobs when an interactive login shell exits. So lets try it. % shopt -s huponexit % find ./ -type f > /dev/null & [1] 29709 % ps -p 29709 PID TT STAT TIME COMMAND 29709 p4 D 0:01.65 find ./ -type f % exit # log back in % ps -p 29709 PID TT STAT TIME COMMAND % enjoy, -jeremy -- ======================================================================== Jeremy Hinegardner jeremy@hinegardner.org