Search code examples
csleep

Why is C nanosleep() not sleeping here?


Im trying to write to the terminal one line at a time but it just prints the whole thing without sleeping. It works if I use sleep(1). Am I just not understanding how nanosleep is suppose to work?

void                                                                           
display_all(int fdin, int fdout)                                               
{                                                                              
    struct timespec tm1,tm2;                                                   

    tm1.tv_sec = 0;                                                            
    tm1.tv_nsec = 1000000000L;                                                                                                            
    while (display_line(fdin, fdout) == 80)                                    
    {                                                                          
        nanosleep(&tm1,&tm2);                                                  
    }                                                                          

}  

display_line is using the function write to write to STDOUT.


Solution

  • From the nanosleep man page:

    The value of the nanoseconds field must be in the range 0 to 999999999