-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process_queue.h
165 lines (147 loc) · 3.2 KB
/
Process_queue.h
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include "PCB.h"
typedef struct Process_queue
{
int maxProcesos;
int tam;
int primero;
int ultimo;
int prioridad;
PCB *pcbs;
} Process_queue;
typedef struct QueuesStruct
{
Process_queue *queues;
int numColas;
} QueuesStruct;
/**************************************************************
***************** INICIALIZACION COLAS ********************
*************************************************************/
Process_queue *crearQueue(int numProcesos, int prioridad)
{
Process_queue *q;
q = (Process_queue *)malloc(sizeof(Process_queue));
q->pcbs = (PCB *)malloc(sizeof(PCB) * numProcesos);
q->tam = 0;
q->maxProcesos = numProcesos;
q->primero = 0;
q->ultimo = -1;
q->prioridad = prioridad;
return q;
}
QueuesStruct *crearQueuesStruct(int prioridades, int numProcesos)
{
QueuesStruct *qs;
qs = (QueuesStruct *)malloc(sizeof(QueuesStruct));
qs->numColas = prioridades;
qs->queues = (Process_queue *)malloc(sizeof(Process_queue) * prioridades);
int i;
for (i = 0; i < prioridades; i++)
{
Process_queue *q = crearQueue(numProcesos, prioridades - i);
qs->queues[i] = *q;
}
return qs;
}
/**************************************************************
***************** FUNCIONES AUXILIARES ********************
*************************************************************/
int colaVacia(Process_queue *q)
{
if (q->tam == 0)
{
return 0;
}
else
{
return 1;
}
}
int quitarPrimero(Process_queue *q)
{
if (q->tam == 0)
{
return 1;
}
else
{
q->tam--;
q->primero++;
return 0;
}
}
void quitarDeCola(QueuesStruct *qs)
{
int i;
for (i = 0; i < qs->numColas; i++)
{
if (quitarPrimero(&qs->queues[i]) == 0)
{
return;
}
}
}
PCB *primero(Process_queue *q)
{
if (q->tam == 0)
{
printf("Cola Vacia\n");
return createEmptyPCB();
}
PCB *pcb = &q->pcbs[q->primero];
return pcb;
}
PCB *primeroEnCola(QueuesStruct *qs)
{
int i;
for (i = 0; i < qs->numColas; i++)
{
if (colaVacia(&qs->queues[i]) == 1)
{
return primero(&qs->queues[i]);
}
}
return createEmptyPCB();
}
void addEnCola(Process_queue *q, PCB *proceso)
{
if (q->tam != q->maxProcesos)
{
q->tam++;
q->ultimo++;
q->pcbs[q->ultimo] = *proceso;
}
return;
}
void addCola(QueuesStruct *qs, PCB *proceso)
{
int i;
for (i = 0; i < qs->numColas; i++)
{
if (proceso->numPrioridad == qs->queues[i].prioridad)
{
addEnCola(&qs->queues[i], proceso);
return;
}
}
}
/**************************************************************
******************** BORRAR COLAS *************************
*************************************************************/
void borrarCola(Process_queue *q)
{
free(q);
}
void borrarColas(QueuesStruct *qs)
{
int i;
for (i = 0; i < qs->numColas; i++)
{
borrarCola(&qs->queues[i]);
}
free(qs);
}