#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef unsigned int uint32;
#define million 1000000L
long duration2ms, duration10ms, duration100ms;
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster;
timer_t firstTimerID, secondTimerID, thirdTimerID;
void TASK1(Task2ms_Raster) {
struct timespec start, stop;
int a, b, c;
uint32 StartTime, StopTime;
a=100, b=2;
if((StartTime = clock_gettime(CLOCK_REALTIME, &start)) == -1) {
perror("clock gettime");
}
printf("start time is= %ld\n", StartTime);
//I am performing some computation like
a = a + 100;
b = a + 120;
a = a + b;
b = a;
c = b;
if((StopTime = clock_gettime(CLOCK_REALTIME, &stop)) == -1) {
perror("clock gettime");
}
printf("stop time is= %ld\n", StopTime);
duration2ms = (stop.tv_sec - start.tv_sec) +
(double)(stop.tv_nsec - start.tv_nsec)/
(double)million;
printf("time difference is= %ld\n", duration2ms);
}
我创建了定时器,并且每2ms,10ms和100ms调用任务。以上是2ms任务的代码。我想计算在任务内执行一些计算的开始时间和停止时间,并最终想知道它们之间的时间差。当我运行我的应用程序时,它只显示时间差(即duration2ms)。它不打印StartTime和StopTime。有人能帮助我吗?linux启动时间和停止计时器不工作?
尝试增加前的最后一个fflush(标准输出)}。在所有事情都被冲掉之前,任务可能会消失。 – cup
@cup,没有必要,在正常执行结束时,所有输出缓冲区都被刷新。 –