Git Product home page Git Product logo

xv6-scheduling-july-2022's Introduction

Xv6 Scheduling

Updates:

- argptr() is now deprecated, refer to argaddr() for passing pointers to the kernel.

- Ticket update ambiguities cleared.

In this assignment, you will be implementing a new scheduler for the xv6 operating system. This scheduler will implement a lottery scheduling algorithm. The basic idea is quite simple. Each process will be assigned a fixed number of tickets (an integer number). A process will be probabilistically assigned a time slice based on its number of tickets.

More specifically, if there are $n$ processes $p_1, p_2, \cdots, p_n$ and they have $t_1, t_2, \cdots, t_n$ tickets respectively at the beginning of a time slice, then a process $p_i$ has a probability of $\frac{t_i}{\sum_{j=1}^{n}t_j}$ of being scheduled at that time slice. You will basically sample $p_i$ based on the probability distribution derived from the ticket counts.

At the end of that time slice, $t_i$ will be reduced by $1$ (i.e., the process will have used that ticket). Hence, the probabilities will have to be recomputed at the beginning of each time slice using the updated ticket counts. Once the ticket counts of all runnable processes become $0$, original ticket counts of all processes will be reinstated.

System Calls

You will need two system calls for your implementation:

  • settickets

The first system call is int settickets(int number), which sets the number of tickets of the calling process. By default, each process should get $1$ ticket; calling this routine makes it such that a process can raise the number of tickets it receives and thus receive a higher proportion of CPU cycles. This routine should return $0$ if successful and $-1$ otherwise (if, for example, the caller passes in a number less than one).

  • getpinfo

The second system call is int getpinfo(struct pstat *). This routine returns some information about all running processes, including how many time slices each has been scheduled to run and the process ID of each. You can use this system call to build a variant of the command line program ps, which can then be called to see what is going on. The structure pstat is defined below; note that you cannot change this structure and must use it exactly as is. This routine should return $0$ if successful and $-1$ otherwise (if, for example, a bad or NULL pointer is passed into the kernel).

You will need to understand how to fill in the structure pstat in the kernel and pass the results to userspace. The structure should look like what you see here in a file, you will have to include called pstat.h:

#ifndef _PSTAT_H_

#define _PSTAT_H_

#include "param.h"

struct pstat {

    int pid[NPROC]; // the process ID of each process

    int inuse[NPROC]; // whether this slot of the process table is being used (1 or 0)

    int tickets_original[NPROC]; // the number of tickets each process originally had

    int tickets_current[NPROC]; // the number of tickets each process currently has

    int time_slices[NPROC]; // the number of time slices each process has been scheduled

};

#endif // _PSTAT_H_

Files

Most of the code for the scheduler is quite localized and can be found in proc.c; the associated header file, proc.h, is also quite useful to examine. To change the scheduler, not much needs to be done; study its control flow and then try some small changes.

Ticket Assignment

You will need to assign tickets to a process when it is created. Specifically, you will need to ensure a child inherits the same number of tickets as its parents. Thus, if the parent originally had $42$ tickets and calls fork() to create a child process, the child should also get $42$ tickets initially.

Argument Passing

Good examples of how to pass arguments into the kernel are found in existing system calls. In particular, follow the path of read(), which will lead you to sys_read(), which will show you how to use argaddr() (and related calls) to obtain a pointer that has been passed into the kernel. Note how careful the kernel is with pointers passed from userspace -- they are a security threat(!) and thus must be checked very carefully before usage.

For this particular offline set CPUS = 1 in the makefile of xv6.

Random Number Generation

You will also need to figure out how to generate (pseudo)random numbers in the kernel; you can implement your own random number generator or use any off-the-shelf implementation from the web. You must make sure that the random number generator uses a deterministic seed (so that the results will be reproducible) and is implemented as a kernel-level module.

Testing

You need to write two user-level programs, testticket.c and testprocinfo.c to test out the ticket assignment and scheduling, respectively. The testticket.c program should also have provisions to test forked processes.

Executing testticket.c followed by testprocinfo.c should print the relevant statistics defined in pstat.h like

PID | In Use | Original Tickets | Current Tickets | Time Slices
5     1        200000             149750            50250
3     1        70000              52521             17479
4     1        160000             118543            41457
6     1        400000             301103            98897
1     1        100000             76198             23802

Submission Guidelines (Deadline: January 17, 8:00 AM)

Start with a fresh copy of xv6 from the original repository. Make necessary changes for this offline. Like the previous assignment, you will submit just the patch file.

Don't commit. Modify and create files that you need to. Then create a patch using the following command:

git diff HEAD > studentID.patch

Where studentID = your own six-digit student ID (e.g., 1405004). Just submit the patch file, do not zip it. In the lab, during evaluation, we will start with a fresh copy of xv6 and apply your patch using the command:

git apply <studentID>.patch

Make sure to test your patch file after submission in the same way we will run it during the evaluation.

Please DO NOT COPY solutions from anywhere (your friends, seniors, internet, etc.). Any form of plagiarism (irrespective of source or destination) will result in getting -100% marks in this assignment. It is your responsibility to protect your code.

xv6-scheduling-july-2022's People

Contributors

tahmid04 avatar

Stargazers

 avatar Aszadur Rahman Rakin avatar Akibur Rahman avatar  avatar Anwarul Bashir Shuaib avatar Fardin Anam Aungon avatar Anup Bhowmik avatar

Watchers

 avatar Bishwajit Bhattacharjee avatar Mobaswirul Islam avatar

xv6-scheduling-july-2022's Issues

Should the shell process have a high ticket_original by default?

If I have multiple testticket processes with a moderately high number of tickets (~10), it takes a long time for the shell process to be be selected by the scheduler. So there's a significant waiting time in between when I can input shell commands. I believe its because shell has a default ticket count of 1, although there could be something wrong in my implementation. Please advise.

Also, my implementation of testticket contains an infinite loop at the end so if I run something like:
testticket 5 &; testticket 4 &; testticket 3

The very last process testticket 3 continues running and doesn't return to shell. A possible workaround is having a redundant call at the end like:
testticket 5 &; testticket 4 &; testticket 3 &; echo 1
Will this be acceptable?

Ticket assignment during fork()

Sir we are instructed to pass the parent's ticket number to child's ticket number when fork() called, should it be the parent's Initial ticket number, or the Current ticket number? Its not clearly mentioned in the specs.

Thanks

Md Toki Tahmid
1805030

"init" process never uses its ticket

Dear Sir

As part of our assignment, we were asked to give processes a default ticket value of 1. I have assumed this includes init, the very first process, and have assigned a ticket value of 1 to it. However, once init forks sh, init is always in the sleep state (as seen by procdump), and never ticks again for as long as xv6 runs. If it does not ever run again, it will never use its ticket.

We were asked to reinstate ticket values only when all processes have zero tickets. How do we deal with this apparent contradiction? Or have I missed anything?

Thanks in advance
Showvik
1805068

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.