-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSJF.c
165 lines (127 loc) · 4.19 KB
/
PSJF.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
#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 run;
int nFinish = 0;
int now_time = 0;
int counting_time = 0;
struct Node *heap_root = NULL;
#define BLOCK_SIGCHILD() \
sigset_t set, oset; \
do \
{ \
sigfillset(&set); \
sigprocmask(SIG_BLOCK, &set, &oset); \
}while(0)
#define UNBLOCK_SIGCHILD() sigprocmask(SIG_SETMASK, &oset, NULL)
#define WAIT_CHILD() do \
{ \
BLOCK_SIGCHILD(); \
while(heap_root != NULL && waitpid(pid[heap_root->index], NULL, WNOHANG) != 0) \
{ \
nFinish++; \
heap_root = pop_heap(heap_root); \
run = 0; \
if(nFinish == nProcess) \
exit(0); \
} \
UNBLOCK_SIGCHILD(); \
}while(0)
void sig_child(int signum)
{
wait(NULL);
nFinish++;
heap_root = pop_heap(heap_root);
run = 0;
if(nFinish == nProcess)
exit(0);
}
void increase_next_process_priority()
{
BLOCK_SIGCHILD();
int i1 = heap_root->index;
heap_root = pop_heap(heap_root);
SET_PRIORITY(pid[i1], SCHED_FIFO, PRIORITY_HIGH);
WAIT_CHILD();
if(heap_root)
{
int i2 = heap_root->index;
SET_PRIORITY(pid[i2], SCHED_FIFO, PRIORITY_INIT);
}
heap_root = insert_heap(heap_root, T[i1], i1);
UNBLOCK_SIGCHILD();
}
void decrease_next_two_processes_priority()
{
BLOCK_SIGCHILD();
if(!run || heap_root == NULL)
return;
int i1 = heap_root->index;
heap_root = pop_heap(heap_root);
T[i1] -= counting_time;
WAIT_CHILD();
if(heap_root)
{
int i2 = heap_root->index;
SET_PRIORITY(pid[i2], SCHED_FIFO, PRIORITY_LOW);
}
SET_PRIORITY(pid[i1], SCHED_FIFO, PRIORITY_LOW);
heap_root = insert_heap(heap_root, T[i1], i1);
counting_time = 0;
UNBLOCK_SIGCHILD();
}
void run_next_process()
{
BLOCK_SIGCHILD();
int nextRun = heap_root->index;
SET_PRIORITY(pid[nextRun], SCHED_FIFO, PRIORITY_HIGH);
run = 1;
counting_time = 0;
UNBLOCK_SIGCHILD();
}
void change_priority()
{
WAIT_CHILD();
BLOCK_SIGCHILD();
if(!run && heap_root != NULL)
run_next_process();
if(run && heap_root != NULL)
increase_next_process_priority();
UNBLOCK_SIGCHILD();
}
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;; t++)
{
change_priority();
while(ptr_Ready < nProcess && t == R[next_Ready])
{
decrease_next_two_processes_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();
counting_time++;
now_time++;
WAIT_CHILD();
}
}