Лекция 4

Download Report

Transcript Лекция 4

Часы и таймеры
Типы таймеров
• относительный таймер
• абсолютный таймер
• периодический таймер
• однократный таймер
Схемы уведомления
• Послать сигнал
• Создать поток
• Послать импульс
Таймеры, посылающие сигналы
•
•
•
•
struct sigevent event;
timer_t timer_id;
SIGEV_SIGNAL_INIT(&event, SIGUSR1);
timer_create(CLOCK_REALTIME, &event,
&timer_id);
Таймеры, создающие потоки
• struct sigevent event;
• timer_t timer_id;
• SIGEV_THREAD_INIT(&event, m_func,
NULL);
• timer_create(CLOCK_REALTIME, &event,
&timer_id);
Таймеры, посылающие импульсы
•
•
•
•
•
struct sigevent event;
timer_t timer_id;
int chid = ChannelCreate(0);
int coid = ConnectAttach(0,0, chid, 0,0);
SIGEV_PULSE_INIT(&event, coid,
SIGEV_PULSE_PRIO_INHERIT,
CODE_TIMER, 0);
• timer_create(CLOCK_REALTIME, &event,
&timer_id);
Установка времени
• int
• timer_settime(
timer_t timer_id,
int flags, // 0 / TIMER_ABSTIME
struct itimerspec * value,
struct itimerspec * oldvalue);
• struct timespec {
long tv_sec,
tv_nsec; // < 1000,000,000
};
• struct itimerspec {
struct timespec it_value,
it_interval;
};
POSIX Timers
• #include <sys/time.h>
•
int getitimer(int which,
struct itimerval *value);
•
int setitimer(int which,
const struct itimerval *restrict value,
struct itimerval *restrict ovalue);
• unsigned alarm(unsigned seconds);
POSIX Timers
•
•
•
ITIMER_REAL
Decrements in real time. A SIGALRM signal is delivered when
this timer expires.
•
•
•
•
ITIMER_VIRTUAL
Decrements in process virtual time. It runs only when the process is executing. A SIGVTALRM signal is delivered when it
expires.
•
•
•
•
•
•
ITIMER_PROF
Decrements both in process virtual time and when the system is
running on behalf of the process. It is designed to be used by
interpreters in statistically profiling the execution of interpreted programs. Each time the ITIMER_PROF timer expires, the
SIGPROF signal is delivered.
clock functions
• #include <time.h>
•
int clock_getres(
clockid_t clock_id, // CLOCK_REALTIME
struct timespec *res);
•
int clock_gettime(clockid_t clock_id,
struct timespec *tp);
•
int clock_settime(clockid_t clock_id,
const struct timespec *tp);
WatchDog
• Программируемый сторожевой (контрольный)
таймер, “будильник”.
• аппаратно или программно реализованная схема
контроля за зависанием системы.
• В его счётчик загружается некоторое значение,
соответствующее заданному временному интервалу.
После инициализации таймер начинает обратный
отсчёт времени, равный этому временному
интервалу.
• Если до конца заданного интервала процессор не
перезагрузит счётчик таймера, то последний через
прерывание вызовет процедуру обработки данной
ситуации, например перезагрузку системы.