#include<time.h> #include<unistd.h>//usleep(num) #include<stdio.h> #include<stdlib.h>//exit(num) void execute(int count); void run(void); int main(){ run(); return 0; } void execute(int count){ printf("%d Hello Boy. ", count); } void run(){ printf("ready run... "); int count = 1; while(1){ unsigned int m; double previous_time_stamp; double current_time_stamp; previous_time_stamp = (double)clock(); //计算睡眠时间(毫秒),保证每隔500ms循环一次 current_time_stamp = (long)clock(); int dis = dis + current_time_stamp - previous_time_stamp; if((dis >= 500) && (count <= 10)){ execute(count);//此处填写执行代码 previous_time_stamp = current_time_stamp; count = count + 1; dis = 0;//每次执行后,计算累计时间的都将清零 } else if(count == 10){ printf("run end... "); exit(0); } //usleep()头文件:unistd.h usleep(1000); //等待m*1000微秒后继续循环 } }