Skip to content

Commit c00dcba

Browse files
committed
MultiLayeredQueue algorithm implemented.
1 parent 5e3809f commit c00dcba

File tree

7 files changed

+139
-83
lines changed

7 files changed

+139
-83
lines changed

defs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ int exec_process(struct proc* p, struct cpu* c); // (
129129
struct proc* checkQueues(int *lastIndex, int *curPriority, char mode); // (Added)
130130
void increase_time(void); // (Added)
131131
int wait2(Times *times); // (Added)
132-
int enQueue(int pid); // (Added)
132+
int enQueue(int queue); // (Added)
133133

134134
// swtch.S
135135
void swtch(struct context**, struct context*);

proc.c

+71-66
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,10 @@ struct {
1212
struct proc proc[NPROC];
1313
} ptable;
1414

15-
/// // added structs/////
16-
typedef struct rr
17-
{
18-
/* data */
19-
struct proc queu[NPROC];
20-
int size;
21-
22-
} RoundRobin;
23-
24-
RoundRobin RoundRobinQ;
25-
26-
typedef struct d
27-
{
28-
/* data */
29-
struct proc queu[NPROC];
30-
int size;
31-
32-
} Default;
33-
34-
Default DefaultQ;
35-
36-
typedef struct prio
37-
{
38-
/* data */
39-
struct proc queu[NPROC];
40-
int size;
41-
42-
} PrioritySchedualing;
43-
44-
PrioritySchedualing priSchedQ;
45-
46-
typedef struct reversePrio
47-
{
48-
/* data */
49-
struct proc queu[NPROC];
50-
int size;
51-
52-
} ReversePrioritySchedualing;
53-
54-
ReversePrioritySchedualing RpriSchedQ;
15+
RoundRobin RoundRobinQ; // (Added)
16+
Default DefaultQ; // (Added)
17+
PrioritySchedualing priSchedQ; // (Added)
18+
ReversePrioritySchedualing RpriSchedQ; // (Added)
5519

5620
static struct proc *initproc;
5721

@@ -370,13 +334,21 @@ wait(void)
370334
// - swtch to start running that process
371335
// - eventually that process transfers control
372336
// via swtch back to the scheduler.
337+
int b = 1;
373338
void // (Modified)
374339
scheduler(void)
375340
{
376-
DefaultQ.size = 0; // (Added)
377-
priSchedQ.size = 0; // (Added)
378-
RpriSchedQ.size = 0; // (Added)
379-
RoundRobinQ.size = 0; // (Added)
341+
if (b == 1){
342+
DefaultQ.size = 0; // (Added)
343+
priSchedQ.size = 0; // (Added)
344+
RpriSchedQ.size = 0; // (Added)
345+
RoundRobinQ.size = 0; // (Added)
346+
q = 0;
347+
b = 0;
348+
}
349+
350+
multilayer = 0; //(Added)
351+
380352
struct proc *p = 0; // (Modified)
381353
struct cpu *c = mycpu();
382354
c->proc = 0;
@@ -421,6 +393,38 @@ scheduler(void)
421393
break;
422394

423395
case 4: // Multy layer Queue
396+
multilayer = 1;
397+
if (q == 1){
398+
399+
curPolicy = 3;
400+
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
401+
if(p->state != RUNNABLE)
402+
continue;
403+
exec_process(p, c);
404+
}
405+
} else if (q == 2){
406+
407+
curPolicy = 1;
408+
p = checkQueues(lastIndex, &lastPriority, 'f');
409+
if(p != 0)
410+
exec_process(p, c);
411+
412+
} else if (q == 3){
413+
414+
curPolicy = 2;
415+
p = checkQueues(lastIndex, &lastPriority, 'r');
416+
if(p != 0)
417+
exec_process(p, c);
418+
419+
} else {
420+
421+
curPolicy = 0;
422+
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
423+
if(p->state != RUNNABLE)
424+
continue;
425+
exec_process(p, c);
426+
}
427+
}
424428
break;
425429
}
426430

@@ -810,10 +814,10 @@ wait2(Times *times)
810814
// Found one.
811815

812816
// writing the related times (Added)
817+
times->sleepingTime = times->sleepingTime;
813818
times->creationTime = p->creationTime;
814819
times->readyTime = p->readyTime;
815820
times->runningTime = p->runningTime;
816-
times->sleepingTime = times->sleepingTime;
817821
// done writing
818822

819823
pid = p->pid;
@@ -841,34 +845,35 @@ wait2(Times *times)
841845
}
842846
}
843847

848+
// this function add current process to given queue (Added)
844849
int
845-
enQueue(int pid)
850+
enQueue(int queue)
846851
{
847-
struct proc p;
848-
849-
for (int i = 0; i < NPROC; i++){
850-
if (ptable.proc[i].pid == pid){
851-
p = ptable.proc[i];
852-
}
853-
}
852+
struct proc *curproc = myproc();
854853

855-
if (p.queue == 1){
856-
RoundRobinQ.queu[RoundRobinQ.size] = p;
854+
if (queue == 1)
855+
{
856+
RoundRobinQ.queu[RoundRobinQ.size] = curproc;
857+
curproc->queue = queue;
857858
RoundRobinQ.size++;
858-
859-
} else if (p.queue == 2){
860-
priSchedQ.queu[priSchedQ.size] = p;
859+
}
860+
else if (queue == 2)
861+
{
862+
priSchedQ.queu[priSchedQ.size] = curproc;
863+
curproc->queue = queue;
861864
priSchedQ.size++;
862-
863-
} else if (p.queue == 3){
864-
RpriSchedQ.queu[RpriSchedQ.size] = p;
865+
}
866+
else if (queue == 3)
867+
{
868+
RpriSchedQ.queu[RpriSchedQ.size] = curproc;
869+
curproc->queue = queue;
865870
RpriSchedQ.size++;
866-
867-
} else {
868-
DefaultQ.queu[DefaultQ.size] = p;
871+
}
872+
else
873+
{
874+
DefaultQ.queu[DefaultQ.size] = curproc;
875+
curproc->queue = queue;
869876
DefaultQ.size++;
870-
871877
}
872-
873878
return 1;
874879
}

proc.h

+36-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ struct cpu {
1313
int curPolicy; // shows that current scheduling policy (Added)
1414
extern struct cpu cpus[NCPU];
1515
extern int ncpu;
16+
int multilayer;
17+
int q;
1618

1719
//PAGEBREAK: 17
1820
// Saved registers for kernel context switches.
@@ -52,15 +54,44 @@ struct proc {
5254
char name[16]; // Process name (debugging)
5355
int syscall_occurrence[24]; // save occurrence of each syscall (Added)
5456
int priority; // priority of a process (Added)
55-
int creationTime; // allocation time
56-
int runningTime; // RUNNING state
57-
int readyTime; // RUNNABLE state
58-
int sleepingTime; // SLEEPING state
59-
int queue; // number of queue that the process belons to
57+
int creationTime; // allocation time (Added)
58+
int runningTime; // RUNNING state (Added)
59+
int readyTime; // RUNNABLE state (Added)
60+
int sleepingTime; // SLEEPING state (Added)
61+
int queue; // number of queue that the process belongs to it (Added)
6062
};
6163

6264
// Process memory is laid out contiguously, low addresses first:
6365
// text
6466
// original data and bss
6567
// fixed-size stack
6668
// expandable heap
69+
70+
// structs for multilevel queue (Added)
71+
typedef struct rr
72+
{
73+
struct proc* queu[NPROC];
74+
int size;
75+
76+
} RoundRobin;
77+
78+
typedef struct d
79+
{
80+
struct proc* queu[NPROC];
81+
int size;
82+
83+
} Default;
84+
85+
typedef struct prio
86+
{
87+
struct proc* queu[NPROC];
88+
int size;
89+
90+
} PrioritySchedualing;
91+
92+
typedef struct reversePrio
93+
{
94+
struct proc* queu[NPROC];
95+
int size;
96+
97+
} ReversePrioritySchedualing;

sysproc.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "proc.h"
99

1010
extern int curPolicy; // shows that current scheduling policy (Added)
11+
extern int multilayer;
12+
extern int q;
1113

1214
int
1315
sys_fork(void)
@@ -175,11 +177,12 @@ sys_wait2(void)
175177
return wait2(times);
176178
}
177179

180+
// this function add current process to given queue (Added)
178181
int
179182
sys_enQueue(void)
180183
{
181-
int pid;
182-
if(argint(0, &pid) < 0)
184+
int queue;
185+
if(argint(0, &queue) < 0)
183186
return -1;
184-
return enQueue(pid);
187+
return enQueue(queue);
185188
}

trap.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,27 @@ trap(struct trapframe *tf)
107107
tf->trapno == T_IRQ0+IRQ_TIMER)
108108
{ // (Modified)
109109
if (curPolicy == 0 || curPolicy == 1 || curPolicy == 2)
110-
{
110+
{
111+
if (multilayer == 1){
112+
curPolicy = 4;
113+
if (q == 3){
114+
q = 0;
115+
} else {
116+
q++;
117+
}
118+
}
111119
yield();
112120
}
113121
else if (curPolicy == 3 && ticks % QUANTUM == 0) // for Round-Robin (Added)
114122
{
123+
if (multilayer == 1){
124+
curPolicy = 4;
125+
if (q == 3){
126+
q = 0;
127+
} else {
128+
q++;
129+
}
130+
}
115131
yield();
116132
}
117133
}

types.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ typedef unsigned int uint;
22
typedef unsigned short ushort;
33
typedef unsigned char uchar;
44
typedef uint pde_t;
5+
6+
// Struct (Added)
57
typedef struct times
68
{
7-
/* data */
8-
int creationTime; // allocation time
9-
int runningTime; // RUNNING state
10-
int readyTime; // RUNNABLE state
11-
int sleepingTime; // SLEEPING state
12-
} Times;
9+
int creationTime; // allocation time (Added)
10+
int runningTime; // RUNNING state (Added)
11+
int readyTime; // RUNNABLE state (Added)
12+
int sleepingTime; // SLEEPING state (Added)
13+
} Times;

user.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int getPriority(void); // (Added)
3131
int changePolicy(int policy); // (Added)
3232
int getPolicy(void); // (Added)
3333
int wait2(Times *times); // (Added)
34-
int enQueue(int pid); // (Added)
34+
int enQueue(int queue); // (Added)
3535

3636
// ulib.c
3737
int stat(const char*, struct stat*);

0 commit comments

Comments
 (0)