gpt4 book ai didi

c - linux启动时间和停止计时器不工作?

转载 作者:行者123 更新时间:2023-11-30 18:57:10 25 4
gpt4 key购买 nike

#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);
}

我创建了计时器并每 2 毫秒、10 毫秒和 100 毫秒调用一次任务。以上是2ms任务的代码。我想计算在任务中执行一些计算的开始时间和停止时间,最后想知道两者之间的时间差。当我运行我的应用程序时,它只是显示时间差(即duration2ms)。它不打印 StartTime 和 StopTime。有人可以帮助我吗?

最佳答案

如果您阅读clock_gettime的手册页,您会看到

clock_gettime() return 0 for success, or -1 for failure.

因此,您仅打印返回代码。要打印开始时间和停止时间,您需要打印变量 startstop 的内容。例如:

   printf("Start time: %d.%09d\n", start.tv_sec, start.tv_nsec);

关于c - linux启动时间和停止计时器不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21965787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com