在while循环中睡眠会得到自己的pid

在while循环中睡眠会得到自己的pid

本文介绍了在while循环中睡眠会得到自己的pid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,可以在循环中进行一些并行处理.我不想让并行进程占用CPU,因此我使用了sleep命令.这是简化版.

I have a bash script that does some parallel processing in a loop. I don't want the parallel process to spike the CPU, so I use a sleep command. Here's a simplified version.


(while true;do sleep 99999;done)&

因此,我从bash提示符执行上面的行,并得到类似以下内容的内容: [1] 12345

So I execute the above line from a bash prompt and get something like:[1] 12345

其中 [1] 是作业编号,而 12345 是while循环的进程ID(pid).我做了 kill 12345 并得到:

Where [1] is the job number and 12345 is the process ID (pid) of the while loop. I do a kill 12345 and get:


[1]+  Terminated              ( while true; do
    sleep 99999;
done )

整个脚本似乎已终止.但是,我进行了 ps aux | grep sleep ,发现sleep命令仍然很强大,但是具有自己的pid!我可以杀死 sleep ,一切似乎都很好.但是,如果我要先杀死睡眠,则while循环会启动一个新的 sleep pid.这对我来说是一个惊喜,因为睡眠与while循环不平行.循环本身是一条执行路径.

It looks like the entire script was terminated. However, I do a ps aux|grep sleep and find the sleep command is still going strong but with its own pid! I can kill the sleep and everything seems fine. However, if I were to kill the sleep first, the while loop starts a new sleep pid. This is such a surprise to me since the sleep is not parallel to the while loop. The loop itself is a single path of execution.

所以我有两个问题:

  1. sleep命令为什么得到自己的进程ID?
  2. 我如何轻松地杀死while循环和睡眠?

推荐答案

  1. Sleep拥有自己的 PID ,因为它是一个正在运行且仅在等待的进程.尝试哪个睡觉看看它在哪里.
  2. 您可以使用 ps -uf 来查看系统上的进程树.从那里,您可以确定睡眠的外壳(运行循环的一个)的 PPID (父PID)是什么.
  1. Sleep gets its own PID because it is a process running and just waiting. Try which sleep to see where it is.
  2. You can use ps -uf to see the process tree on your system. From there you can determine what the PPID (parent PID) of the shell (the one running the loop) of the sleep is.

这篇关于在while循环中睡眠会得到自己的pid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:18