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

4

16

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

Tridimensional answered 4/10, 2010 at 9:2 Comment(1)
The processes is sleeping because they have nothing to do at the moment.Kitchenette
T
22

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.

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 not the case, it tends to indicate that you need more CPU horsepower.

Taam answered 4/10, 2010 at 9:33 Comment(0)
D
11

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()

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.

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

Discretion answered 4/10, 2010 at 12:9 Comment(0)
L
2

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

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.

Otherwise, it's fine.

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

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.

Leporide answered 4/10, 2010 at 9:52 Comment(0)
C
2

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

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.

Canister answered 14/9, 2013 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.