I know this likely isn’t the first time questions have been asked about lottery schedulers, but I’ve been doing a lot of searching, and I’m just having a ton of difficulty with figuring all of this out. This is for a college project, and the task seems simple enough; I just need to define a couple of functions, and also make some modifications to the proc.c file to get it to work. It’s that I’ve come across a bunch of different sources giving different ways to do so, that just leaves me feeling very jumbled. The file I need to run is below:
lotterytest.c
#include "syscall.h"
#include "types.h"
#include "user.h"
#include "pstat.h"
#include "param.h"
void spin();
int main(int argc, char *argv[]){
int tickets[] = {100, 200, 300};
int pids[] = {0, 0, 0};
struct pstat info;
int i, rc;
// create three children
for(i = 0; i < 3; i++){
rc = fork();
if(rc == 0) {
settickets(tickets[i]);
spin();
exit();
}
else {
pids[i] = rc;
}
}
// prevent parent from using too much CPU
settickets(10);
// periodically print process information
for(i = 0; i < 5; i++){
sleep(500);
if(getpininfo(&info) < 0)
exit();
printf(1, "PIDtTICKETStTICKSn");
for (int i = 0; i < NPROC; ++i) {
if(info.inuse[i] == 1){
printf(1, "%dt%dt%dn", info.pid[i], info.tickets[i], info.ticks[i]);
}
}
}
// kill all children
for (int i = 0; i < 3; ++i) {
kill(pids[i]);
}
for (int i = 0; i < 3; ++i) {
wait();
}
exit();
}
// infinite computing loop
void spin(){
volatile int sink = 0;
for(;;){
sink = sink + 1;
sink = sink + 1;
sink = sink + 1;
}
}
this is the code for the header file containing a structure I can’t change the code of:
pstat.h
#ifndef _PSTAT_H_
#define _PSTAT_H_
#include "param.h"
struct pstat {
int inuse[NPROC]; //whether this slot of the process table is in use (1 or 0)
int tickets[NPROC]; //the number of tickets this process has
int pid[NPROC]; //the PID of each process
int ticks[NPROC]; //the number of ticks each process has accumulated
};
#endif //_PSTAT_H_
I do have random number generator files set in place for the lottery aspect of the scheduler, but the only thing that has to be worried about for making use of it, is using the random_at_most(long max) function. Now that the information for what I’m working with is given, the thing I’m trying to do is define the int settickets(int number) function, and the
int getpininfo(struct pstat *pinfo) function. It’s just all of the information out there that gives different ways of going about this task, that trips me up so much. I don’t want to ask for flat out answers, seeing as this is a college assignment and all, but I could really use some just some pointers or advice on where to start, as well as how to modify the void scheduler(void) function in proc.c, without disrupting it from “booting up” on the virtual machine I’m using. If there’s information or code I’ve accidentally forgotten to provide that would be important to include, let me know and I’ll add it.
I haven’t been able to figure out where I want to tackle the int getpininfo(struct pstat *pinfo) function from yet, but here are my most recent attempts with modfifying void scheduler(void) and creating int settickets(int number).
modified void scheduler(void):
void
scheduler(void)
{
struct proc *p;
//local var
int found_proc = 1;
struct cpu *c = mycpu();
c->proc = 0;
for(;;){
// Enable interrupts on this processor.
sti();
//local vars within for(;;)
int ticket_count = 0;
int tickets_read = 0;
// Loop over process table looking for process to run.
//acquire(&ptable.lock);
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->state != RUNNABLE){
continue;
}
ticket_count = ticket_count + p->tickets;
}
long winning_ticket = random_at_most(ticket_count);
if(!found_proc){
found_proc = 0;
}
acquire(&ptable.lock);
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->state != RUNNABLE){
continue;
}
tickets_read += p->tickets;
if(tickets_read < winning_ticket){
continue;
}
// Switch to chosen process. It is the process's job
// to release ptable.lock and then reacquire it
// before jumping back to us.
found_proc = 1;
c->proc = p;
switchuvm(p);
p->state = RUNNING;
swtch(&(c->scheduler), p->context);
switchkvm();
// Process is done running for now.
// It should have changed its p->state before coming back.
c->proc = 0;
break;
}
release(&ptable.lock);
}
}
settickets function:
int settickets(int number){
struct proc *p;
int pid;
//get data
if(argint(0,&pid) < 0){
return -1;
}
if(argint(1, &number) < 0){
return -1;
}
if(pid < 1 || number < 1){
return -1;
}
acquire(&ptable.lock);
//find and assign tickets
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->pid == pid){
p->tickets = number;
release(&ptable.lock);
return 0;
}
}
release(&ptable.lock);
return -2;
}
I haven’t really been able to tell if there’s anyway to see how those functions “mesh” without the getpininfo function also being set up, so at the very least, it’d help a lot to get some feedback on if settickets does anything for the modified scheduler function (and if it doesn’t, how I might want to go about fixing it so that it does), as well as how I could approach the getpininfo function so that it can return values. I know at the very least, that I want getpininfo to take values stored in the pstat structure.