https://www.cse.iitb.ac.in/~mythili/os/labs/lab-xv6-proc/xv6-proc.pdf
to record number of number of times the process was context switched in by the scheduler, add a field to the proc structure.
proc.h
37 // Per-process state
38 struct proc {
53 int cscnt; //number of context switched
54 };
First, allocproc() have to initialize the cscnt as 0.
Then, every time that the process is scheduled, scheduler will increment the cscnt.
proc.c
74 static struct proc*
75 allocproc(void)
...
89 found:
90 p->state = EMBRYO;
91 p->pid = nextpid++;
92 p->cscnt = 0; //initialization
324 void
325 scheduler(void)
326 {
...
337 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
338 if(p->state != RUNNABLE)
339 continue;
...
344 c->proc = p;
345 switchuvm(p);
346 p->state = RUNNING;
347 p->cscnt++;
function getProcInfo() was implemented as follows:
PID 1, init process has no parent field, and thus it would cause segmentation fault without exception.
-> parent pid of the init process is set as 0 by convention.
proc.c
665 int getProcInfo(int pid, struct processInfo *pinfo) {
666
667 struct proc *p;
668
669 acquire(&ptable.lock);
670 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
671 if(p->pid == pid){
672 if(pid == 1) pinfo->ppid = 0; //parent pid of the init process is 0 by convention
673 else pinfo->ppid = p->parent->pid;
674 pinfo->psize = p->sz;
675 pinfo->numberContextSwitches = p->cscnt;
676 release(&ptable.lock);
677 return 0;
678 }
679 }
680 release(&ptable.lock);
681 return -1;
682
683 }
A process with high priority should be given more time slices than others.
trap.c
104 // Force process to give up CPU on clock tick.
105 // If interrupts were on while locks held, would need to check nlock.
106 if(myproc() && myproc()->state == RUNNING &&
107 tf->trapno == T_IRQ0+IRQ_TIMER) {
108 current = myproc()->currentprio;
109 if(current > 0) {
110 myproc()->currentprio = current-1;
111 }
112
113 else {
114 yield();
115 }
116 }
child process must executes a function specified by a parent process.
To inform the child process to begin execution at the specific function, I set the "wf" in the struct proc and "fnc" field.
proc.h
38 struct proc {
...
56 int wf; //welcome function 0 / 1
57 uint fnc; //address of welcome function
58 };
proc.c
184 int
185 fork(void)
186 {
...
207 if(curproc->wf) {
208 np->tf->eip = curproc->fnc; //if the welcomefunction is enabled, set the instruction p
ointer to function address
209 }
...
704 void welcomeFunction(void(*fnc) (void)) {
705 struct proc *curproc = myproc();
706
707 curproc->wf = 1;
708 curproc->fnc = (uint)fnc;
709
710 }
711
712 int welcomeDone(void) {
713 struct proc *curproc = myproc();
714 curproc->tf->eip = curproc->parent->tf->eip;
715 // child process must return 0 from fork() function.
716 return 0; //this must be set because %eax will be set by syscall return value. -> 161 line of syscall.c, if return is void, %eax will be set to unknown value and test code cannot recognize the child process.
717 }
https://www.cse.iitb.ac.in/~mythili/os/labs/lab-xv6-mem/xv6-mem.pdf
https://www.cse.iitb.ac.in/~mythili/os/labs/lab-xv6-sync/xv6-sync.pdf