busy waiting - How can I write a C program to execute for a certain number of processor seconds? -
i need c program execute precise number of cpu seconds passed in parameter. need such program in order test code monitors process' cpu usage.
example:
busywait x
should run x seconds on processor.
this c program wrote solve problem. continuously checks number of clock cycles until 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(); } }
Comments
Post a Comment