-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpcb.c
74 lines (62 loc) · 1.52 KB
/
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
#include "pcb.h"
#include <stdio.h>
#include <stdlib.h>
#include "interupt_shims.h"
#include "tables.h"
#define INITIAL_SPSR 0x10
/* Just for reference!
typedef struct pcb_t
{
int pid;
enum { READY, RUNNING, BLOCKED, DEAD } state;
int register[16];
int spsr;
}
PCB;
*/
void create_pcb(PCB *new, int pid, int(*proc)(void))
{
//int i;
new->pid = pid;
new->state = READY;
/*
// No initialization for speed
for (i = 0; i < N_REGS; i++)
new->reg[i] = 0;
*/
new->reg[PC] = (int)proc;
new->spsr = INITIAL_SPSR;
new->reg[LR] = (int)Kill;
new->reg[SP] = (int)&(new->stack[PROCESS_STACK_SIZE-1]); // Descending stack so use the END
new->priority = 1+ (rand() % 5);
}
int add_process(int (*proc)(void))
{
int pid = find_free_pid(pcb_table);
if (pid >= 0)
{
printf("\nINFO: Creating process with PID %d\n", pid);
create_pcb(&pcb_table[pid], pid, proc);
return 0;
}
printf("\nWARN: Couldn't create process for process at 0x%X\n", (int)proc);
return -1;
}
// Replaced with assembly in start.s
void init_pcb_table(void)
{
int i;
for (i = 0; i < PCB_TABLE_SIZE; i++)
pcb_table[i].state = DEAD;
os_state.current_pid = PCB_TABLE_SIZE-1;
}
int find_free_pid(PCB *table)
{
int i;
for (i = 0; i < PCB_TABLE_SIZE; i++)
/* Yes os_state thing is stupid. Blame the arm scatterloader for being stupid */
if (table[i].state == DEAD && i != os_state.current_pid)
return i;
printf("\nWARN: No space for creating any more processes.\n");
return -1;
}