Add a new alarm(interval, handler) system call. If an application calls alarm(n, fn), then after every n ticks of CPU time that the program consumes, the kernel will cause application function fn to be called. When fn returns, the application will resume where it left off.
First, we modify the struct proc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
structproc { uint sz; // Size of process memory (bytes) pde_t* pgdir; // Page table char *kstack; // Bottom of kernel stack for this process enumprocstatestate;// Process state int pid; // Process ID structproc *parent;// Parent process structtrapframe *tf;// Trap frame for current syscall structcontext *context;// swtch() here to run process void *chan; // If non-zero, sleeping on chan int killed; // If non-zero, have been killed structfile *ofile[NOFILE];// Open files structinode *cwd;// Current directory char name[16]; // Process name (debugging) // fields for alarm int alarmticks; int current_tick; void (*alarmhandler)(); };
We also need to modify allocproc() in proc.c to initialize the fields we added.