[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微秒后繼續循環
	} 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM