#include
#include
#include
#include
#include
#include
#include
#define MAX_TIMERS 100
#define TRUE 1
#define FALSE 0
#define MAX_LONG_TIME 500
#define MAX_LINE 50
struct timer{
int inuse;
time_t time;
char event;
} timers[MAX_TIMERS];
struct timer timer_next = NULL;
time_t time_timer_set;
sigset_t newmask,oldmask;
volatile time_t time_now(void);
void undeclare(struct timer );
struct timer *declare(unsigned int,char);
void timers_update(time_t);
int disable_interrupt(void);
void start_physical_timer(time_t);
int enable_interrupt(void);
void timer_interrupt_handler(void);
int
main(int argc,char argv[])
{
int i,j,k,l,time;
char number[MAX_LINE];
char program[MAX_LINE];
struct sigaction action,old;
action.sa_handler = timer_interrupt_handler;
action.sa_flags = 0;
sigaction(SIGALRM,&action,&old);
if(argc == 1) {
fprintf(stderr,"big bro ,i hate you");
exit(0);
}
while(++argv != NULL){
j=strlen(*argv);
for(i=0;i<j;i++)
if (isalpha(*argv[i])) break;
strcnpy(number,*argv,i-1);
time = (unsigned int)atoi(number);
for(k=i;k<j;k++)
program[l++] = *argv[k];
declare(time,program);
}
return 0;
}
void
start_physical_time(time_t lon)
{
if(alarm((unsigned int)lon) < 0)
fprintf(stderr,"alarm error");
}
void
timer_interrupt_handler(void)
{
time_t t =time_now();
timers_update( t - time_timer_set);
if (timer_next) {
time_timer_set = t;
start_physical_timer(timer_next->time);
}
}
void timers_update(time_t gap)
{
int i;
static struct timer last_time {
FALSE,
MAX_LONG_TIME,
TRUE
};
timer_next = &last_time;
for(i=0;i
if (timers[i]->time >gap) {
timers[i]->time -=gap;
if (timers[i]->time < timer_next->time)
timer_next = &timers[i];
} else {
*timers[i]->event = TRUE;
timers[i]->inuse = 0;
}
}
if (!timer_next->inuse) timer_next = 0;
}
struct timer*
declare(unsigned int time,char *event)
{
int i;
time_t t;
for(i=0;i
if(!timers[i]->inuse)
break;
if (i == MAXTIMERS) {
return 0;
}
timers[i]->time = (time_t)time;
timers[i]->event = event;
if (!timer_next) {
time_timer_set = t;
start_physical_timer(timers[i]->time);
} else if (time+t < timer_next->time+time_time_set) {
timers_update(t - time_timer_set);
time_timer_set = t;
start_physical_timer((timer_next = t)->time);
}else {}
timers[i]->inuse = TRUE;
return (timers[i]);
}
volatile time_t
time_now(void)
{
static long clk;
clk = sysconf(_SC_CLK_TCK);
clock_t sum;
struct tms *buf;
times(buf);
sum = buf->tms_utime+buf->tms_stime;
return (time_t)(sum/clk);
}
还没有完成,没有加入信号的控制,编译了一下,发现结构数组的结构指针无效,给出的错误为error: invalid type argument of ‘->’ (have ‘struct timer’)
timers[i]->event = event;
还有这个 error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
static struct timer last_time {
^
timer.c:86:2: error: expected ‘;’ before ‘}’ token
};困扰我一上午了,求大神解答,谢谢了