Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How sleep eats CPU php [closed]

How is sleep using cpu resources?

Does it use 100% or 0% of cpu when you sleep your script. Can you explain?

As far as i know it increases load average, but how does it use CPU?

like image 293
pain.reign Avatar asked May 03 '13 12:05

pain.reign


People also ask

Does sleep consume CPU cycles?

Processes do not consume CPU resources while they are sleeping. They may add some overhead since the Kernel has to juggle them around, but that is very insignificant.

How does sleep work in PHP?

The sleep() function delays execution of the current script for a specified number of seconds. Note: This function throws an error if the specified number of seconds is negative.

Does sleep reduce CPU usage?

Yes, it is really true. Sleeping in a thread does reduce the CPU usage of that thread.

Does PHP wait for function to finish?

PHP is single-threaded, which means that it executes one instruction at a time before moving on. In other words, PHP naturally waits for a function to finish executing before it continues with the next statement.


1 Answers

The way that sleep works is OS-dependent, because the PHP function sleep calls into the appropriate runtime function to actually do what it says on the tin.

For Windows, that function is SleepEx. For other operating systems it is the POSIX function sleep. In both cases, the documentation for those functions clearly states that the sleeping thread is ineligible for running during the sleep period and therefore cannot consume CPU resources even if it wanted to.

like image 121
Jon Avatar answered Oct 20 '22 23:10

Jon