種類
- インスタンスメソッド (95)
- モジュール関数 (88)
- クラス (22)
ライブラリ
- ビルトイン (205)
クラス
- File (11)
- IO (22)
- SignalException (11)
- Thread (22)
-
Thread
:: ConditionVariable (18) - ThreadGroup (11)
モジュール
- Kernel (88)
検索結果
先頭5件
-
Kernel
. # sleep -> Integer (26144.0) -
sec 秒だけプログラムの実行を停止します。
...スレッドからの Thread#run
などで明示的に起こさない限り永久にスリープします。Thread#runを呼ぶとその時点で
sleepの実行が中断されます。
@param sec 停止する秒数を非負の数値で指定します。浮動小数点数も指定できます。......省略された場合、永久にスリープします。
@return 実際に停止していた秒数 (整数に丸められた値) です。
//emlist[例][ruby]{
it = Thread.new do
sleep
puts 'it_end'
end
re = sleep 2.11
puts re
it.run
re2 = sleep 0.76
puts re2
#=> 2
# it_end
# 1
//}... -
Kernel
. # sleep(sec) -> Integer (26144.0) -
sec 秒だけプログラムの実行を停止します。
...スレッドからの Thread#run
などで明示的に起こさない限り永久にスリープします。Thread#runを呼ぶとその時点で
sleepの実行が中断されます。
@param sec 停止する秒数を非負の数値で指定します。浮動小数点数も指定できます。......省略された場合、永久にスリープします。
@return 実際に停止していた秒数 (整数に丸められた値) です。
//emlist[例][ruby]{
it = Thread.new do
sleep
puts 'it_end'
end
re = sleep 2.11
puts re
it.run
re2 = sleep 0.76
puts re2
#=> 2
# it_end
# 1
//}... -
Process
:: Status (8096.0) -
プロセスの終了ステータスを表すクラスです。 メソッド Process.#wait2 などの返り値として使われます。
...exited normally. status=#{$?.exitstatus}"
else
p "unknown status %#x" % $?.to_i
end
SIGCHLD を trap する例
trap(:SIGCHLD) {|sig|
puts "interrupted by signal #{sig} at #{caller[1]}"
# 複数の子プロセスの終了に対して1つの SIGCHLD しか届かない
# 場......signaled?
puts " child #{$?.pid} was killed by signal #{$?.termsig}"
if $?.coredump?
puts " child #{$?.pid} dumped core."
end
when $?.stopped?
puts " child #{$?.pid} was stopped by signal #{$?.stopsig}"
when $?.exited?
puts " chil......p pid1 = fork { sleep 1; exit }
p pid2 = fork { loop { sleep } } # signal を待つための sleep
begin
Process.kill :STOP, pid2
sleep # SIGCHLD を待つための sleep
Process.kill :CONT, pid2
Process.kill :TERM, pid2
loop { sleep } #... -
File
# flock(operation) -> 0 | false (8054.0) -
ファイルをロックします。
...
puts "locked by process1"
fork {
f = File.open("/tmp/foo", "r")
f.flock(File::LOCK_SH)
puts "locked by process2"
sleep 5
puts "unlocked by process2"
}
sleep 5
f.flock(File::LOCK_UN)
puts "unlocked by process1"
sleep 1 # <- 子プロセスが確実に先にロックするための sleep......f.flock(File::LOCK_EX)
puts "re-locked by process1"
# => locked by process1
# unlocked by process1
# locked by process2
# unlocked by process2
# re-locked by process1
//}... -
ThreadGroup
# add(thread) -> self (8042.0) -
スレッド thread が属するグループを自身に変更します。
...uby]{
puts "Initial group is #{ThreadGroup::Default.list}"
# => Initial group is [#<Thread:0x4a49168 run>]
tg = ThreadGroup.new
t1 = Thread.new { sleep }
t2 = Thread.new { sleep }
puts "t1 is #{t1}" # => t1 is #<Thread:0x50bef60>
puts "t2 is #{t2}" # => t2 is #<Thread:0x50beed0>
tg.add(t1)
puts "In......itial group now #{ThreadGroup::Default.list}"
# => Initial group now [#<Thread:0x3039168 run>, #<Thread:0x50beed0 run>]
puts "tg group now #{tg.list}"
# => tg group now [#<Thread:0x50bef60 run>]
//}... -
Kernel
. # fork -> Integer | nil (8036.0) -
fork(2) システムコールを使ってプロセスの複製を作 ります。親プロセスでは子プロセスのプロセスIDを、子プロセスでは nil を返します。ブロックを指定して呼び出した場合には、生成し た子プロセスでブロックを評価します。
...y]{
if child_pid = fork
puts "parent process. pid: #{Process.pid}, child pid: #{child_pid}"
# => parent process. pid: 81060, child pid: 81329
# 親プロセスでの処理
# ...
# 子プロセスの終了を待って終了。
Process.waitpid(child_pid)
else
puts "child process. pid:......81329
# 子プロセスでの処理
sleep(1)
end
//}
//emlist[ブロックを指定した場合][ruby]{
child_pid = fork do
puts "child process. pid: #{Process.pid}"
# => child process. pid: 79602
# 子プロセスでの処理
sleep(1)
end
puts "parent process. pid: #{Process.pid},... -
Kernel
. # fork { . . . } -> Integer | nil (8036.0) -
fork(2) システムコールを使ってプロセスの複製を作 ります。親プロセスでは子プロセスのプロセスIDを、子プロセスでは nil を返します。ブロックを指定して呼び出した場合には、生成し た子プロセスでブロックを評価します。
...y]{
if child_pid = fork
puts "parent process. pid: #{Process.pid}, child pid: #{child_pid}"
# => parent process. pid: 81060, child pid: 81329
# 親プロセスでの処理
# ...
# 子プロセスの終了を待って終了。
Process.waitpid(child_pid)
else
puts "child process. pid:......81329
# 子プロセスでの処理
sleep(1)
end
//}
//emlist[ブロックを指定した場合][ruby]{
child_pid = fork do
puts "child process. pid: #{Process.pid}"
# => child process. pid: 79602
# 子プロセスでの処理
sleep(1)
end
puts "parent process. pid: #{Process.pid},... -
Thread
# run -> self (8030.0) -
停止状態(stop)のスレッドを再開させます。 Thread#wakeup と異なりすぐにスレッドの切り替え を行います。
...。
@raise ThreadError 死んでいるスレッドに対して実行すると発生します。
//emlist[例][ruby]{
a = Thread.new { puts "a"; Thread.stop; puts "c" }
sleep 0.1 while a.status!='sleep'
puts "Got here"
a.run
a.join
# => a
# => Got here
# => c
//}
@see Thread#wakeup, Thread.stop... -
Kernel
. # exec(env , program , *args , options={}) -> () (8026.0) -
引数で指定されたコマンドを実行します。
...t[例][ruby]{
# a.rb
exec ['sleep', 'mysleep'], '600'
//}
上記スクリプトを実行すると以下のようになります。
$ ruby a.rb
## sleep してるので制御が戻ってこない。別の仮想端末に切替えて以下を実行
$ ps aux|grep sleep
xxxx 32754 0.0 0.0......2580 468 pts/3 S+ 22:01 0:00 mysleep 600
xxxx 32761 0.0 0.0 2824 792 pts/6 S+ 22:01 0:00 grep sleep
@see Kernel.#system,Kernel.#`,Kernel.#spawn,Kernel.#fork,IO.popen,IO.pipe,Kernel.#open,exec(3)... -
Kernel
. # exec(program , *args , options={}) -> () (8026.0) -
引数で指定されたコマンドを実行します。
...t[例][ruby]{
# a.rb
exec ['sleep', 'mysleep'], '600'
//}
上記スクリプトを実行すると以下のようになります。
$ ruby a.rb
## sleep してるので制御が戻ってこない。別の仮想端末に切替えて以下を実行
$ ps aux|grep sleep
xxxx 32754 0.0 0.0......2580 468 pts/3 S+ 22:01 0:00 mysleep 600
xxxx 32761 0.0 0.0 2824 792 pts/6 S+ 22:01 0:00 grep sleep
@see Kernel.#system,Kernel.#`,Kernel.#spawn,Kernel.#fork,IO.popen,IO.pipe,Kernel.#open,exec(3)...