Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
200 lines (160 loc) · 5.83 KB
/
Copy pathmain.c
File metadata and controls
200 lines (160 loc) · 5.83 KB
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
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include "queue.h"
#include "proc.h"
#include "proc_init.h"
#include "sched.h"
#include "utils.h"
#include "thread.h"
#include "proc_time.h"
#include "verbose.h"
// Número de processos
int NPROC = 10;
// Tempo máximo da execução total de um processo (microsegundos)
int MAX_TIME = 100;
// Tempo máximo da execução de um processo por entrada na CPU (microsegundos)
int QUANTUM = 20;
// Habilita ou desabilita o modo VERBOSE (DEBUG)
int VERBOSE = 0;
// Opção de fixar o valor da semente
int SEED = -1;
// Valor que define a chance de haver uma interrupção
double INTERRUPT_PROB = 0.5;
// Identificador da thread do scheduler
pthread_t sched_tid;
// Ponteiro para o processo que está rodando atualmente
struct proc * running = NULL;
// Ponteiros para as filas
struct queue * ready; // fila de aptos
struct queue * ready2; // segunda fila de aptos
struct queue * blocked; // fila de bloqueados
// NOTE: usado apenas para contabilização final
struct queue * finished; // fila de processos finalizados
// Global buffer para imprimir debug
char gbuffer[100];
// counter for the number of events of the system
int event_num = 1;
// Variaveis utilizadas para definir o conjunto de signals que as threads dos
// processos irão aceitar
int snum;
sigset_t set;
int main (int argc, char *argv[])
{
// tratando os parametros da linha de comando
int opt;
while ((opt = getopt(argc, argv, "n:q:s:vh")) != -1) {
switch (opt) {
// Imprimindo uma mensagem de ajuda (help)
case 'h':
printf("Ajuda:\n");
printf("\t-n NPROC: Numero de processos.\n");
printf("\t\tValido se NPROC >= 1. Default: NPROC = 10\n");
printf("\t-q QUANTUM: Valor do QUANTUM.\n");
printf("\t\tValido se QUANTUM >= 2. Default: QUANTUM = 20\n");
printf("\t-s SEED: Valor fixo para a semente aleatória.\n");
printf("\t\tValido se SEED != 1. Default: SEED = time(NULL)+PID\n");
printf("\t-v: Habilita o modo 'verbose', para debug.\n");
printf("\t-h: Exibe esta ajuda.\n");
exit(0);
break;
// Obtem o número de processos NPROC informado na linha de comando
case 'n':
NPROC = atoi(optarg);
// Verificando sua validade
if( NPROC < 1)
{
fprintf(stderr, "Informe um valor válido para os processos [NPROC >= 1]\n");
exit(1);
}
break;
// Obtem o valor do QUANTUM informado na linha de comando
case 'q':
QUANTUM = atoi(optarg);
if( QUANTUM < 2)
{
fprintf(stderr, "Informe um valor válido para o QUANTUM [QUANTUM >= 2]\n");
exit(2);
}
break;
// Fixando a SEED do srand
case 's':
SEED = atoi(optarg);
if( SEED == -1)
{
fprintf(stderr, "Informe um valor válido para o SEED [SEED != -1]\n");
exit(3);
}
break;
// Modo 'verbose' para debug ativado
case 'v':
VERBOSE = 1;
break;
case '?':
fprintf(stderr, "Erro de parametros!\n");
fprintf(stderr, "Uso correto: %s [-n NPROC] [-q QUANTUM] [-v] [-h]\n",
argv[0]);
exit(4);
default:
break;
}
}
// Parameters
vprint("%s MAIN - Numero de PROCESSOS %d\n", event(), NPROC);
vprint("%s MAIN - Valor do QUANTUM: %d\n", event(), QUANTUM);
// definindo o conjunto de signals que as threads dos processos irão tratar
sigemptyset(&set);
if(sigaddset(&set, SIGUSR1) == -1)
{
perror("Sigaddset error");
pthread_exit((void *)1);
}
if(sigaddset(&set, SIGUSR2) == -1)
{
perror("Sigaddset error");
pthread_exit((void *)1);
}
// NOTE: Marlison's FIX
// blocking signals for all child threads (inheriting), only listen to
// signals with sigwait, and the SO treats the sent signals as PENDING,
// until the thread calls sigwait, not losing the signals sent before the
// sigwait call
if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
perror("pthread_sigmask error");
exit(3);
}
// Iniciando a semente do random
if (SEED == -1)
srand(time(NULL)+(int)getpid());
else
srand(SEED);
vprint("%s MAIN - Iniciando filas\n", event());
// Inicializando as filas
ready = initqueue(ready);
ready2 = initqueue(ready2);
blocked = initqueue(blocked);
finished = initqueue(finished);
vprint("%s MAIN - Iniciando os processos\n", event());
// Inicia os processos, inserindo-os na fila de aptos
// NOTE: a fila é selecionada internamente à função, a partir das variaveis globais
proc_init();
vprint("%s MAIN - imprimindo fila 'ready':\n", event());
if (VERBOSE)
printqueue(ready);
// printf("main: fila blocked:\n");
// printqueue(blocked);
// printproc(ready->head->next->next);
vprint("%s MAIN - Iniciando o escalonador\n", event());
// call scheduler
start_scheduler();
// printqueue(finished);
// printf("%s MAIN - Liberando memória utilizada\n", event());
// Finalizando os processos
procend(finished);
// printf("%s MAIN - Finalizando o simulador\n", event());
return 0;
}