cschedulingfifoxv6lifo

Where to implement fifo and lifo in xv6


I am currently doing a homework with xv6, i need to implement FIFO and LIFO with the following style of code:

#define IFO 1
#if IFO==1
  DO FIFO HERE
#endif


#if IFO==2
  DO LIFO HERE
#endif

What xv6 file should i modify to implement those scheduling policies. Any tip of what should i do, i am newbie using xv6 and i don't understand much of what i am doing. Also, the homework include an extra file called sched_test:

#include "types.h"
#include "stat.h"
#include "user.h"
#include <time.h> 

void delay(int number_of_milliseconds){ 
    // Converting time into milli_seconds 
    int milli_seconds = number_of_milliseconds; 

    int start_time = uptime(); 

    while (uptime() < start_time + milli_seconds) ;
} 
int main(){ 
    // Creating first child 
    int n1 = fork(); 
    int count = 0;
    int times = 20; 
    int millisec_to_wait = 5;

    // Creating second child. First child 
    // also executes this line and creates 
    // grandchild. 
    int n2 = fork(); 

    if (n1 > 0 && n2 > 0) 
    { 
        printf(1,"\n0.- parent ID: %d\n",getpid());
        while(count < times){
            printf(1,"0 ");
            delay(millisec_to_wait); 
            count++;
        }
    } 
    else if (n1 == 0 && n2 > 0) 
    { 
        printf(1,"\n1.- first child ID: %d\n",getpid()); 
        while(count < times){
            printf(1,"1 ");
            delay(millisec_to_wait); 
            count++;
        }
    } 
    else if (n1 > 0 && n2 == 0) 
    { 
        printf(1,"\n2.- second child ID: %d\n",getpid()); 
        while(count < times){
            printf(1,"2 ");
            delay(millisec_to_wait); 
            count++;
        }
    } 
    else { 
        printf(1,"\n3.- third child ID: %d\n",getpid()); 
        while(count < times){
            printf(1,"3 ");
            delay(millisec_to_wait); 
            count++;
        }
    } 

    while(wait() >= 0);
    exit(); 
} 

I already included it on MAKEFILE, and execute, make clean, make and make qemu.


Solution

  • You can start your implementation in proc.c in the scheduler() function. Here's where it is in vanilla xv6. In basic xv6, it just loops over the process table to find the first process. You'll want to add you own data structure that you'll use in scheduler() to determine which processes to run next. This is an important part of that function with some notes:

        // LOOP OVER SOME DATA STRUCTURE TO FIND A RUNNABLE PROCESS
        acquire(&ptable.lock);
        for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
          if(p->state != RUNNABLE)
            continue;
    
          c->proc = p;
          switchuvm(p);
          p->state = RUNNING;
    
          swtch(&(c->scheduler), p->context);
          switchkvm();
    
          c->proc = 0;
    
          // ADUJST YOUR DATA STRUCTURE TO AFTER RUNNING THE PROCESS
          #if OWNSCHED==1
              // Add to the end of some D/S
          #elif OWNSCHED==2
              // Add to the beginning of some D/S
          #endif
        }
        release(&ptable.lock);
    
    

    You could add a data structure to the ptable struct in proc.c to track the order of processes to run then loop over this in scheduler(). Of course, that means you'll have to modify some other functions such as allocproc() to add new processes in the appropriate places. Some of the rest depends on a few things, like if you're allowed to use a static array to store processes or if you must use a linked list instead.

    If you haven't already, I highly recommend reading Chapter 6 of the xv6 book. It was really helpful when I had to implement an MLFQ in xv6!