Lab 6 Alarm
Implement a primitive form of user-level interrupt/fault handlers.
Last updated
Was this helpful?
Implement a primitive form of user-level interrupt/fault handlers.
Last updated
Was this helpful?
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.
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.
Initialize cur_ticks in allocproc
(process allocation method). p->cur_ticks = 0;
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.
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. 这样可以再用户端实现分配器,分配用户程序的时长。