15 |
#define YSIZE 500 |
#define YSIZE 500 |
16 |
#include "algorithm.h" |
#include "algorithm.h" |
17 |
|
|
|
#define VERBOSE_HANDLE "stderr"; |
|
|
|
|
18 |
BOOL VERBOSE = FALSE; |
BOOL VERBOSE = FALSE; |
19 |
|
|
20 |
|
|
173 |
|
|
174 |
// threads or processes? |
// threads or processes? |
175 |
BOOL use_processes = TRUE; |
BOOL use_processes = TRUE; |
176 |
BOOL is_worker = FALSE; |
BOOL is_worker_process = FALSE; |
177 |
|
|
178 |
// information for creating processes |
// information for creating processes |
179 |
STARTUPINFO si; |
STARTUPINFO si; |
186 |
// "parse" command line arguments |
// "parse" command line arguments |
187 |
if (argc >= 2) { |
if (argc >= 2) { |
188 |
if (strcmp(argv[1], "--worker") == 0) { |
if (strcmp(argv[1], "--worker") == 0) { |
189 |
is_worker = TRUE; |
is_worker_process = TRUE; |
190 |
} |
} |
191 |
} |
} |
192 |
|
|
193 |
if (VERBOSE && use_processes) { |
if (VERBOSE && use_processes) { |
194 |
fprintf(stdout, "===================================================== "); |
fprintf(stdout, "===================================================== "); |
195 |
if (is_worker) |
if (is_worker_process) |
196 |
fprintf(stdout, "WORKER-PROCESS\n"); |
fprintf(stdout, "WORKER-PROCESS\n"); |
197 |
else |
else |
198 |
fprintf(stdout, "MASTER-PROCESS\n"); |
fprintf(stdout, "MASTER-PROCESS\n"); |
199 |
} |
} |
200 |
|
|
201 |
// create empty bmp-file (black background) |
// create empty bmp-file (black background) |
202 |
if (!is_worker) |
if (!is_worker_process) |
203 |
write_blank_file("test.bmp"); |
write_blank_file("test.bmp"); |
204 |
|
|
205 |
if (!is_worker) { |
// master creates memory mapped file |
206 |
|
if (!is_worker_process) { |
207 |
|
|
208 |
/* open file for reading and writing */ |
// open file for reading and writing |
209 |
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
210 |
if (INVALID_HANDLE_VALUE == hFile) { |
if (INVALID_HANDLE_VALUE == hFile) { |
211 |
err = GetLastError(); |
err = GetLastError(); |
212 |
printErrorAndExit("Error at CreateFile",err); |
printErrorAndExit("Error at CreateFile",err); |
213 |
} |
} |
214 |
|
|
215 |
/* create the file mapping object */ |
// create the file mapping object |
216 |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
217 |
if (NULL == hMap) { |
if (NULL == hMap) { |
218 |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
219 |
} |
} |
220 |
|
|
221 |
|
// worker uses existing memory mapped file |
222 |
} else { |
} else { |
223 |
|
|
224 |
// open existing mapping object |
// open existing mapping object |
227 |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
228 |
} |
} |
229 |
|
|
230 |
/* map the whole file into the process context */ |
// map the whole file into the process context |
231 |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
232 |
if (NULL == pData) { |
if (NULL == pData) { |
233 |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
244 |
} |
} |
245 |
|
|
246 |
|
|
247 |
if (use_processes && is_worker) { |
if (use_processes && is_worker_process) { |
248 |
|
|
249 |
if (VERBOSE) |
if (VERBOSE) |
250 |
fprintf(stdout, "inside worker-process\n"); |
fprintf(stdout, "inside worker-process\n"); |
263 |
worker_args[0].pBitmap = pDataBitmap; |
worker_args[0].pBitmap = pDataBitmap; |
264 |
|
|
265 |
fractal_create_segment(&worker_args[0]); |
fractal_create_segment(&worker_args[0]); |
266 |
|
|
267 |
|
// cleanup mmap-handle |
268 |
|
if (!CloseHandle(hMap)) |
269 |
|
printErrorAndExit("Error at CloseHandle", GetLastError()); |
270 |
|
|
271 |
return 0; |
return 0; |
272 |
} |
} |
379 |
for (worker_index = 0; worker_index < workers; worker_index++) |
for (worker_index = 0; worker_index < workers; worker_index++) |
380 |
CloseHandle(worker_handles[worker_index]); |
CloseHandle(worker_handles[worker_index]); |
381 |
|
|
382 |
/* write the result into the file */ |
// write the result into the file |
383 |
if (!FlushViewOfFile(pData, 0)) { |
if (!FlushViewOfFile(pData, 0)) { |
384 |
err = GetLastError(); |
err = GetLastError(); |
385 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
386 |
} |
} |
387 |
|
|
388 |
/* remove the mapped file */ |
// remove the mapped file |
389 |
if (!UnmapViewOfFile(pData)) { |
if (!UnmapViewOfFile(pData)) { |
390 |
err = GetLastError(); |
err = GetLastError(); |
391 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
392 |
exit(err); |
exit(err); |
393 |
} |
} |
394 |
|
|
395 |
/* cleanup handles */ |
// cleanup handles |
396 |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
397 |
err = GetLastError(); |
err = GetLastError(); |
398 |
printErrorAndExit("Error at CloseHandle", err); |
printErrorAndExit("Error at CloseHandle", err); |