Lab 6 Alarm
Implement a primitive form of user-level interrupt/fault handlers.
Introduction
In this exercise you'll add a feature to xv6 that periodically alerts a process as it uses CPU time. This might be useful for compute-bound processes that want to limit how much CPU time they chew up, or for processes that want to compute but also want to take some periodic action. More generally, you’ll be implementing a primitive form of user-level interrupt/fault handlers; you could use something similar to handle page faults in the application, for example.
Task
Add a new sigalarm(interval, handler)
system call. If an application calls sigalarm(n, fn)
, then after every n “ticks” of CPU time that the program consumes, the kernel should cause application function fn
to be called. When fn
returns, the application should resume where it left off. A tick is a fairly arbitrary unit of time in xv6, determined by how often a hardware timer generates interrupts.
Lab: user-level threads and alarm
Hints
Test code calls sigalarm
to set up alarm so that call function periodic
after 2 ticks. periodic
is the handler code after alarm is fired.
It calls sigreturn
at the end to reset process state to before the alarm fired.
Solution
Add 2 system calls
Add alarm infos in Process state
Initialize cur_ticks in allocproc
(process allocation method). p->cur_ticks = 0;
Handler alarm in trap handler
Save trapframe snapshot in process new field alarm_tf
Increment ticks. If ticks is reaching max, set epc
program counter to the handler, so handler is called once trap is returned to user space.
Implement system calls
sigalarm
saves function handler and max ticks to process state.
sigreturn
is called after alarm is fired, alarm handler is called, and handled by user space. It restores trapframe page from alarm_tf
. Clean up ticks info.
心得
在内核的帮助下,用户端也可以实现‘中断’。用system call来启动, 设置好中断后要做的操作,当tick到了设定好的时长,执行记录的操作,操作结束调回用户程序中断前的snapshot. 这样可以再用户端实现分配器,分配用户程序的时长。
Last updated