implement time delay in c

后端 未结 17 1762
我在风中等你
我在风中等你 2020-11-30 05:43

I don\'t know exactly how to word a search for this.. so I haven\'t had any luck finding anything.. :S

I need to implement a time delay in C.

for example I w

相关标签:
17条回答
  • 2020-11-30 06:16

    for C use in gcc. #include <windows.h>

    then use Sleep(); /// Sleep() with capital S. not sleep() with s .

    //Sleep(1000) is 1 sec /// maybe.

    clang supports sleep(), sleep(1) is for 1 sec time delay/wait.

    0 讨论(0)
  • 2020-11-30 06:17

    Although many implementations have the time function return the current time in seconds, there is no guarantee that every implementation will do so (e.g. some may return milliseconds rather than seconds). As such, a more portable solution is to use the difftime function.

    difftime is guaranteed by the C standard to return the difference in time in seconds between two time_t values. As such we can write a portable time delay function which will run on all compliant implementations of the C standard.

    #include <time.h>
    
    void delay(double dly){
        /* save start time */
        const time_t start = time(NULL);
    
        time_t current;
        do{
            /* get current time */
            time(&current);
    
            /* break loop when the requested number of seconds have elapsed */
        }while(difftime(current, start) < dly);
    }
    

    One caveat with the time and difftime functions is that the C standard never specifies a granularity. Most implementations have a granularity of one second. While this is all right for delays lasting several seconds, our delay function may wait too long for delays lasting under one second.

    There is a portable standard C alternative: the clock function.

    The clock function returns the implementation’s best approximation to the processor time used by the program since the beginning of an implementation-defined era related only to the program invocation. To determine the time in seconds, the value returned by the clock function should be divided by the value of the macro CLOCKS_PER_SEC.

    The clock function solution is quite similar to our time function solution:

    #include <time.h>
    
    void delay(double dly){
        /* save start clock tick */
        const clock_t start = clock();
    
        clock_t current;
        do{
            /* get current clock tick */
            current = clock();
    
            /* break loop when the requested number of seconds have elapsed */
        }while((double)(current-start)/CLOCKS_PER_SEC < dly);
    }
    

    There is a caveat in this case similar to that of time and difftime: the granularity of the clock function is left to the implementation. For example, machines with 32-bit values for clock_t with a resolution in microseconds may end up wrapping the value returned by clock after 2147 seconds (about 36 minutes).

    As such, consider using the time and difftime implementation of the delay function for delays lasting at least one second, and the clock implementation for delays lasting under one second.

    A final word of caution: clock returns processor time rather than calendar time; clock may not correspond with the actual elapsed time (e.g. if the process sleeps).

    0 讨论(0)
  • 2020-11-30 06:18

    For delays as large as one minute, sleep() is a nice choice.

    If someday, you want to pause on delays smaller than one second, you may want to consider poll() with a timeout.

    Both are POSIX.

    0 讨论(0)
  • 2020-11-30 06:18
    system("timeout /t 60"); // waits 60s. this is only for windows vista,7,8
    system("ping -n 60 127.0.0.1 >nul"); // waits 60s. for all windows
    
    0 讨论(0)
  • 2020-11-30 06:18

    Write this code :

    void delay(int x)
    {   int i=0,j=0;
        for(i=0;i<x;i++){for(j=0;j<200000;j++){}}
    }
    
    int main()
    {
        int i,num;
    
        while(1) {
    
        delay(500);
    
        printf("Host name");
        printf("\n");}
    
    }
    
    0 讨论(0)
  • 2020-11-30 06:20

    sleep(int) works as a good delay. For a minute:

    //Doing some stuff...
    sleep(60); //Freeze for A minute
    //Continue doing stuff...
    
    0 讨论(0)
提交回复
热议问题