From Green software

Jump to: navigation, search

A computer program (process, task, or thread) may sleep, which places it into an inactive state for a period of time. Eventually the expiration of an interval timer, or the receipt of a signal or interrupt causes the program to resume execution.

When sleep is properly used, a CPU can reach a high utilization which is good for energy efficiency.

Putting applications in a state of sleep is already common practice in software for mobile platforms.


Low Level Functionality

Sleep causes the thread or process to enter the Not Runnable state. This allows the CPU to suspend the thread or process and continue executing other threads or processes until the sleep has finished, and the thread or process is allowed to continue executing. The sleep system call is non-interruptible, which differs from the Wait system call, which can be interrupted. Wait is mostly used in multi-threaded platforms.


A typical sleep system call takes a time value as a parameter, specifying the minimum amount of time that the process is to sleep before resuming execution. The parameter typically specifies seconds, although some operating systems provide finer resolution, such as milliseconds or microseconds.


On Windows 2000 and newer, the Sleep() function takes a single parameter of the number of milliseconds to sleep.

The sleep() function is included in the kernel32.dll, but no sleep command (executable) is natively available for scripts (batch files). It can be found in collections of Windows utilities like Windows 2003 Resource Kit.


On Unix-like operating systems, the sleep() function is called providing a single parameter of type unsigned integer of the number of seconds to sleep.

C/C++ Examples

In Windows OS:

   while (myInt <= 100)
       Sleep(2*1000);  // Sleep for 2 seconds

In Unix:

       sleep(2);  // Sleep for 2 seconds
   } while (externalVar > 10);


In Energy efficient software, this practice was empirically evaluated on the Apache Webserver. The application of the practice caused an 8% decrease in energy consumption.


batch file sleep command
UNIX Man Page - SLEEP(3)</ref> (For more precise sleep times one can use the usleep() function.)
UNIX Man Page - usleep(3)
Sleep vs. Wait (1)
Sleep vs. Wait (2)
MSDN Library Reference - Sleep()
The Science of Sleep (for Windows 7). 2010. Microsoft Corporation.


This best practice was recognized as such by IT professionals, described in Energy efficient software.

Personal tools