#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
static char *runStatusFile = "/syscfg/etc/runStatusFile";
//static char *runStatusFile = "runStatusFile";
FILE *pFile = NULL;
char runCountStr[10]={0};
unsigned runCountInt = 0;
char runTimeStr[15]={0};
unsigned runTimeInt = 0;
void doTask()
{
pFile = fopen(runStatusFile, "r+");
/*
if(NULL == pFile)
{
pFile = fopen(runStatusFile,"w+");
fseek (pFile, 0L, SEEK_SET);
fwrite("0", sizeof(char), 1, pFile);
fseek (pFile, 6L, SEEK_SET);
fwrite("0", sizeof(char), 1, pFile);
}
*/
fseek (pFile, 11L, SEEK_SET);
fread (runTimeStr, sizeof(char), 10, pFile);
runTimeInt = atoi(runTimeStr);
runTimeInt += 30;
fseek (pFile, 11L, SEEK_SET);
sprintf(runTimeStr,"%d",runTimeInt);
fwrite(runTimeStr, sizeof(char),10, pFile);
/*
fseek (pFile, 11L, SEEK_SET);
fread (runTimeStr, sizeof(char), 10, pFile);
printf("runTimeStr = %s/n",runTimeStr);
*/
fclose(pFile);
}
// 建立信号处理机制
void init_sigaction(void)
{
struct sigaction tact; /*信号到了要执行的任务处理函数为doTask*/
tact.sa_handler = doTask;
tact.sa_flags = 0; /*初始化信号集*/
sigemptyset(&tact.sa_mask); /*建立信号处理机制*/
sigaction(SIGALRM, &tact, NULL);
}
void init_time()
{
struct itimerval value; /*设定执行任务的时间间隔为30秒0微秒*/
value.it_value.tv_sec = 30;
value.it_value.tv_usec = 0; /*设定初始时间计数也为30秒0微秒*/
value.it_interval = value.it_value; /*设置计时器ITIMER_REAL*/
setitimer(ITIMER_REAL, &value, NULL);
}
int main()
{
pFile = fopen(runStatusFile, "r+");
if(pFile == NULL)
{
pFile = fopen(runStatusFile,"w+");
fseek (pFile, 0L, SEEK_SET);
fwrite("0", sizeof(char), 1, pFile);
fseek (pFile, 11L, SEEK_SET);
fwrite("0", sizeof(char), 1, pFile);
}
fseek (pFile, 0L, SEEK_SET);
fread (runCountStr, sizeof(char), 5, pFile);
runCountInt = atoi(runCountStr);
runCountInt++;
sprintf(runCountStr,"%d",runCountInt);
fseek (pFile, 0L, SEEK_SET);
fwrite(runCountStr, sizeof(char), 5, pFile);
fclose(pFile);
init_sigaction();
init_time();
while(1);
return 0;
}