For example, if we are developing a console application where we need to inform the user of the tasks that will be executed in sequence, the information messages should persist for a reasonable time so that the user has time to read them before they are cleared and the program moves on to the next command.
In this Linuxhint article, you’ll learn how to use the sleep() function to create delays in real time. We’ll show you the syntax and description of this function, as well as the options that POSIX provides to create delays with fractions of less than a second. Then, using practical examples, codes, and pictures, we’ll show you how to delay the execution of a program and the effects of signals on this function.
Syntax of the Sleep() Function in C Language
Description of the Sleep() Function in C Language
The sleep() function puts the process or thread to sleep for the time in seconds that is specified in the “sec” input argument which is an unsigned integer. Once the sleep() function is called, the calling process sleeps until it times out or receives a signal.
This function is often used to enforce long delays of more than 1 second in the execution of real-time processes. For delays of less than 1 second, POSIX provides the microsecond resolution function, usleep(), which uses the same method call as sleep(). For the delays of less than 1 microsecond, there is also the nanosleep() function with a resolution of 1 nanosecond, but with a different call method where it uses the “timespec” structures as input arguments to set the delay time.
If the sleep() function has consumed all of the specified time, it returns 0 as the result. If the execution is interrupted by the arrival of a signal before the specified time has elapsed, it returns the remaining number of seconds until that time.
The sleep() function is defined in the “unistd.h” header. To use it, we need to include this file in the code as follows:
How to Introduce Delays in a Process with the Sleep() Function
In this example, we create a timer that consists of an infinite loop in which we print the “Elapsed time” message in the command console, followed by the elapsed seconds of the process. Each of these loops is repeated every 2 seconds due to the delay that is caused by the sleep() function.
To do this, we take an empty file with the “.c” extension and add the “stdio.h” and “unistd.h” headers in it. Then, we open an empty main() function and define in it the variable seconds of type int which we’ll use as a counter for the elapsed time.
Once the headers are inserted and the variable is declared, we open an infinite loop and use the printf() function in it to display the message and the time value. On the next line, we increment the time variable by 2 and then call the sleep() function with the value of 2 as the input argument. In this way, this cycle is repeated every second and we get a counter that displays the elapsed time on the screen. Now, let’s take a look at the code for this application. Let’s see the complete code for this example:
#include <unistd.h>
void main ()
{
int seconds =0;
while (1)
{
printf("Elapsed time: %i\n", seconds);
seconds +=2;
sleep(2);
}
}
In the following, we will see a picture with the compilation and execution of this code. As we can see, every 2 seconds, the program prints the elapsed seconds on the screen since the execution of the process.
Effect of Signals on the Sleep() Function
In this example, we want to observe the effect of signals on a process that is put to sleep using the sleep() function. To do this, we create a simple application that consists of a main() function and a handler for signal 36.
In the first line of the main() function, we declare the remaining variable of type int where we store the value that is returned by the sleep() function. Then, we use the signal() function to bind the handler to signal 36. On the next line, we display the PID of the process which we then use to send a signal from a second shell to the process. Finally, we call the sleep() function and set its input argument to 60 seconds, long enough to send a signal from a second shell. We send the remaining variable as the output argument to sleep().
The handler that is attached to signal 36 consists of a line of code where the printf() function prints the “Time remaining:” message followed by the value that is returned by sleep() at the time the signal arrives at the process. Here, let’s look at the code for this example.
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
void handler ( int remaining);
void main ()
{
int remaining;
signal (36, handler);
printf("Process ID: %i\n", getpid());
remaining = sleep(60);
}
void handler (int remaining)
{
printf("Time remaining: %i\n", remaining);
}
The following image that we see shows the compilation and execution of this code:
To see the effect of the signals in this process, we compile this code and run it. Then, from a second terminal, we send a signal with the following syntax:
The following image that we see shows the execution of the code in the previous console and the effects of the arrival of a signal that is sent from the following console. As you can see, the signal has suppressed the effect of the sleep() function by waking up the process:
Conclusion
In this Linuxhint article, we showed you how to use the sleep() function to put a process to sleep for a specified number of seconds. We also showed you the syntax as well as the description of the function and the calling method.
Using practical examples, code snippets, and pictures, we showed you how to put a process to sleep and what affects the arrival of a signal has on a sleeping process using the sleep() function.