Implementation of multi-timer under Linux (Classic) __linux

Source: Internet
Author: User
One, the existing timer interface
Time and space management is the main task of computer system. In time management, we often use the timer to deal with things: such as the TCP protocol in the use of Timer management packet timeout, video display in the use of timers to display video frames regularly, the Web service in the use of timers to manage user timeout. Windows provides a timer interface such as SetTimer and timeSetEvent, while Linux provides interfaces such as Setitimer. The interfaces of these functions are very similar, in general the user provides callback functions and timeout time to register a timer event with the OS, and when the OS is timed out, the user-supplied callback function is invoked to accomplish what the user wants to do. The interface in Windows has multiple timers in the single process, while Linux only allows a single process to have a timer, so in a single process under Linux to use multiple timers, you need to maintain their own management, which is the starting point of this article. In addition, the OS provides the timer management algorithm in the large-scale timer management may not be satisfactory, this time the user to optimize the management algorithm, this article in this regard provides a bit of material.

Second the simplest implementation of multiple timers (Linux version)
1. Implementation Details
This implementation allows the user to use multiple custom timers, and each custom timer will be triggered periodically until it is deleted. The main ideas to realize are:
I first use Setitimer to register a timed event for a basic time unit (such as 1s) when initializing multiple timers (Init_mul_timer);
II users need to register the Set_a_timer custom timer, in the Timer_manage management structure to record the timer callback function and timing cycle parameters;
III when the basic unit of time expires (such as when the SIGALRM signal arrives), traverse the entire timer_manage, and if there is a custom timer timeout, execute the corresponding callback function and set the custom timer timeout to the original value; otherwise the custom timer The time-out period is correspondingly reduced by a basic time unit;
IV The user removes a timer through Del_a_timer and Destroy_mul_timer to remove the entire timer.
2, Code
i) mul_timer.h

/* This file provides an interface of multiple timers. For convenience, it simplify signal processing.
* Also, it can ' t be used in multithreading environment.
* Author:bripengandre
* date:2009-04-29
*/
#ifndef _mul_timer_h_
#define _mul_timer_h_

#include <sys/time.h>

#define MAX_TIMER_CNT 10
#define MUL_TIMER_RESET_SEC 10
#define TIMER_UNIT 60
#define Max_func_arg_len 100
#define INVALID_TIMER_HANDLE (-1)

typedef int timer_handle_t;

typedef struct _TIMER_MANAGE
{
struct _TIMER_INFO
{
int state; /* On or off * *
int interval;
int elapse; * 0~interval * *
Int (* timer_proc) (void *arg, int arg_len);
Char Func_arg[max_func_arg_len];
int Arg_len;
}TIMER_INFO[MAX_TIMER_CNT];

void (* old_sigfunc) (int);
void (* new_sigfunc) (int);
struct Itimerval value, ovalue;
}_timer_manage_t;

/* success, return 0; Failed, return-1 * *
int Init_mul_timer (void);
/* success, return 0; Failed, return-1 * *
int Destroy_mul_timer (void);
/* Success, return timer handle (>=0); Failed, return-1 * *
timer_handle_t set_a_timer (int interval, int (* timer_proc) (void *arg, int arg_len), void*arg, int arg_len);
/* success, return 0; Failed, return-1 * *
int Del_a_timer (timer_handle_t handle);

#endif/* _mul_timer_h_ * *

II) MUL_TIMER.C

#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include "Mul_timer.h"

static struct _timer_manage timer_manage;

static void Sig_func (int signo);

/* success, return 0; Failed, return-1 * *
int Init_mul_timer (void)
{
int ret;

memset (&timer_manage, 0, sizeof (struct _timer_manage));
if ((Timer_manage.old_sigfunc = Signal (SIGALRM, sig_func)) = = Sig_err)
{
Return (-1);
}
Timer_manage.new_sigfunc = Sig_func;

Timer_manage.value.it_value.tv_sec = mul_timer_reset_sec;
timer_manage.value.it_value.tv_usec = 0;
Timer_manage.value.it_interval.tv_sec = Timer_unit;
timer_manage.value.it_interval.tv_usec = 0;
ret = Setitimer (Itimer_real, &timer_manage.value, &timer_manage.ovalue);

return (ret);
}


/* success, return 0; Failed, return-1 * *
int Destroy_mul_timer (void)
{
int ret;

if ((Signal (SIGALRM, timer_manage.old_sigfunc)) = = Sig_err)
{
Return (-1);


}

ret = Setitimer (Itimer_real, &timer_manage.ovalue, &timer_manage.value);
if (Ret < 0)
{
Return (-1);
}
memset (&timer_manage, 0, sizeof (struct _timer_manage));

return (0);
}


/* Success, return timer handle (>=0); Failed, return-1 * *
timer_handle_t set_a_timer (int interval, int (* timer_proc) (void *arg, int arg_len), void *arg,int Arg_len)
{
int i;

if (Timer_proc = NULL | | interval <= 0)
{
Return (-1);
}

for (i = 0; i < max_timer_cnt; i++)
{
if (timer_manage.timer_info[i].state = 1)
{
Continue
}

memset (&timer_manage.timer_info[i], 0, sizeof (timer_manage.timer_info[i));
Timer_manage.timer_info[i].timer_proc = Timer_proc;
if (Arg!= NULL)
{
if (Arg_len > Max_func_arg_len)
{
Return (-1);
}
memcpy (Timer_manage.timer_info[i].func_arg, ARG, Arg_len);
Timer_manage.timer_info[i].arg_len = Arg_len;
}
Timer_manage.timer_info[i].interval = interval;
Timer_manage.timer_info[i].elapse = 0;
Timer_manage.timer_info[i].state = 1;
Break
}

if (i >= max_timer_cnt)
{
Return (-1);
}
return (i);
}


/* success, return 0; Failed, return-1 * *
int Del_a_timer (timer_handle_t handle)
{
if (Handle < 0 | | | handle >= max_timer_cnt)
{
Return (-1);
}

memset (&timer_manage.timer_info[handle], 0, sizeof (timer_manage.timer_info[handle));

return (0);
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.