[C++]Linux之间隔时间内循环执行指定程序

#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.\n", count); 
}

void run(){
	printf("ready run...\n");
	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...\n");
			exit(0);
		}
		//usleep()头文件:unistd.h  
		usleep(1000); //等待m*1000微秒后继续循环
	} 
}

 

posted @ 2017-12-07 20:25  千千寰宇  阅读(1762)  评论(0编辑  收藏  举报