AnsweredAssumed Answered

problem about usleep() on p1010rdb-pb

Question asked by 成劼 顾 on May 16, 2016
Latest reply on May 17, 2016 by Pavel Chubakov

Hello,I write a simple program to print time every 1ms.

 

#include <errno.h>

#include <signal.h>

#include <stdio.h>

#include <string.h>

#include <sys/resource.h>

#include <sys/time.h>

 

int main(void)

{

pid_t pid = getpid();

setpriority(PRIO_PROCESS, pid, -20);

 

while (1)

{

usleep(1000);

struct timeval t;

gettimeofday(&t, NULL);

printf("%u.%06u\n", t.tv_sec, t.tv_usec);

}

return 0;

}

 

while I get the following result.

 

......

1402463778.370107

1402463778.371202

1402463778.372297

1402463778.373393

1402463778.374487

1402463778.375582

1402463778.376677

1402463778.377772

1402463778.378893

1402463778.380010

1402463778.381105

1402463778.382199

1402463778.383293

1402463778.384388      ***********

1402463778.718214      ***********lost

1402463778.719311

1402463778.720407

1402463778.721502

1402463778.722597

1402463778.723691

1402463778.724786

1402463778.725896

1402463778.726992

......

 

about 300ms lost every 1s.Why?

 

 

Then I use setitimer() and pause()

 

struct itimerval tv; 

tv.it_interval.tv_sec = 0;

tv.it_interval.tv_usec = 1000;

tv.it_value.tv_sec = 0;

tv.it_value.tv_usec = 1000;

 

I get the following result.

 

1402465124.8487241402465124.851723

1402465124.852722

1402465124.854723

1402465124.855724

1402465124.856722

1402465124.858724

1402465124.860722

1402465124.862724

1402465124.863722

1402465124.865724

1402465124.866724

1402465124.869723

1402465124.870721     ******

1402465124.872723     ******lost

1402465124.873724

1402465124.874722

1402465124.8767231402465124.879724   ******output overlap

1402465124.880723

1402465124.881721

1402465124.883724

1402465124.884741

1402465124.885722

1402465124.888721

1402465124.890723

1402465124.891724

1402465124.892722

1402465124.8947241402465124.897723

 

lost and  output overlap.Why?

 

Thanks in advance for any help.

Outcomes