-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
126 lines (113 loc) · 3.33 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
// Arquivo: php.c
// Autor: Roland Teodorowitsch
// Data: 28 ago. 2018
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_RESOURCES 5
#define MAX_TASKS 26
#define MAX_STEPS 20
#define MAX_TIME 1000
#define RUN 0
#define LOCK 1
#define UNLOCK 2
typedef struct {
int p; //prioridade
int a; //tempo de chegada
int ns; //numero de computaçoes
int s[MAX_STEPS][2]; //steps [run, lock, unlock]
char name;
// ...
} TASK;
int main() {
int num_resources, num_tasks, i, j, value;
TASK tasks[MAX_TASKS], orderedTaks[MAX_TASKS];
char word[20];
char result[MAX_RESOURCES+1][MAX_TIME];
int sim_time = -1;
char* provisorio;
int runs;
while (1) {
// LEITURA
//num_resources numero de coisas que podem ser usadas pelas taks
//num_tasks é numero de tasks que vao ter de ser escalonadas
scanf("%d %d", &num_resources, &num_tasks);
if (num_tasks == 0)
break;
for (i=0; i<num_tasks; ++i) {
//prioridade --- tempo de chegada --- numero de computações
scanf("%d %d %d", &(tasks[i].p), &(tasks[i].a), &(tasks[i].ns) );
for (j=0; j<tasks[i].ns; ++j) {
//word é pra
scanf("%s %d", word, &value);
if (strcmp(word,"run")==0 || strcmp(word,"RUN")==0)
tasks[i].s[j][0] = RUN;
else if (strcmp(word,"lock")==0 || strcmp(word,"LOCK")==0)
tasks[i].s[j][0] = LOCK;
else if (strcmp(word,"unlock")==0 || strcmp(word,"UNLOCK")==0)
tasks[i].s[j][0] = UNLOCK;
else
exit(1);
tasks[i].s[j][1] = value;
}
}
//Ordena por tempo de chegada as tasks
memcpy(orderedTaks, reorderTasks(tasks, num_tasks), sizeof(tasks));
//Nomeia de alfabeticamente as tasks
for(i=0;i<sizeof(tasks); i++){
tasks[i].name = 'A' + i;
}
//Começa simulação sem os a atribuição de requisitos
for(i=0;i<num_tasks; i++){
if(sim_time < orderedTaks[i].a){
for(j=0; j<orderedTaks[i].ns; j++){
if(orderedTaks[i].s[j][0] == "RUN"){
for(runs =0; runs<orderedTaks[i].s[j][1];runs++){
provisorio = orderedTaks[i].name;
sim_time++;
}
}
}
}
}
// // provisorio:
// sim_time = 20;
// for (i=0; i<num_resources+1; ++i) {
// for (j=0; j<sim_time; ++j) {
// result[i][j] = '.';
// }
// }
// // fim do provisorio.
// MOSTRAR RESULTADOS
for (i=0; i<num_resources+1; ++i) {
for (j=0; j<sim_time; ++j) {
putchar(result[i][j]);
}
putchar('\n');
}
putchar('\n');
}
return 0;
}
TASK * setTasksName(TASK tasks[]){
int i;
for(i=0;i<sizeof(tasks); i++){
tasks[i].name = 'A' + i;
}
return tasks;
}
TASK * reorderTasks(TASK tasks[], int numTasks){
int i =0;
TASK orderedTasks[numTasks];
for(i=0; i<numTasks; i++){
if(sizeof(orderedTasks)==0){
orderedTasks[i] = tasks[i];
}
if(tasks[i].a < orderedTasks[i].a){
orderedTasks[i+1] = orderedTasks[i];
orderedTasks[i] = tasks[i];
}
else orderedTasks[i] = tasks[i];
}
return orderedTasks;
}