-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA1.c
252 lines (223 loc) · 5.88 KB
/
A1.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* Compile with 'gcc A1.c', then run with two arguments.
The first argument being the file where the grid is located.
The second argument being how many threads you want to use.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
/* Global Variables */
int **current_gen;
int N;
int rows;
int start;
/* readGrid, Function that reads the grid from the file and assigns it to the current_gen grid.
FILE *fp, file to read from.
*/
void readGrid(FILE *fp) {
int i, j;
char ch;
system("clear");
printf("%d\n", N);
//Loop through file to assign correct positions to grid
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
ch = fgetc(fp);
//if ch is not new line or blank space, assign
if (ch != '\n' && ch != ' ') {
current_gen[i][j] = ch - '0';
}
//ch is a new line or black space
else {
//get next character
ch = fgetc(fp);
//if ch is not new line or blank space, assign
if (ch != '\n' && ch != ' ') {
current_gen[i][j] = ch - '0';
}
//ch is a new line or blank space
else {
//Get next char and assign
ch = fgetc(fp);
current_gen[i][j] = ch - '0';
}
}
}
}
}
/* checkGrid, Function to handle error handling of grid format.
*/
void checkGrid() {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (current_gen[i][j] != 0 && current_gen[i][j] != 1) {
fprintf(stderr, "Error: Grid from file incorrect format\n");
exit(EXIT_FAILURE);
}
}
}
}
/* initPrintGrid, Function to print out the starting grid in the correct format.
*/
void initPrintGrid() {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%d ", current_gen[i][j]);
}
printf("\n");
}
sleep(1);
}
/* getNext, Function to calculate the next generation and assign it to the current generation grid.
int **current_gen, current grid to base next generation off of.
int N, number of rows or columns.
*/
int getNext() {
int **next_gen;
int i, j, x, y, current_rows;
// Allocate memory for next_gen grid
next_gen = malloc(N * sizeof(int *));
if ((N - start) < rows ){
current_rows = N;
}
else {
current_rows = start + rows;
}
for (i = start; i < current_rows; i++) {
next_gen[i] = malloc(N * sizeof(int));
}
//Loop through each position in grid
for (i = start; i < current_rows; i++) {
for (j = 0; j < N; j++) {
int neighbours = 0;
//Loop through all neighbours for current position
for (x = i - 1; x <= i + 1; x++) {
for (y = j - 1; y <= j + 1; y++) {
//Determines if current position has a neighbour at each expected position
if (current_gen[(x + N) % N][(y + N) % N]) {
neighbours++;
}
}
}
//Current position was incremented if live, therefore have to clean up
if (current_gen[i][j]) {
neighbours--;
}
//Assigns position as live if live neighbours = 3 or if live neighbours = 2 and was previously live
next_gen[i][j] = (neighbours == 3 || (neighbours == 2 && current_gen[i][j]));
}
}
//Assign the calculated next generation grid to the current grid
for (i = start; i < current_rows; i++) {
for (j = 0; j < N; j++) {
current_gen[i][j] = next_gen[i][j];
}
}
//Free space allocated for next_gen
for (i = start; i < current_rows; ++i) {
free(next_gen[i]);
}
free(next_gen);
return 0;
}
/* printGrid, Function to print the grid for each generation in the correct format.
int **grid, the grid to be printed.
int N, number of rows or columns.
*/
void printGrid(int **grid, int N) {
system("clear");
int i, j;
for (i = -1; i <= N; i++) {
printf("-");
}
putchar('\n');
for (i = 0; i < N; i++) {
printf("|");
for (j = 0; j < N; j++) {
//Print x if live, blank space if dead
printf(grid[i][j] ? "x" : " ");
}
printf("|");
printf("\n");
}
for (i = -1; i <= N; i++) {
printf("-");
}
putchar('\n');
sleep(1);
}
/* mainLoop, Function that runs the while loop for the game.
int *stack_top, memory space to use for clone processes
int threads, number of threads to divide the work between.
*/
void mainLoop(int threads) {
const int STACK_SIZE = 65536;
int *stack;
int *stack_top;
int pid, i;
while (1) {
stack = malloc(STACK_SIZE * sizeof(*stack));
if (stack == NULL) {
fprintf(stderr, "malloc");
exit(EXIT_FAILURE);
}
stack_top = stack + STACK_SIZE; /* Assume stack grows downward */
start = 0;
for (i = 1; i <= threads; i++) {
//Clone with flag CLONE_VM to keep same memory space, and CLONE_VFORK to wait until child process is done
pid = clone(getNext, stack_top, CLONE_VM|CLONE_VFORK, NULL);
if (pid == -1) {
exit(EXIT_FAILURE);
}
start = (N / threads) * i;
}
if ((N - start) < rows) {
pid = clone(getNext, stack_top, CLONE_VM|CLONE_VFORK, NULL);
}
printGrid(current_gen, N);
}
}
int main( int argc, char *argv[] ) {
int i, threads;
char str[5];
char ch;
FILE *fp;
//Invalid number of arguments
if (argc != 3) {
fprintf(stderr, "Error: Invalid number of arguments.");
exit(EXIT_FAILURE);
}
else {
char* filename = argv[1];
fp = fopen(filename, "r");
if( fp == NULL ) {
fprintf(stderr, "Error while opening the file.\n");
exit(EXIT_FAILURE);
}
threads = atoi(argv[2]);
fgets(str, 5, fp);
N = atoi(str);
if (N == 0) {
fprintf(stderr, "Error: Incorrect file format");
exit(EXIT_FAILURE);
}
rows = (N / threads);
//Allocate memory for current_gen grid
current_gen = malloc(N * sizeof(int *));
for (i = 0; i < N; i++) {
current_gen[i] = malloc(N * sizeof(int));
}
readGrid(fp);
checkGrid();
initPrintGrid();
fclose(fp);
mainLoop(threads);
}
}