linux 定时器使用二

时间:2023-01-31 23:34:35

#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;
}