-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0001-Implement-lottery-scheduling.patch
303 lines (282 loc) · 7.05 KB
/
0001-Implement-lottery-scheduling.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
---
Makefile | 1 +
proc.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++---------------
proc.h | 1 +
syscall.c | 2 ++
syscall.h | 1 +
sysproc.c | 14 +++++++++++
user.h | 1 +
usys.S | 1 +
8 files changed, 84 insertions(+), 20 deletions(-)
diff --git a/Makefile b/Makefile
index 5d91068..7d17a81 100644
--- a/Makefile
+++ b/Makefile
@@ -174,6 +174,7 @@ UPROGS=\
_wc\
_zombie\
_hackbench\
+ _lotterytest\
fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
diff --git a/proc.c b/proc.c
index b122e3b..8d5dfd2 100644
--- a/proc.c
+++ b/proc.c
@@ -25,8 +25,6 @@ void
pinit(void)
{
initlock(&ptable.lock, "ptable");
- // Seed RNG with current time
- sgenrand(unixtime());
}
//PAGEBREAK: 32
@@ -51,18 +49,18 @@ found:
p->state = EMBRYO;
p->pid = nextpid++;
release(&ptable.lock);
-
+ p->tickets = 10;//initialise the tickets
// Allocate kernel stack.
if((p->kstack = kalloc()) == 0){
p->state = UNUSED;
return 0;
}
sp = p->kstack + KSTACKSIZE;
-
+
// Leave room for trap frame.
sp -= sizeof *p->tf;
p->tf = (struct trapframe*)sp;
-
+
// Set up new context to start executing at forkret,
// which returns to trapret.
sp -= 4;
@@ -83,7 +81,7 @@ userinit(void)
{
struct proc *p;
extern char _binary_initcode_start[], _binary_initcode_size[];
-
+
p = allocproc();
initproc = p;
if((p->pgdir = setupkvm()) == 0)
@@ -111,7 +109,7 @@ int
growproc(int n)
{
uint sz;
-
+
sz = proc->sz;
if(n > 0){
if((sz = allocuvm(proc->pgdir, sz, sz + n)) == 0)
@@ -158,14 +156,14 @@ fork(void)
np->cwd = idup(proc->cwd);
safestrcpy(np->name, proc->name, sizeof(proc->name));
-
+
pid = np->pid;
// lock to force the compiler to emit the np->state write last.
acquire(&ptable.lock);
np->state = RUNNABLE;
release(&ptable.lock);
-
+
return pid;
}
@@ -270,19 +268,50 @@ scheduler(void)
{
struct proc *p;
int foundproc = 1;
+ long counters = 0;
+ long total_no_tickets = 0;
+ int g_total = 0; // 1 for True and 0 for False
+ long winner;
+ int lucky_winner = 0;// winning ticket
for(;;){
// Enable interrupts on this processor.
sti();
- if (!foundproc) hlt();
- foundproc = 0;
+ if (!foundproc)
+ hlt();
+ if (g_total == 1) { // when got total is true
+ foundproc = 0;
+ winner = random_at_most(total_no_tickets); // randomly selecting tickets
+ total_no_tickets = 0;
+ counters = 0;
+ lucky_winner = 0;
+ }
+
+ acquire(&ptable.lock); //acquiring lock
- // 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;
+
+ if(p->state != RUNNABLE) { // check if state is RUNNABLE
+ continue;
+ }
+
+ if (g_total == 0) {
+ total_no_tickets += p->tickets;
+ continue;
+ }
+
+ counters += p->tickets; // update the counters
+
+ if (counters < winner) {
+ total_no_tickets += p->tickets; //update total number of tickets if counter is less than winner
+ continue;
+ }
+
+ if (lucky_winner) {
+ total_no_tickets += p->tickets; // update total number of tickets if he is the winner in the random draw
+ continue;
+ }
// Switch to chosen process. It is the process's job
// to release ptable.lock and then reacquire it
@@ -296,9 +325,20 @@ scheduler(void)
// Process is done running for now.
// It should have changed its p->state before coming back.
+
+
+ if (p->state == RUNNABLE) { // if process is still in the state of RUNNABLE
+ total_no_tickets += p->tickets;
+
+ lucky_winner = 1; // winner is found
+ }
+
proc = 0;
+
}
- release(&ptable.lock);
+
+ release(&ptable.lock); // release the lock acquired earlier
+ g_total = 1;
}
}
@@ -344,13 +384,13 @@ forkret(void)
if (first) {
// Some initialization functions must be run in the context
- // of a regular process (e.g., they call sleep), and thus cannot
+ // of a regular process (e.g., they call sleep), and thus cannot
// be run from main().
first = 0;
iinit(ROOTDEV);
initlog(ROOTDEV);
}
-
+
// Return to "caller", actually trapret (see allocproc).
}
@@ -455,7 +495,7 @@ procdump(void)
struct proc *p;
char *state;
uint pc[10];
-
+
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->state == UNUSED)
continue;
@@ -463,12 +503,15 @@ procdump(void)
state = states[p->state];
else
state = "???";
- cprintf("%d %s %s", p->pid, state, p->name);
+ cprintf("%d %s %s %d", p->pid, state, p->name, p->tickets);
if(p->state == SLEEPING){
getcallerpcs((uint*)p->context->ebp+2, pc);
for(i=0; i<10 && pc[i] != 0; i++)
cprintf(" %p", pc[i]);
}
+ // cprintf(" %d", p->tickets);
+ // cprintf(" %d", winning_ticket);
+ // cprintf(" %d", total_tickets);
cprintf("\n");
}
}
diff --git a/proc.h b/proc.h
index 3b9c3ac..8f4e896 100644
--- a/proc.h
+++ b/proc.h
@@ -66,6 +66,7 @@ struct proc {
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
+ int tickets; // Define number of tickets
};
// Process memory is laid out contiguously, low addresses first:
diff --git a/syscall.c b/syscall.c
index 0e06ad4..849877c 100644
--- a/syscall.c
+++ b/syscall.c
@@ -99,6 +99,7 @@ extern int sys_wait(void);
extern int sys_write(void);
extern int sys_uptime(void);
extern int sys_gettime(void);
+extern int sys_settickets(void);
static int (*syscalls[])(void) = {
[SYS_fork] sys_fork,
@@ -123,6 +124,7 @@ static int (*syscalls[])(void) = {
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_gettime] sys_gettime,
+[SYS_settickets] sys_settickets,
};
void
diff --git a/syscall.h b/syscall.h
index 6d6c224..e59bc4a 100644
--- a/syscall.h
+++ b/syscall.h
@@ -21,3 +21,4 @@
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_gettime 22
+#define SYS_settickets 23
diff --git a/sysproc.c b/sysproc.c
index ddaed7c..4a14050 100644
--- a/sysproc.c
+++ b/sysproc.c
@@ -98,3 +98,17 @@ sys_gettime(void) {
cmostime(d);
return 0;
}
+
+//Set number of tickets
+int sys_settickets(void) {
+ int num_tick;
+ if(argint(0, &num_tick) < 0) {
+ proc->tickets = 10; // initialise it to 10
+ }
+ else
+ {
+ proc->tickets = num_tick; // set the process tickets to number of tickets
+ }
+
+ return 0;
+}
diff --git a/user.h b/user.h
index 46d1059..58e344c 100644
--- a/user.h
+++ b/user.h
@@ -24,6 +24,7 @@ char* sbrk(int);
int sleep(int);
int uptime(void);
int gettime(struct rtcdate *);
+int settickets(int);
// ulib.c
int stat(char*, struct stat*);
diff --git a/usys.S b/usys.S
index e556d66..27646a9 100644
--- a/usys.S
+++ b/usys.S
@@ -30,3 +30,4 @@ SYSCALL(sbrk)
SYSCALL(sleep)
SYSCALL(uptime)
SYSCALL(gettime)
+SYSCALL(settickets)
--
1.9.1