-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.c
More file actions
131 lines (96 loc) · 2.12 KB
/
scheduler.c
File metadata and controls
131 lines (96 loc) · 2.12 KB
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
/*
** SCCS ID: @(#)scheduler.c 1.1 4/17/15
**
** File: scheduler.c
**
** Author: CSCI-452 class of 20145
**
** Contributor:
**
** Description: Scheduler/dispatcher implementation
*/
#define __SP_KERNEL__
#include "common.h"
#include "scheduler.h"
/*
** PRIVATE DEFINITIONS
*/
/*
** PRIVATE DATA TYPES
*/
/*
** PRIVATE GLOBAL VARIABLES
*/
/*
** PUBLIC GLOBAL VARIABLES
*/
pcb_t *_current; // the currently-running process
queue_t _ready[N_READY]; // the MLQ ready queue structure
/*
** PRIVATE FUNCTIONS
*/
/*
** PUBLIC FUNCTIONS
*/
/*
** _sched_modinit()
**
** initialize the scheduler module
*/
void _sched_modinit( void ) {
// allocate and initialize all the MLQ levels
if( _queue_alloc(_ready,N_READY) != N_READY ) {
_kpanic( "_sched_modinit", "ready queue alloc failed" );
}
for( int i = 0; i < N_READY; ++i ) {
_queue_init( _ready[i], NULL );
}
// no current process, initially
_current = NULL;
// report that we have finished
c_puts( " SCHED" );
}
/*
** _schedule(pcb)
**
** schedule a process for execution according to its priority
*/
void _schedule( pcb_t *pcb ) {
#ifdef DEBUG
if( pcb == NULL ) {
_kpanic( "_schedule", "NULL pcb pointer" );
}
#endif
// ensure the priority value for this process is legal
if( pcb->prio >= N_PRIOS ) {
pcb->prio = PRIO_USER_LOW;
}
// mark this process as ready
pcb->state = STATE_READY;
// add it to the appropriate ready queue level
// c_printf( "*** sched pid %d\n", pcb->pid );
_queue_insert( _ready[pcb->prio], (void *)pcb, 0 );
}
/*
** _dispatch()
**
** give the CPU to a process
*/
void _dispatch( void ) {
// select a process from the highest-priority
// ready queue that is not empty
for( int i = 0; i < N_READY; ++i ) {
if( !_queue_empty(_ready[i]) ) {
_current = (pcb_t *) _queue_remove( _ready[i] );
if( _current == NULL ) {
_kpanic( "_dispatch", "NULL from non-empty ready queue" );
}
_current->state = STATE_RUNNING;
_current->quantum = _current->default_quantum;
// c_printf( "*** dispatch pid %d\n", _current->pid );
return;
}
}
// uh, oh - didn't find one
_kpanic( "_dispatch", "no ready processes!?!?!" );
}