Delay in C

A delay in C is defined as stopping the execution of the program for a specific amount of time. Normally, users delay programs for specific requirements.

Let us see some of those requirements:

  • Testing and Debugging:Introducing delays can simulate real-world conditions, such as network latency, and observe program behaviour over time.
  • Synchronization:Delays are often used to control the timing of events in a program.
  • Resource management:When two programs use the same resources, they must wait for each other to finish using them.

Now, let us see how we can implement delays in C programming.

1. sleep()

The sleep method is used to pause program execution when it is called. Sleep function delays the program for specified seconds.

You must include the special library <unisd.h> to use the sleep function. Let us dive deeper to understand how the sleep method is used.

Example 1

Let us consider an example of delaying the program using the sleep method.

Program

Output:

This message will appear immediately.
This message will appear after a 3-second delay.

Explanation:

The first message appears immediately, followed by the second after 3 seconds.

2. usleep()

If we need to introduce a microsecond delay in our program, how can we achieve that?

We can achieve it using the same library <unistd.h> with different methods. The usleep method accepts an integer input in microseconds, which pauses the program for the specified duration.

Example

Let us discuss using the "usleep" function to pause the program for a certain number of microseconds.

Program

Output:

This message will appear instantly
This message will be displayed one second after a delay.

Explanation:

The above program demonstrated how a program gets delayed for microseconds using the usleep() method. First, we instantly display the first message, followed by the second message 1000000 microseconds later.

3. For loop

Without using any additional libraries, we can delay the program in C.

Here, for loop, iterate over the empty loop with the given number.

Example

Let us see an example program using for loop to delay the program.

Program

Output:

This message will appear instantly
This message will be displayed after specified seconds

Explanation:

The above program prints the first message followed by the second message after the iterations specified in the for loop later.

4. creating own delay function

We can create our own delay function using <time.h> library and loops.

One way to create a precise delay in a loop is to continuously check the current time until a certain amount of time has passed. However, this may consume CPU resources.

Example 1

Output:

This message will appear instantly.
This message will appear after a 3-second delay.

Explanation:

The above program loops until the current_time minus start_time equals the specified seconds.

Example 2

Output:

1 seconds have passed
2 seconds have passed
3 seconds have passed
4 seconds have passed
5 seconds have passed
6 seconds have passed
7 seconds have passed
8 seconds have passed
9 seconds have passed
10 seconds have passed

Explanation:

The above program demonstrates delay, which creates a busy-wait loop that consumes CPU cycles until the current time exceeds start_time + milli_seconds.

Although this method can cause delays, it may be more effective for longer delays as it uses CPU resources. However, for most practical purposes, it is recommended to use platform-specific sleep or delay functions as they are more efficient and provide better timing control.

5. Platform-specific functions

Depending on your platform, you may have access to platform-specific functions that allow you to introduce delays. For example, you can use the 'Sleep' function on Windows, and other platforms use the 'sleep' function.

Example

Let us consider platform-specific functions concerning Windows by including <windows.h>

Program

Output:

This message will appear instantly.
This message will appear after a 3-second delay.

Explanation:

The program demonstrates the Sleep function by including <windows.h>. The first message is printed immediately, while the second message is printed after a 3-second delay.

Conclusion

In various applications, delays in C programming are crucial for controlling timing and synchronization. The choice of delay method depends on the program's specific requirements, the desired precision, and the target platform. When implementing delays in C, it's important to be aware of potential limitations and platform dependencies to ensure the program functions properly. Specialized techniques and hardware may be necessary in real-time systems to guarantee accurate timing for critical operations.