#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,沒有必要,在正常執行結束時,所有輸出緩衝區都被刷新。 –