I need a C program that would execute for a precise number of CPU seconds passed in as a parameter. I need such a program in order to test code that monitors a process' CPU usage.
Example:
busywait x
Should run for x seconds on my processor.
This is the C program I wrote to solve my problem. It continuously checks the number of clock cycles until the correct number of processor-seconds has been used.
#include <stdio.h>
#include <time.h>
int main(int argc, char * argv[])
{
clock_t start, end;
double cpu_time_used;
int wait;
sscanf(argv[1], "%d", &wait);
start = clock();
end = clock();
while (((double) (end - start)) / CLOCKS_PER_SEC < wait)
{
end = clock();
}
}