sleep() Function in C
Overview
sleep() is a function provided by the `unistd. he library in C that stalls the execution of a program (or a thread) by a specified number of seconds (or milliseconds).
Syntax of sleep() Function in C
The syntax for calling sleep() in C is as follows:
The above code will execute until the sample code part 1 ends, and then will pause its execution for number_of_seconds seconds, and then resume executing sample code part 2.
One other way of calling sleep is:
In most cases, t will have the value 0. But if the sleep state of the program is somehow interrupted by some signal in the system, then t will have the value of the time (in seconds) that the program had to wait for more.
For example, you set the sleep parameter to 10, so ideally, the program will wait for 10 seconds. But if somehow, it is interrupted at the 7th second, it will return a value of 3, indicating that though 3 more seconds had to be waited by the program, it has been terminated due to some external cause.
Parameters of sleep() Function in C
The sleep function in C takes only one parameter of the `unsigned integer type, that is, the number of seconds that the program has to pause.
If you pass a signed integer instead, it will be converted implicitly to unsigned integers. It won't be an issue in the case of positive numbers, but in the case of negative integers, it might get converted into an unwanted number (depending on the implementation).
If your system uses the two's complement method, -1 will be converted to 4294967295.
To avoid such unwanted conversions, only use the appropriate data type only while passing the parameters to the sleep function in C.
Return Value of sleep() Function in C
The sleep function returns an `unsigned int value.
- It returns a value of 0 when it can pause the program for the specified duration; that is, it does not return before its expected time.
- It returns a non-zero positive value t if it is forced to return before the intended num_seconds seconds (because of some external signals). The value returned is equal to the remaining time (in seconds) that the sleep() function was supposed to wait for (example given in the Syntax section)
What is Sleep Function in C and Why to use it?
Imagine you are writing a program that can play rock-paper-scissors with you. Now, if you write a simple program as shown below, then as soon as you execute it, the output will be shown instantly on the output console.
Just for the sake of adding a little dramatic effect (say, you want to count to three and say out loud "Rock - Paper - Scissor" like in an actual game), you might want your program to wait. But how do you do that?
In situations like these, we can use the sleep() function provided in the unistd.h header file.
sleep() pauses the execution of the program (or thread) in which it is called in by a specified number of seconds. Unless some signal (given by the operating system or another program) interrupts its execution, the sleep() function can pause the program's execution for the entire specified duration. Using the sleep() function, the above program will look like this:
The above program will wait for 5 seconds before displaying the output, and in that time, you can easily complete saying "Rock-Paper-Scissors".
Just observe that the difference between the two programs is just the sleep() statement and the inclusion of the `unistd. he header file. No other significant changes were required. Thus, the sleep function in C provides a simple and elegant way to pause the program execution.
Example of sleep() Function in C
Code:
Output:
Explanation:
- We use (int) time(NULL) to obtain the UNIX timestamp that provides us with the number of seconds that have elapsed since Jan 1, 1970, till now.
- Since we called the sleep() function for 5 seconds, we can see that the difference in the two timestamps (one called before and one after calling the sleep() function) is also 5 seconds only.
Code 2:
Output 2:
Explanation:
In this example, we saw that when the sleep() function returns after waiting for the entire specified number of seconds, it returns a zero value. Seeing the difference in the timestamps again, we can confirm that the executing program slept for 5 seconds.
Conclusion
- The sleep function in C allows the calling thread/program to pause its execution for a specified number of seconds.
- The sleep function in C takes a single parameter, an unsigned integer, that specifies the number of seconds you want the program to sleep.
- The sleep function in C returns a single value, an unsigned integer, that signifies the difference in the time the sleep was supposed to execute and the time it was returned because of external signals.