這裏只寫部分代碼,其他的給出的提示已經不少了node
trap.c
中的case T_IRQ0 + IRQ_TIMER:
裏:函數
struct proc* p = myproc(); if (p != 0 && (tf->cs & 3) == 3) { p->current_ticks++; if (p->current_ticks == p->alarmticks) { p->current_ticks = 0; tf->esp -= 4; *(uint*)(tf->esp) = tf->eip; tf->eip = (uint)p->alarmhandler; } }
proc.c
中的allocproc
函數裏:ui
p->current_ticks = 0; p->alarmticks = -1;
alarmtest.c
:code
#include "types.h" #include "stat.h" #include "user.h" void periodic(); int main(int argc, char* argv[]) { int i; printf(1, "alarmtest starting\n"); alarm(1, periodic); // 此處我改爲了1 for (i = 0; i < 25 * 500000; i++) { if ((i % 250000) == 0) write(2, ".", 1); } exit(); } void periodic() { printf(1, "alarm!\n"); }
輸出:htm
xv6... cpu1: starting 1 cpu0: starting 0 sb: size 1000 nblocks 941 ninodes 200 nlog 30 logstart 2 inodestart 32 bmap start 58 init: starting sh $ alarmtest alarmtest starting ....alarm! ..........alarm! ..............alarm! .......alarm! ..........alarm! ....alarm! .$