简体   繁体   English

time.h 时钟() 在Windows 下如何工作?

[英]How does time.h clock() work under Windows?

I am trying to create a simple queue schedule for an embedded System in C. The idea is that within a Round Robin some functions are called based on the time constraints declared in the Tasks[] array.我正在尝试为 C 中的嵌入式系统创建一个简单的队列计划。这个想法是在循环中根据Tasks[]数组中声明的时间限制调用一些函数。

#include <time.h>
#include <stdio.h>
#include <windows.h>
#include <stdint.h>

//Constants
#define SYS_TICK_INTERVAL   1000UL
#define INTERVAL_0MS        0
#define INTERVAL_10MS       (100000UL / SYS_TICK_INTERVAL)
#define INTERVAL_50MS       (500000UL / SYS_TICK_INTERVAL)

//Function calls
void task_1(clock_t tick);
void task_2(clock_t tick);
uint8_t get_NumberOfTasks(void);

//Define the schedule structure
typedef struct
{
    double Interval;
    double LastTick;
    void (*Function)(clock_t tick);
}TaskType;

//Creating the schedule itself
TaskType Tasks[] =
{
    {INTERVAL_10MS, 0, task_1},
    {INTERVAL_50MS, 0, task_2},
};

int main(void)
{
    //Get the number of tasks to be executed
    uint8_t task_number = get_NumberOfTasks();

    //Initializing the clocks
    for(int i = 0; i < task_number; i++)
    {
        clock_t myClock1 = clock();
        Tasks[i].LastTick = myClock1;
        printf("Task %d clock has been set to %f\n", i, myClock1);
    }

    //Round Robin
    while(1)
    {       
        //Go through all tasks in the schedule
        for(int i = 0; i < task_number; i++)
        {
            //Check if it is time to execute it
            if((Tasks[i].LastTick - clock()) > Tasks[i].Interval)
            {
                //Execute it
                clock_t myClock2 = clock();
                (*Tasks[i].Function)(myClock2);
                //Update the last tick
                Tasks[i].LastTick = myClock2;
            }
        }
        Sleep(SYS_TICK_INTERVAL);       
    }
}

void task_1(clock_t tick)
{
    printf("%f - Hello from task 1\n", tick);
}

void task_2(clock_t tick)
{
    printf("%f - Hello from task 2\n", tick);
}

uint8_t get_NumberOfTasks(void)
{
    return sizeof(Tasks) / sizeof(*Tasks);
}

The code compiles without a single warning, but I guess I don't understand how the command clock() work.代码编译时没有任何警告,但我想我不明白命令clock()如何工作的。

Here you can see what I get when I run the program:在这里你可以看到我运行程序时得到的结果:

F:\AVR Microcontroller>timer
Task 0 clock has been set to 0.000000
Task 1 clock has been set to 0.000000

I tried changing Interval and LastTick from float to double just to make sure this was not a precision error, but still it does not work.我尝试将IntervalLastTick从 float 更改为 double 以确保这不是精度错误,但仍然不起作用。

%f is not the right formatting specifier to print out myClock1 as clock_t is likely not double . %f不是打印myClock1的正确格式说明符,因为clock_t可能不是double You shouldn't assume that clock_t is double .你不应该假设clock_tdouble If you want to print myClock1 as a floating point number you have to manually convert it to double :如果要将myClock1打印为浮点数,则必须手动将其转换为double

printf("Task %d clock has been set to %f\n", i, (double)myClock1);

Alternatively, use the macro CLOCKS_PER_SEC to turn myClock1 into a number of seconds:或者,使用宏CLOCKS_PER_SECmyClock1转换为秒数:

printf("Task %d clock has been set to %f seconds\n", i,
    (double)myClock1 / CLOCKS_PER_SEC);

Additionally, your subtraction in the scheduler loop is wrong.此外,您在调度程序循环中的减法是错误的。 Think about it: clock() grows larger with the time, so Tasks[i].LastTick - clock() always yields a negative value.想一想: clock()会随着时间变大,所以Tasks[i].LastTick - clock()总是产生一个负值。 I think you want clock() - Tasks[i].LastTick instead.我想你想要clock() - Tasks[i].LastTick代替。

The behavior of the clock function is depending on the operating system. clock功能的行为取决于操作系统。 On Windows it basically runs of the wall clock, while on eg Linux it's the process CPU time.在 Windows 上它基本上运行挂钟,而在例如 Linux 上它是进程 CPU 时间。

Also, the result of clock by itself is useless, it's only use is in comparison between two clocks (eg clock_end - clock_start ).此外, clock本身的结果是无用的,它仅用于比较两个时钟(例如clock_end - clock_start )。

Finally, the clock_t type (which clock returns) is an integer type, you only get floating point values if you cast a difference (as the one above) to eg double and divide by CLOCKS_PER_SEC .最后, clock_t类型( clock返回的)是整数类型,如果您将差异(如上面的那个)转换为例如double并除以CLOCKS_PER_SEC ,则只能获得浮点值。 Attempting to print a clock_t using the "%f" format will lead to undefined behavior .尝试使用"%f"格式打印clock_t将导致未定义的行为

Reading a clock reference might help.阅读clock参考可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM