2

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.

#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.

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.

1
  • At least one less obvious bug beyond the obvious one, clock() always increments. So Tasks[i].LastTick - clock() is backwards and produces a negative number. Nov 12, 2015 at 10:00

2 Answers 2

1

%f is not the right formatting specifier to print out myClock1 as clock_t is likely not double. You shouldn't assume that clock_t is double. If you want to print myClock1 as a floating point number you have to manually convert it to 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:

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. I think you want clock() - Tasks[i].LastTick instead.

4
  • Thanks for the hint. It shows the same results as before though. :-/
    – Bracketz
    Nov 12, 2015 at 9:51
  • Try to add strategic printf() calls to figure out what's happening.
    – fuz
    Nov 12, 2015 at 9:55
  • Ah! I think your subtraction is the wrong way round. It should be clock() - Tasks[i].LastTick > Tasks[i].Interval as clock() grows larger with time.
    – fuz
    Nov 12, 2015 at 9:56
  • That should be (double)myClock1 / CLOCKS_PER_SEC, not the other way around.
    – user253751
    Nov 12, 2015 at 10:02
1

The behavior of the clock function is depending on the operating system. On Windows it basically runs of the wall clock, while on e.g. Linux it's the process CPU time.

Also, the result of clock by itself is useless, it's only use is in comparison between two clocks (e.g. 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 e.g. double and divide by CLOCKS_PER_SEC. Attempting to print a clock_t using the "%f" format will lead to undefined behavior.

Reading a clock reference might help.

2
  • But OP is comparing the difference between two clock calls.
    – fuz
    Nov 12, 2015 at 9:55
  • @FUZxxl True, but the OP doesn't print the difference. Nov 12, 2015 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.