-
Notifications
You must be signed in to change notification settings - Fork 0
/
SJF.c
94 lines (75 loc) · 2.01 KB
/
SJF.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
#define _GNU_SOURCE
#include <signal.h>
#include <sys/wait.h>
#include "library/common.h"
#include "library/merge_sort.h"
#include "library/priority.h"
#include "library/heap.h"
char N[MAX_PROCESS_NUM][PROCESS_NAME];
unsigned int R[MAX_PROCESS_NUM], T[MAX_PROCESS_NUM];
extern int nProcess;
extern int idProcess[MAX_PROCESS_NUM];
extern pid_t pid[MAX_PROCESS_NUM];
extern int ptr_Ready;
extern int ptr_Run;
extern int run;
struct Node *heap_root = NULL;
void sig_child(int signum)
{
static int nFinish = 0;
wait(NULL);
nFinish++;
ptr_Run++;
run = 0;
if (nFinish == nProcess)
exit(0);
}
void increase_next_process_priority()
{
int next_Run = heap_root->index;
SET_PRIORITY(pid[next_Run], SCHED_FIFO, PRIORITY_INIT);
}
void decrease_next_process_priority()
{
if (!run || heap_root == NULL)
return;
int next_Run = heap_root->index;
SET_PRIORITY(pid[next_Run], SCHED_FIFO, PRIORITY_LOW);
}
void run_next_process()
{
int next_Run = heap_root->index;
heap_root = pop_heap(heap_root);
SET_PRIORITY(pid[next_Run], SCHED_FIFO, PRIORITY_HIGH);
run = 1;
}
void change_priority()
{
if (!run && heap_root != NULL)
run_next_process();
if (run && heap_root != NULL)
increase_next_process_priority();
}
int main (void)
{
READ_INPUT();
for(int i = 0; i < nProcess; ++i)
idProcess[i] = i;
sort(nProcess, idProcess, T);
sort(nProcess, idProcess, R);
SIGACT_SIGCHILD();
int next_Ready = idProcess[ptr_Ready];
for (int t = 0, i = nProcess; i > 0; t++)
{
change_priority();
while (ptr_Ready < nProcess && t == R[next_Ready])
{
decrease_next_process_priority();
create_process(&pid[next_Ready], N[next_Ready], next_Ready, T[next_Ready]);
heap_root = insert_heap(heap_root, T[next_Ready], next_Ready);
next_Ready = idProcess[ptr_Ready++];
change_priority();
}
unit_time();
}
}