Linux 在 *nix 中,是什么导致了 top 命令中的“睡眠”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3853845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:34:01  来源:igfitidea点击:

In *nix, what causes "sleeping" in top command?

phplinuxunixprocesstop-command

提问by StackOverflowNewbie

What causes these sleepingprocesses that I see in top? If I were to call PHP's sleep()function, would that add to the sleepingcount I see in top? Are there any disadvantages to having a high number in sleeping?

是什么导致sleeping了我在 中看到的这些过程top?如果我要调用 PHP 的sleep()函数,这会增加sleeping我看到的计数top吗?有高的数字有什么坏处sleeping吗?

采纳答案by caf

A process is sleeping when it is blocked, waiting for something. For example, it might have called read()and is waiting on data to arrive from a network stream.

进程被阻塞时正在休眠,等待某事。例如,它可能已经调用read()并等待来自网络流的数据到达。

sleep()is indeed one way to have your process sleep for a while. Sleeping is, however, the normal state of all but heavily compute-bound processes - sleeping is essentially what a process does when it isn't doing anything else. It's the normal state of affairs for most of your processes to be sleeping - if that's notthe case, it tends to indicate that you need more CPU horsepower.

sleep()确实是让您的进程休眠一段时间的一种方法。然而,睡眠是除大量计算绑定进程之外的所有进程的正常状态 - 睡眠本质上是一个进程在不做任何其他事情时所做的事情。大多数进程处于休眠状态是正常的事态 - 如果不是这种情况,则往往表明您需要更多的 CPU 马力。

回答by MarkR

They are processes which aren't running on the CPU right now. This is not necessarily a bad thing.

它们是不是在CPU上运行的进程,现在。这不一定是坏事。

If you have huge numbers (10,000 on a server system, for example) of processes sleeping, the amount of memory etc used to keep track of them may make the system less efficient for non-sleeping processes.

如果您有大量(例如,服务器系统上有 10,000 个)进程处于休眠状态,那么用于跟踪它们的内存量等可能会使系统对非休眠进程的效率降低。

Otherwise, it's fine.

否则,没关系。

Most normal server systems have 100 to 1000 much of the time; this is not a big deal.

大多数普通服务器系统大部分时间都有 100 到 1000 个;这没什么大不了的。

Just because they're not doing anything just now doesn't mean they won't, very soon. Keeping them in memory, ready, reduces latency when they are required.

仅仅因为他们现在没有做任何事情并不意味着他们很快就会做。将它们保存在内存中,准备就绪,可以在需要时减少延迟。

回答by Aboelnour

A sleeping process is like suspended process. A process sleeps when:

休眠进程就像挂起的进程。进程在以下情况下休眠:

  1. It's doing an I/O operation (blocking for I/O)
  2. When you order it to sleep by sleep()
  1. 它正在执行 I/O 操作(阻塞 I/O)
  2. 当你通过 sleep() 命令它睡觉时

The status of any process can be:

任何进程的状态可以是:

  • Ready: when it ready for execution and it's in the queue waiting the processor call with specific priority
  • Sleeping: When it was running and it was blocked for I/O operation or when executing sleep()
  • Running: When the processor executes a process it becomes running.
  • 就绪:当它准备好执行并且在队列中等待具有特定优先级的处理器调用时
  • 休眠:当它正在运行并且被 I/O 操作阻塞或执行 sleep() 时
  • 运行:当处理器执行一个进程时,它开始运行。

Status Meaning

状态 含义

  • R Runnable

  • T Stopped

  • P Waiting on Pagein

  • D Waiting on I/O

  • S Sleeping < 20 seconds

  • I Idle - sleeping >20 seconds

  • Z Zombie or defunct

  • R 可运行

  • 停止

  • P 等待寻呼

  • D 等待 I/O

  • S 睡眠 < 20 秒

  • I 空闲 - 睡眠 >20 秒

  • Z 僵尸或不复存在

回答by Chris Dodd

To go into a bit more detail here, the Sstate means the process is waiting on a timer or a slow device, while the Dstate means it is waiting on a fast device.

在这里更详细一点,S状态意味着进程正在等待计时器或慢速设备,而D状态意味着它正在等待快速设备。

What constitutes a fast device vs a slow device is not terribly well defined, but generally, all serial, network, and terminal devices are slow devices, while disks are fast devices.

什么构成快速设备与慢速设备并没有很好的定义,但通常,所有串行、网络和终端设备都是慢速设备,而磁盘是快速设备。