C Programming

Sleep function in C

The sleep() method, which suspends the implementation of the program for a specified number of seconds, is familiar to most C programmers. Seconds are a significant amount of time, particularly with a computer where things happen swiftly. As a result, there is a demand for a function that pauses execution for shorter periods. The sleep() method in the C programming language allows you to wait for just a current thread for a set amount of time. The sleep() function will sleep the present executable for the time specified by the thread. Presumably, the CPU and other operations will function normally.

The sleep() function suspends execution of the requesting thread until the number of real-time seconds provided by the argument seconds has passed or a signal is given to the calling thread with the action of invoking a signal-catching function or terminating the process has elapsed. Because the system planned activities, the stoppage time may be greater than required. Now, let’s discuss a few sample code examples and return values of sleep function in the C programming language.

Return Value of Sleep function

The value returned by sleep() should be 0 if the specified period has passed. If sleep() returns due to a signal’s transmission, the “unslept” quantity, the desired time minus the time slept in seconds, should be returned.

Example 1

Let’s begin with the first example of this guide. Initially, generate a file with any with your preferred name having “.c” extension. Add the subsequent displayed code into it and save the file.

In the initial line of code, we have utilized an input/output library. “.h” means that it is the header file. After that, we have a main() function where the actual code is present. We have a printf() function in the main program, which will exhibit the string declared in the inverted commas. Then we have a sleep function. We have added “5” in its parameters, which depicts that it will sleep for 5 seconds only.

Once you understand the whole code, move towards its execution and compilation that’s very easy. Use any compiler of your choice. We are implementing this illustration on the GCC compiler. Simply click on the “run and compile” option present in the GCC compiler. You will get the attached black screen along with the output presented in it.

Example 2

Now move towards our second example of this guide. Initially, just create a file with any with your desired name having “.c” extension. Add the following presented code into it and save the file.

In the first few codes of code, we have utilized a few standard libraries necessary for the program execution. “.h” means that it is the header file. After that, we have a main() function where the actual code is present. We have a printf() function in the main program, to display the string mentioned in the inverted commas. Then we have a sleep function. We have added “5000” in its parameters, which depicts that it will sleep for 5000 microseconds only. After sleep time, another printf() function will execute and display another string that is presented in the inverted commas.

Once you understand the whole code, move towards its execution and compilation that’s very simple. Use any compiler of your choice. We are implementing this illustration on the GCC compiler. Simply click on the “run and compile” option present in the GCC compiler. You will get the attached black screen along with the output displayed on it.

Example 3

Now move towards our third and last example of this guide. This example is slightly dissimilar from the above examples. Initially, just create a file with any with your desired name having “.c” extension. Add the following presented code into it and save the file.

In the initial line of code, we have utilized an input/output library. “.h” means that it is the header file. After that, we have a main() function where the actual code is present. We have a printf() function in the main program, to display the string mentioned in the inverted commas. Then we have a sleep function. In its parameters, we have added “0.100,” which depicts that it will sleep for 100 milliseconds only. We have used a float type value as a parameter.

Once you understand the whole code, move towards its execution and compilation that’s very easy. Use any compiler of your choice. We are implementing this example on the GCC compiler. Simply click on the “run and compile” option present in the GCC compiler. You will get the attached black screen along with the output displayed on it.

Conclusion

This article is a complete and comprehensive guide for all those programmers who want to learn about sleep() function in the C programming language. We have elaborated on three different examples to apply sleep function in milliseconds, microseconds, and seconds. I advise all readers to implement them on their system to have a grip on the sleep function.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content