forked from thbighead/SO-trab2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesso.c
More file actions
76 lines (69 loc) · 1.9 KB
/
processo.c
File metadata and controls
76 lines (69 loc) · 1.9 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
#include <stdlib.h>
#include <stdio.h>
#include "processo.h"
PROCESSO* criaProcesso(int pid, int numSeg, int* tamanhos) {
int i;
PROCESSO *temp;
if (!(temp = malloc(sizeof(PROCESSO))))
{
printf("funcao criaProcesso: Erro ao alocar memoria do temp com malloc\n");
exit(-1);
}
temp->pid = pid;
temp->numSeg = numSeg;
if (!(temp->segTable = calloc(numSeg, sizeof(LINHA))))
{
printf("funcao criaProcesso: Erro ao alocar memoria do temp->segTable com calloc\n");
exit(-1);
}
for (i = 0; i < numSeg; ++i)
{
temp->segTable[i].tamanho = tamanhos[i];
temp->segTable[i].bitPresenca = 0;
}
return temp;
}
void insereProcesso(PROCESSO *processo) {
int tamAntigo = tamListaProc;
printf("%d\n", tamListaProc);
listaProc = realloc(listaProc, ++tamListaProc * sizeof(PROCESSO));
if (!listaProc)
{
printf("funcao insereProcesso: Erro ao alocar memoria do temp->segTable com calloc\n");
exit(-1);
}
listaProc[tamAntigo] = *processo;
}
void removeNull() {
int i, j = 0;
int tam = tamListaProc - 1;
PROCESSO *temp;
temp = calloc(tam, sizeof(PROCESSO));
if (!temp)
{
printf("funcao removeNull: Erro ao alocar memoria do temp com calloc\n");
exit(-1);
}
for (i = 0; i < tamListaProc; ++i)
{
if (listaProc[i].pid != -7) {
temp[j] = listaProc[i];
j++;
}
}
listaProc = temp;
}
void retiraProcesso(int pid){
int i;
for (i = 0; i < tamListaProc; ++i)
{
if (listaProc[i].pid == pid){
free(listaProc[i].segTable);
listaProc[i].pid = -7;
removeNull();
tamListaProc--;
return;
}
}
printf("Funcao retiraProcesso: PID %d nao encontrado\n", pid);
}