Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在线程ruby中杀死系统进程_Ruby_Multithreading - Fatal编程技术网

在线程ruby中杀死系统进程

在线程ruby中杀死系统进程,ruby,multithreading,Ruby,Multithreading,如何在ruby线程中终止ping(或其他最长的无超时等系统进程)(ping-这只是一个简单的示例): a = Thread.new do system 'ping localhost' end a.kill a.exit a.terminate while true sleep 5 p a.alive? end 输出:=> PING localhost.localdomain (127.0.0.1) 56(84) bytes of data. 64 bytes fr

如何在ruby线程中终止ping(或其他最长的无超时等系统进程)(ping-这只是一个简单的示例):

a = Thread.new do
    system 'ping localhost'
end

a.kill
a.exit
a.terminate

while true
    sleep 5
    p a.alive?
end
输出:=>

PING localhost.localdomain (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost.localdomain (127.0.0.1): icmp_req=1 ttl=64 time=0.023 ms
....
true
64 bytes from localhost.localdomain (127.0.0.1): icmp_req=7 ttl=64 time=0.022 ms
.....
true
......
所以我需要用线程停止ping进程,但我不知道怎么做

不会为您提供
pid

改用。并使用
process::spawn
返回的pid终止进程

例如:

pid = Process.spawn('ping localhost')
sleep 3
Process.kill(:TERM, pid)
Process.wait(pid)

你可以看看宝石