-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_state.c
64 lines (58 loc) · 2.38 KB
/
init_state.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
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
/* init_state.c: In order to simulate the usage of the Banker's
Algorithm, we must read the initial system state from the user. The
function below implements this process and sets up our "system" to
be analyzed and changed by other routines. Author(s): Originally
written by David Taylor and Terrez Hall. Edited and reorganized by
Jason Franklin. */
/* declare global data structures */
extern int NUMBER_OF_PROCESSES;
extern int NUMBER_OF_RESOURCES;
extern Vector AVAILABLE;
extern Matrix MAX;
extern Matrix ALLOCATION;
extern Matrix NEED;
/* init_state: responsible for reading input from the user and
initializing our simulated system state */
void init_state(void)
{
printf("Banker's Algorithm Simulator!\n");
printf("=============================\n");
printf("Please specify the initial system state.\n");
/* get numbers of processes and resource types */
printf(" General system-wide parameters...\n");
printf(" No. of Processes > ");
scanf("%d", &NUMBER_OF_PROCESSES);
printf(" No. of Resources > ");
scanf("%d", &NUMBER_OF_RESOURCES);
/* allocate memory for shared data structures */
AVAILABLE = vector_create(NUMBER_OF_RESOURCES);
MAX = matrix_create(NUMBER_OF_PROCESSES, NUMBER_OF_RESOURCES);
ALLOCATION = matrix_create(NUMBER_OF_PROCESSES, NUMBER_OF_RESOURCES);
NEED = matrix_create(NUMBER_OF_PROCESSES, NUMBER_OF_RESOURCES);
/* initialize AVAILABLE, MAX, ALLOCATION, and NEED */
int i, j;
printf(" Units available of each resource type...\n");
for (i = 0; i < NUMBER_OF_RESOURCES; i++) {
printf(" Resource type %d > ", i + 1);
scanf("%d", &AVAILABLE[i]);
}
printf(" Maximum resource needs for each process...\n");
for (i = 0; i < NUMBER_OF_PROCESSES; i++) {
for (j = 0; j < NUMBER_OF_RESOURCES; j++) {
printf(" Proc. %d, res. %d > ", i + 1, j + 1);
scanf("%d", &MAX[i][j]);
if (MAX[i][j] > AVAILABLE[j]) {
printf(" ERROR: Max cannot exceed available.\n");
j = j - 1;
continue;
}
ALLOCATION[i][j] = 0;
NEED[i][j] = MAX[i][j];
}
if (i < NUMBER_OF_PROCESSES - 1)
printf(" -------------------\n");
}
}