-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.c
121 lines (105 loc) · 1.76 KB
/
kernel.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shell.h"
#include "ram.h"
#include "pcb.h"
#include "cpu.h"
struct PCB* head = NULL;
struct PCB* tail = NULL;
void enqueue( struct PCB* thisNode )
{
thisNode->next = NULL ;
tail->next = thisNode ;
tail = thisNode ;
}
struct PCB* dequeue()
{
struct PCB *node = head ;
head = node->next ;
node->next = NULL ;
return node ;
}
void addToReady(struct PCB* thisPCB )
{
if( head == NULL )
{
tail = thisPCB ;
head = thisPCB ;
} else
{
thisPCB->next = NULL ;
tail->next = thisPCB ;
tail = thisPCB ;
}
}
int scheduler()
{
int errorCode = 0;
while(head != NULL)
{
if(!isAvailable)
{
CPU.IP = head->PC ;
int linesLeft = head->end - head->PC + 1 ;
if( linesLeft <= CPU.quanta )
{
errorCode = run( linesLeft ) ;
if(errorCode == 0)
{
removeFromRam(head->start,head->end) ;
head->PC += linesLeft ;
struct PCB* temp = dequeue() ;
free(temp) ;
} else
{
clearRam() ;
return errorCode ;
}
} else
{
errorCode = run( CPU.quanta ) ;
if( errorCode == 0 )
{
head->PC += CPU.quanta ;
struct PCB* temp = copyPCB(head) ;
enqueue(temp) ;
head = head->next ;
} else
{
clearRam() ;
return errorCode ;
}
}
}
}
return errorCode;
}
int myinit(char *fileName)
{
int errorCode = 0 ;
FILE *p = fopen( fileName, "r" ) ;
int start ;
int end ;
if( p == NULL )
{
errorCode = 3 ;
return errorCode ;
}
addToRam( p , &start , &end ) ;
if ( start == -1 || end == -1 )
{
errorCode = 8 ;
return errorCode ;
}
struct PCB* thisPCB = constructorPCB ( start , end ) ;
addToReady ( thisPCB ) ;
return 0 ;
}
int main( void )
{
head = NULL ;
tail = NULL ;
CPU.quanta = 2 ;
shellUI() ;
}