steinliber 2015-08-09 03:31 采纳率: 0%
浏览 1578
已结题

apue第10章的题,关于使进程可以设置任意数量的定时器的问题

#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
};困扰我一上午了,求大神解答,谢谢了

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-08-09 22:56
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境