Usleep Function In C

The usleep function halts the execution of the current thread until the specified number of microseconds (defined by the useconds argument) have elapsed in real-time or until the thread receives a signal. This function may trigger a signal handler or terminate the process. Keep in mind that the actual suspension duration might exceed the specified time due to the system's scheduling of other tasks. It is important to note that the useconds parameter should be a value less than 1,000,000. If useconds is set to 0, the function call will have no effect.

It is not specified whether the usleep function will terminate if a SIGALRM signal is queued for the calling process while the usleep function is running, especially if the SIGALRM signal is either blocked or ignored. Additionally, it is ambiguous whether the signal will be discarded or remain pending after the usleep function completes if it is blocked.

It is not definitively known if a SIGALRM signal triggered for the current process while usleep is running, unless it originates from a prior alarm invocation, and as long as it is not blocked or masked from reaching the process, has any consequences beyond prompting usleep to complete and return.

If a signal-catching function interrupts the usleep function and inspects or alters the scheduled time for a SIGALRM signal, its associated action, or the delivery prevention of the SIGALRM signal, the results become unpredictable.

The behavior linked to the SIGALRM signal and the timing of a scheduled SIGALRM signal are unspecified if a signal-handling function interrupts the usleep operation and invokes siglongjmp or longjmp to reinstate a context preserved prior to the usleep invocation. Furthermore, it is uncertain if the SIGALRM signal is held back until the signal mask of the thread is also reinstated along with the environment.

The precision of timer values might be limited by different implementations. When setting an interval timer, if the specified timer value is more precise than what the implementation allows, it must be rounded up to the nearest supported value.

Syntax of the usleep function in C Language

It has the following syntax:

Example

int usleep (useconds_tuseconds);

An unsigned integer of type useconds_t is employed, yielding a return value of 0 upon success and -1 upon failure. The permissible range of values spans from 0 to 999,999 microseconds, encompassing acceptable microsecond values.

RETURN VALUE

The Usleep function is designed to return a value of 0 upon successful completion. In case of failure, it will return -1 and also set the errno variable to signal an error condition.

Example:

Let's consider an example to showcase the usleep function in the C programming language.

Example

#include <stdio.h>
#include <unistd.h> // For usleep function

int main() {
printf("Start of program\n");

    // Delay for 2 seconds (2000000 microseconds)
usleep(2000000);

printf("End of program\n");

    return 0;
}

Output:

Output

Start of program
[2-second delay]
End of program

Explanation:

For the usleep function and standard input/output operations, we include the necessary header files: stdio.h and unistd.h.

The message "Beginning of program" is displayed on the console right when the program initiates. To introduce a 2-second (2,000,000 microseconds) pause in the program flow, we invoke the usleep(2000000) function.

The software continues execution following a pause and displays "End of program" on the screen. Because of system scheduling and related factors, the exact timing of the pause may vary slightly, making it better suited for short delays rather than precise timekeeping. It's worth noting that the usleep function conforms to POSIX standards and may not be available on all operating systems. In cases where cross-platform compatibility issues surface, it's advisable to explore alternative system-specific functions like Sleep for Windows or nanosleep for POSIX systems.

Example:

Example

#include <stdio.h>
#include <unistd.h> // For usleep function

int main() {
printf("Countdown starting...\n");

    for (int i = 1; i<= 5; i++) {
printf("%d\n", i);
usleep(1000000); // 1 second delay
    }

printf("Countdown completed!\n");

    return 0;
}

Output:

Output

Countdown starting...
1
2
3
4
5
Countdown completed!

Explanation:

  • "Countdown starting..." is printed to the console as soon as the program launches. After that, we utilize a for loop to display numbers from 1 to 5 .
  • Next, we use the printf function within the loop to show the current value of the loop variable i .
  • We use usleep(1000000) ; to add a 1-second delay after each number is displayed. As a result, there is a one-second gap between each presentation of a number.
  • We print "Countdown completed!" to the terminal after the loop is finished.
  • A one-second pause will occur between each number while the program displays the numbers 1 through 5 . It is a more straightforward illustration of how to use usleep to add a delay to the program.

Input Required

This code uses input(). Please provide values below: