linux 定時器使用二

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>

static char *runStatusFile   = "/syscfg/etc/runStatusFile";
FILE *pFile = NULL;
char runCountStr[6]={0};
unsigned runCountInt = 0;
char runTimeStr[6]={0};
unsigned runTimeInt = 0;

void doTask()
{
 
 pFile = fopen(runStatusFile, "r+");
 fseek (pFile, 6L, SEEK_SET);
 fread (runTimeStr, sizeof(char), 5, pFile);
 runTimeInt = atoi(runTimeStr);
 runTimeInt += 10;
 fseek (pFile, 6L, SEEK_SET);
 sprintf(runTimeStr,"%d",runTimeInt);
 fwrite(runTimeStr, sizeof(char),5, pFile);
 
 //fseek (pFile, 6L, SEEK_SET);
 //fread (runTimeStr, sizeof(char), 5, pFile);
 //printf("runTimeStr = %s/n",runTimeStr);
 fclose(pFile); 
}


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, 6L, 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);
 
 
 struct sigaction act;
 union sigval tsval;
 act.sa_handler = doTask;
 act.sa_flags = 0;
 sigemptyset(&act.sa_mask);
 sigaction(50, &act, NULL);
 
 while(1)
 {
  sleep(10); //睡眠10秒
  sigqueue(getpid(), 50, tsval);//向主進程發送信號,實際上是自己給自己發信號
 }
 
 return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章