-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCFS.c
77 lines (61 loc) · 1.46 KB
/
FCFS.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
#define _GNU_SOURCE
#include<signal.h>
#include<sys/wait.h>
#include"library/common.h"
#include"library/merge_sort.h"
#include"library/priority.h"
char N[MAX_PROCESS_NUM][PROCESS_NAME];
unsigned int R[MAX_PROCESS_NUM], T[PROCESS_NAME];
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;
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 = idProcess[ptr_Run + 1];
SET_PRIORITY(pid[next_Run], SCHED_FIFO, PRIORITY_INIT);
}
void run_next_process()
{
int next_Run = idProcess[ptr_Run];
SET_PRIORITY(pid[next_Run], SCHED_FIFO, PRIORITY_HIGH);
run = 1;
}
void change_priority() {
if (!run && ptr_Run < ptr_Ready)
run_next_process();
if (run && ptr_Run + 1 < ptr_Ready)
increase_next_process_priority();
}
int main(void)
{
READ_INPUT();
for(int i = 0; i < nProcess; i++)
idProcess[i] = i;
sort(nProcess, idProcess, R);
SIGACT_SIGCHILD();
int nextReady = idProcess[ptr_Ready];
for(int i = nProcess, t = 0; i > 0; t++)
{
change_priority();
while(ptr_Ready < nProcess && t == R[nextReady])
{
create_process(&pid[nextReady], N[nextReady], nextReady, T[nextReady]);
nextReady = idProcess[ptr_Ready++];
change_priority();
}
unit_time();
}
}