基于Linux下的信号机制的时间定时器

 #include <sys/time.h>

#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include     <termios.h>    /*PPSIX终端控制定义*/
#include     <errno.h>      /*错误号定义*/

#define SYS_TIMER_NUMBER                120

pthread_mutex_t  g_Timermutex = PTHREAD_MUTEX_INITIALIZER;    

 

 

/*timer structure*/
struct timer_struct
{
    int currentTicks;                /*current ticks */
    int defaulTicks;                  /*default ticks*/
    int timerId;                         /*timer ID*/
    unsigned char  useState;     /*0: don't used  1: used*/
    unsigned char timerType;   /*0: no timer function ,1 have timer function */
    void *timeParams;    
    int (*timerProcess)(int timerId);        /*timer function*/
};

struct timer_struct             g_sysTimerArray[SYS_TIMER_NUMBER];

 

/*************************************************
  Function:        secondsTimerSevice
  Description:  
  Calls:          
  Called By:      
  Table Accessed: 
  Table Updated:  
  Input:          
  Output:       
  Return:        
  Others:       
*************************************************/
void secondsTimerSevice(int s)
{
    int index;
 
    for(index = 0 ; index <SYS_TIMER_NUMBER ;index++)
    {
 if((g_sysTimerArray[index].useState ==1) &&(g_sysTimerArray[index].currentTicks != 0))
 {
             g_sysTimerArray[index].currentTicks--;
 }

    }
}

 

 


/*************************************************
 
 Function:    timerThread
  Description:  manage the timer of this system
              
  Input:       
                
  Output:     none     
  Return:     0 failure  1 success   
*************************************************/

void timerThread(void)

{

        int iIndex,iReturn,iMoniterTimerId;
  struct itimerval tick;
  float ftest;
  char uBCD;
  uBCD = 0x30;
 
 
 
 
  /*initialize system timer*/
  memset(&tick, 0, sizeof(tick));  
  tick.it_value.tv_sec = 0;  
  tick.it_value.tv_usec = 100000;  
  tick.it_interval.tv_sec = 0; 
  tick.it_interval.tv_usec = 100000;
        signal(SIGALRM, secondsTimerSevice);
 
         /*starting the real timer*/ 
   iReturn = setitimer(ITIMER_REAL, &tick, NULL);
   if(iReturn == -1)
   { 
         printf("timerThread: set timer failed!!\n"); 
   }

   //moinitorStates
   iMoniterTimerId = getSecondsTimer(g_sysTimerArray,SYS_TIMER_NUMBER, 1,100,NULL, moinitorStates);

//备注:moinitorStates 为一个定时器执行的函数;此处为声明
   while (1) 
   {
 
        pthread_mutex_lock(&g_Timermutex);
        for(iIndex = 0; iIndex<SYS_TIMER_NUMBER; iIndex++)
        {
 
                if(g_sysTimerArray[iIndex].useState == 1  && g_sysTimerArray[iIndex].timerType ==1)
                {
                        if(g_sysTimerArray[iIndex].currentTicks == 0 && g_sysTimerArray[iIndex].timerProcess != NULL)
                        {
 
                                g_sysTimerArray[iIndex].timerProcess(g_sysTimerArray[iIndex].timerId);
 
                       g_sysTimerArray[iIndex].currentTicks = g_sysTimerArray[iIndex].defaulTicks;
                        }
                 } 
        } 
  pthread_mutex_unlock(&g_Timermutex); 
        sleep(1);
          }

 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值