I recently purchased this book to get a better understanding of how operating systems work. I am on the second chapter and I'm stuck on this problem and my OS does not boot with code I have added. The code below was added to proc.c at the start of the pic_proc function to try and modify the scheduler.
Modification.
int realtime;
clock_t recent_time[NR_TASKS + NR_PROCS];
clock_t stopwatch;
realtime = getuptime();
recent_time[proc_ptr->p_nr + NR_TASKS] = realtime - stopwatch;
stopwatch = realtime;
Original Code:
PRIVATE void pick_proc()
{
register struct proc *rp; /* process to run */
int q; /* iterate over queues */
/* Modified Code here */
for (q=0; q < NR_SCHED_QUEUES; q++) {
if ( (rp = rdy_head[q]) != NIL_PROC) {
next_ptr = rp;
if (priv(rp)->s_flags & BILLABLE)
bill_ptr = rp;
return;
}
}
}
Book Operating Systems Design and Implementation. 3rd, Edition The Minix Book, P219, #45. Modify the Minix 3 scheduler to keep track of how much CPU time each user process has had recently. When no task or server wants to run, pick the user process that has had the smallest share of the CPU.



Based on your new code [and the
pick_procfunction body], the only possible failures are the ones I've described above in my top comments.That is,
proc_ptrmust be valid (i.e. non-NULL) andproc_ptr->p_nrmust be in range to prevent UB.What are the
NR_TASKSandNR_PROCSvalues? Canrecent_timefit on the stack? Stack size in a kernel is usually very limited. For example, under linux, you can only have about 4KB of stack.So, if
NR_PROCSwas (e.g.) 32767, thenrecent_timecould not be placed on the stack.And, I don't see a reason the have an independent array for this on the stack as it won't persist past the call.
So, to aid in debug, I'd add
staticto therecent_timedefinitionYou'll need to add some debug code to see what the issue is.
Here's a sample. Adjust the
printfet. al. to suit theminuxkernel's print mechanism [assuming you can print at the state your code is called in]:UPDATE:
I can't emphasize enough the importance of
assertand/orifstatements to pre-validate anything you do to prevent UB. And, debugprintf, such as:BTW, I pulled the
minixsource code from github:git clone https://github.com/Stichting-MINIX-Research-Foundation/minix.git
It appears that you may have outdated source because the
pick_procin that repo has more SMP related code.You really want to either use the existing fields in
struct procor add some of your own vs. creating a separate array [indexed by process number].Yes, hopefully elapsed CPU time for each process is in
struct proc. If not, you'll have to add a field. I suspect thatp_user_time[and/orp_sys_time] may be what you want. And, maybe,p_cpu_time_left. Other possibilities:p_accounting.time_in_queue. Or,p_cyclespick_procjust looks at the head of a given run queue. So, you may need to change the code that actually inserts a given process into that queue. That might beenqueueand/ordequeue. They appear to honor process priority [p_priority] and preemption.I've skimmed the kernel code, and at a guess,
sched_procmay be of interest.But, I really think you'll need to examine the kernel code more closely to see what functions actually add a process to a given run queue. And, how they select a process and from which queue.
When adding a process, it may just append to the tail. That code would need to scan the queue and [assuming the priority is the same], insert based on lowest CPU usage.