-
Notifications
You must be signed in to change notification settings - Fork 0
/
disastrOS_pcb.c
140 lines (116 loc) · 2.87 KB
/
disastrOS_pcb.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
#include <assert.h>
#include <stdio.h>
#include "pool_allocator.h"
#include "disastrOS_pcb.h"
#include "disastrOS.h"
#include "disastrOS_globals.h"
#include "disastrOS_timer.h"
#define PCB_SIZE sizeof(PCB)
#define PCB_MEMSIZE (sizeof(PCB)+sizeof(int))
#define PCB_BUFFER_SIZE MAX_NUM_PROCESSES*PCB_MEMSIZE
static char _pcb_buffer[PCB_BUFFER_SIZE];
static PoolAllocator _pcb_allocator;
#define PCBPTR_SIZE sizeof(PCBPtr)
#define PCBPTR_MEMSIZE (sizeof(PCBPtr)+sizeof(int))
#define PCBPTR_BUFFER_SIZE MAX_NUM_PROCESSES*PCBPTR_MEMSIZE
#define PCBSEM_SIZE (sizeof(SemaphoreList))
static char _pcb_ptr_buffer[PCBPTR_BUFFER_SIZE];
static PoolAllocator _pcb_ptr_allocator;
void PCB_init(){
int result=PoolAllocator_init(& _pcb_allocator,
PCB_SIZE,
MAX_NUM_PROCESSES,
_pcb_buffer,
PCB_BUFFER_SIZE);
assert(! result);
result=PoolAllocator_init(& _pcb_ptr_allocator,
PCBPTR_SIZE,
MAX_NUM_PROCESSES,
_pcb_ptr_buffer,
PCBPTR_BUFFER_SIZE);
assert(! result);
}
PCB* PCB_alloc() {
PCB* pcb = (PCB*) PoolAllocator_getBlock(&_pcb_allocator);
pcb->list.prev=0;
pcb->list.next=0;
pcb->pid=last_pid; last_pid++;
pcb->return_value=0;
pcb->status=Invalid;
pcb->last_fd=0;
pcb->last_sem_fd=0;
pcb->signals=0;
pcb->signals_mask=0xFFFFFFFF;
pcb->status=Invalid;
List_init(&pcb->descriptors);
List_init(&pcb->sem_descriptors);
pcb->parent=0;
pcb->timer=0;
List_init(&pcb->children);
return pcb;
}
int PCB_free(PCB* pcb){
return PoolAllocator_releaseBlock(&_pcb_allocator, pcb);
}
PCBPtr* PCBPtr_alloc(PCB* pcb) {
PCBPtr* pcb_ptr=(PCBPtr*) PoolAllocator_getBlock(&_pcb_ptr_allocator);
pcb_ptr->list.prev=0;
pcb_ptr->list.next=0;
pcb_ptr->pcb=pcb;
return pcb_ptr;
}
int PCBPtr_free(PCBPtr* pcb_ptr){
return PoolAllocator_releaseBlock(&_pcb_ptr_allocator, pcb_ptr);
}
PCB* PCB_byPID(ListHead* head, int pid){
ListItem* aux=head->first;
while (aux) {
PCB* pcb=(PCB*) aux;
if (pcb->pid==pid)
return pcb;
aux=aux->next;
}
return 0;
}
PCBPtr* PCBPtr_byPID(ListHead* head, int pid){
ListItem* aux=head->first;
while (aux) {
PCBPtr* pcb_ptr=(PCBPtr*) aux;
if (pcb_ptr->pcb->pid==pid)
return pcb_ptr;
aux=aux->next;
}
return 0;
}
/* DEBUG FUNCTIONS */
void PCBPtrList_print(ListHead* head) {
ListItem* aux=head->first;
printf("(");
while(aux){
PCBPtr* pcb_ptr= (PCBPtr*) aux;
printf("%d", pcb_ptr->pcb->pid);
aux=aux->next;
if (aux)
printf(", ");
}
printf(")");
}
void PCB_print(PCB* pcb){
printf("[pid: %d, child: ", pcb->pid);
PCBPtrList_print(&pcb->children);
printf("]");
}
void PCBList_print(ListHead* head) {
ListItem* aux=head->first;
printf("{\n");
while(aux){
printf("\t");
PCB* pcb= (PCB*) aux;
PCB_print(pcb);
aux=aux->next;
if (aux)
printf(",");
printf("\n");
}
printf("}\n");
}