-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
189 lines (155 loc) · 4.25 KB
/
main.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
184
185
186
187
188
189
#define _GNU_SOURCE
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<sched.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/syscall.h>
#include<sys/wait.h>
#include<signal.h>
#define TURE 1
typedef struct PROGRAM{
char name[10];
pid_t pid;
int ready;
int execute;
}Program;
Program program[20];
static int time, running, numfin, last_switch, numpro;
char type[10];
void unit_time(){
volatile unsigned long i;
for(i = 0; i < 1000000UL; i++);
}
int compare(const void *A, const void *B){
Program a = *(Program *)A;
Program b = *(Program *)B;
return (a.ready-b.ready);
}
void cpu_assign(int pid, int core){
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(core, &mask);
if(sched_setaffinity(pid, sizeof(mask), &mask) < 0){
printf("sched_setaffinity fault\n");
exit(1);
}
return;
}
int pro_execute(Program program){
int pid = fork();
if(pid < 0){
printf("fork error\n");
return -1;
}
if(pid == 0){
unsigned long start_s, start_ns, end_s, end_ns;
char string[200];
syscall(334, &start_s, &start_ns);
for(int i = 0; i < program.execute; i++){
unit_time();
}
syscall(334, &end_s, &end_ns);
sprintf(string, "[Project1] %d %lu.%09lu %lu.%09lu\n", getpid(), start_s, start_ns, end_s, end_ns);
syscall(333, string);
exit(0);
}
cpu_assign(pid, 1);
return pid;
}
int demote(int pid){
struct sched_param param;
param.sched_priority = 0;
int ret = sched_setscheduler(pid, SCHED_IDLE, ¶m);
return ret;
}
int promote(int pid){
struct sched_param param;
param.sched_priority = 0;
int ret = sched_setscheduler(pid, SCHED_OTHER, ¶m);
return ret;
}
int next_program(){
if(running != -1 && (type[0] == 'S' || type[0] == 'F'))
return running;
int ret = -1;
if(type[0] == 'P' || type[0] == 'S'){
for(int i = 0; i < numpro; i++){
if(program[i].pid == -1 || program[i].execute == 0)
continue;
if(ret == -1 || program[i].execute < program[ret].execute)
ret = i;
}
}else if(type[0] == 'F'){
for(int i = 0; i < numpro; i++){
if(program[i].pid == -1 || program[i].execute == 0)
continue;
if(ret == -1 || program[i].ready < program[ret].ready)
ret = i;
}
}else if(type[0] == 'R'){
if(running == -1){
for(int i = 0; i < numpro; i++){
if(program[i].pid != -1 && program[i].execute > 0){
ret = i;
break;
}
}
}else if((time - last_switch) % 500 == 0){
ret = (running + 1) % numpro;
while(program[ret].pid == -1 || program[ret].execute == 0)
ret = (ret + 1) % numpro;
}else{
ret = running;
}
}
return ret;
}
int scheduling(){
cpu_assign(getpid(), 0);
promote(getpid());
time = 0;
running = -1;
numfin = 0;
while(TURE){
if(running != -1 && program[running].execute == 0){
waitpid(program[running].pid, NULL, 0);
printf("%s %d\n", program[running].name, program[running].pid);
running = -1;
numfin++;
if(numfin == numpro)
break;
}
for(int i = 0; i < numpro; i++){
if(program[i].ready == time){
program[i].pid = pro_execute(program[i]);
demote(program[i].pid);
}
}
int next = next_program();
if(next != -1){
if(running != next){
promote(program[next].pid);
demote(program[running].pid);
running = next;
last_switch = time;
}
}
unit_time();
if(running != -1)
program[running].execute--;
time++;
}
}
int main(){
scanf("%s%d", type, &numpro);
for(int i = 0; i < numpro; i++){
scanf("%s%d%d", program[i].name, &program[i].ready, &program[i].execute);
program[i].pid = -1;
}
qsort(program, numpro, sizeof(Program), compare);
scheduling();
return 0;
}