| by Arround The Web | No comments

Sleep() Function in C Language

When we develop the programs that require a user interaction, there are cases where it is necessary to adjust the system times so that the application is readable and easy to use. For example, if we are developing a console application where we need to inform the user of the tasks that are 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, code, 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:

unsigned int sleep ( unsigned int sec )

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 the delays of less than 1 second, POSIX provides the microsecond resolution the usleep() function 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:

#include <unistd.h>

How to Introduce Delays in a Process with the Sleep() Function in C Language

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 “seconds” variable of type int which we 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. In the following, we can see the complete code for this example:

#include <stdio.h>
#include <unistd.h>

void main ()
{
int seconds =0;
while (1)
       {
       printf("Elapsed time: %i\n", seconds);
       seconds +=2;
       sleep(2);
       }

}

We can see the following image with the compilation and execution of this code. As we can see, every 2 seconds, the program prints on the screen the seconds elapsed 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 consisting 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 which is long enough to send a signal from a second shell. As the output argument to sleep(), we send the remaining variable.

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. Next, we see the code for this example:

#include <stdio.h>
#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 image that we see in the following 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:

kill -n signal PID

The image that we see in the following shows in the console above the execution of this code and the effects of the arrival of a signal that is sent from the console below. 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.

Share Button

Source: linuxhint.com

Leave a Reply