-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scheduler.c
191 lines (142 loc) · 3.41 KB
/
Scheduler.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
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
/*
* Scheduler.c
*
* Created on: Apr 14, 2018
* Author: ppari
*
* OVerview of the project is that we need to create a circular linked list
* that has a pointer called End and Current
*
*/
#include <stdio.h>
#include <stdbool.h> // allow boolean operations
#include <stdlib.h>
#include <fcntl.h> //files
//global node structure
struct node {
int ExecTime, ProcessID;
struct node* next;
};
//Create a linked list from the data of the test1.txt file. Returns the End
//pointer to the end node
struct node * CreateList();
//this adds a node to the end of the list. The end of the list is defined by
// the end pointer. Recieves to interger values, ExecTime and ProcessID
void Add(int, int);
//scans the linked list every one second in order to find the process that needs
//to be removed from the list. Receives the End pointer to obtain the address
// of the node first node and initiate the scanning process. It removes the
//address of the node that should be removed by making it null.
struct node* ScanList(struct node*);
//removes the process that current is pointing to when the process execution time is
//done.Receives current node pointer. USed by ScanList and returns the reference of
// the recently removed node. returns null on empty list
struct node* RemoveCurrent(struct node*);
//explains itself
bool isEmpty();
void PrintList();
struct node * End = NULL;
struct node * Current = NULL;
int main(){
//printf("%d", sizeof(struct node));
CreateList();
sleep(2);
ScanList(End);
}
bool isEmpty(){
return (End == NULL);
}
void PrintList(){
struct node *temp = End;
printf("\n[");
//if (isEmpty() == false){
/*while(temp->next != Current){
printf("(%d,%d)", temp->ProcessID,temp->ExecTime);
temp=temp->next;
}*/
do{
printf("(%d,%d)", temp->ProcessID,temp->ExecTime);
temp=temp->next;
}while(temp != End);
//}
printf("]");
}
void Add(int exectime, int processID){
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->ProcessID = processID;
temp->ExecTime= exectime;
if(isEmpty()){
End = temp;
Current = End;
End->next = End;
Current = temp;
}else{
temp->next = End->next;
End->next = temp;
End = temp;
}
}
struct node* CreateList(){
FILE * file;
//buffer
char buff[100];
int i = 0, exec, proc;
file = fopen("test1.txt", "r");
//read into buffer
fread(buff,1, 100,file);
fclose(file);
//read file
while(buff[i] != '\0'){
if(buff[i] == 'P'){
//converts to int
proc = buff[i+4] - '0';
exec = buff[i+6] - '0';
Add(exec,proc);
}
i++;
}
return End;
}
struct node* ScanList(struct node* end){
//initiate scanning process from the end node
// decrease exectime by every call of function
PrintList();
while(1){
Current->ExecTime = Current->ExecTime - 1;
if(Current->ExecTime == 1)
RemoveCurrent(Current);
else
Current=Current->next;
if(isEmpty()){
sleep(3);
exit(1);
}
sleep(1);
PrintList();
}
//sleep(5);
return end;
}
struct node* RemoveCurrent(struct node* target){
//reference to removedLink
struct node *removedLink = target;
Current = Current->next;
if(End->next == End){
End = NULL;
return removedLink;
}
else{
//find previous node
struct node* prev = End;
while(prev->next != target)
prev = prev->next;
prev->next=target->next;
//moves the next
if(target == End)
End = End->next;
//this deletes the node
free(target);
return removedLink;
}
return NULL;
}