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 |
|
|
156 |
|
|
157 |
} |
} |
158 |
|
|
159 |
|
BOOL scan_argv(int argc, char *argv[], char opt_name[], char *opt_value[]) { |
160 |
|
int i; |
161 |
|
char * opt_current_name; |
162 |
|
char * opt_current_value; |
163 |
|
|
164 |
|
//printf("searching for: '%s'\n", opt_name); |
165 |
|
|
166 |
|
for (i = 1; i < argc; i++) { |
167 |
|
opt_current_name = argv[i]; |
168 |
|
if (strcmp(opt_current_name, opt_name) == 0) { |
169 |
|
//printf("opt: %s\n", opt_current_name); |
170 |
|
opt_current_value = argv[i+1]; |
171 |
|
//printf("opt-current-value: %s\n", opt_current_value); |
172 |
|
if (opt_current_value != NULL) { |
173 |
|
//*opt_value = *argv[i+1]; |
174 |
|
*opt_value = *opt_current_value; |
175 |
|
//printf("opt-value: %s\n", opt_value); |
176 |
|
} |
177 |
|
return TRUE; |
178 |
|
} |
179 |
|
} |
180 |
|
return FALSE; |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
int main(int argc, char *argv[]) { |
int main(int argc, char *argv[]) { |
185 |
|
|
196 |
PWORKERARGS worker_args; |
PWORKERARGS worker_args; |
197 |
|
|
198 |
// threads or processes? |
// threads or processes? |
199 |
BOOL use_processes = TRUE; |
BOOL use_processes = FALSE; |
200 |
BOOL is_worker = FALSE; |
BOOL is_worker_process = FALSE; |
201 |
|
|
202 |
// information for creating processes |
// information for creating processes |
203 |
STARTUPINFO si; |
STARTUPINFO si; |
204 |
PROCESS_INFORMATION pi; |
PROCESS_INFORMATION pi; |
205 |
char szCmdline[1024]; |
char szCmdline[65536]; |
206 |
|
|
207 |
|
// command line stuff |
208 |
|
char arg_option[1024]; |
209 |
|
char *arg_value[1024]; |
210 |
|
//arg_value = malloc(1024); |
211 |
|
|
212 |
VERBOSE = TRUE; |
VERBOSE = FALSE; |
213 |
|
|
214 |
// "parse" command line arguments |
// "parse" command line arguments |
215 |
|
/* |
216 |
if (argc >= 2) { |
if (argc >= 2) { |
217 |
if (strcmp(argv[1], "--worker") == 0) { |
if (strcmp(argv[1], "--worker") == 0) { |
218 |
is_worker = TRUE; |
is_worker_process = TRUE; |
219 |
} |
} |
220 |
} |
} |
221 |
|
*/ |
222 |
|
|
223 |
|
//scan_argv(argc, argv, "--worker", &arg_value); |
224 |
|
//*arg_value = '\0'; |
225 |
|
// parse command line arguments |
226 |
|
if (scan_argv(argc, argv, "--worker", arg_value)) { |
227 |
|
use_processes = TRUE; |
228 |
|
is_worker_process = TRUE; |
229 |
|
|
230 |
|
} else if (scan_argv(argc, argv, "-p", arg_value)) { |
231 |
|
use_processes = TRUE; |
232 |
|
is_worker_process = FALSE; |
233 |
|
workers = atoi(arg_value); |
234 |
|
|
235 |
|
} else if (scan_argv(argc, argv, "-t", arg_value)) { |
236 |
|
use_processes = FALSE; |
237 |
|
is_worker_process = FALSE; |
238 |
|
workers = atoi(arg_value); |
239 |
|
|
240 |
|
} |
241 |
|
//printf("value: %s\n", arg_value); |
242 |
|
//exit(0); |
243 |
|
|
244 |
|
|
245 |
if (VERBOSE && use_processes) { |
if (VERBOSE && use_processes) { |
246 |
fprintf(stdout, "===================================================== "); |
fprintf(stdout, "===================================================== "); |
247 |
if (is_worker) |
if (is_worker_process) |
248 |
fprintf(stdout, "WORKER-PROCESS\n"); |
fprintf(stdout, "WORKER-PROCESS\n"); |
249 |
else |
else |
250 |
fprintf(stdout, "MASTER-PROCESS\n"); |
fprintf(stdout, "MASTER-PROCESS\n"); |
251 |
} |
} |
252 |
|
|
253 |
// create empty bmp-file (black background) |
// create empty bmp-file (black background) |
254 |
if (!is_worker) |
if (!is_worker_process) |
255 |
write_blank_file("test.bmp"); |
write_blank_file("test.bmp"); |
256 |
|
|
257 |
if (!is_worker) { |
// master creates memory mapped file |
258 |
|
if (!is_worker_process) { |
259 |
|
|
260 |
/* open file for reading and writing */ |
// open file for reading and writing |
261 |
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); |
262 |
if (INVALID_HANDLE_VALUE == hFile) { |
if (INVALID_HANDLE_VALUE == hFile) { |
263 |
err = GetLastError(); |
err = GetLastError(); |
264 |
printErrorAndExit("Error at CreateFile",err); |
printErrorAndExit("Error at CreateFile",err); |
265 |
} |
} |
266 |
|
|
267 |
/* create the file mapping object */ |
// create the file mapping object |
268 |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
269 |
if (NULL == hMap) { |
if (NULL == hMap) { |
270 |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
271 |
} |
} |
272 |
|
|
273 |
|
// worker uses existing memory mapped file |
274 |
} else { |
} else { |
275 |
|
|
276 |
// open existing mapping object |
// open existing mapping object |
279 |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
280 |
} |
} |
281 |
|
|
282 |
/* map the whole file into the process context */ |
// map the whole file into the process context |
283 |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
284 |
if (NULL == pData) { |
if (NULL == pData) { |
285 |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
296 |
} |
} |
297 |
|
|
298 |
|
|
299 |
if (use_processes && is_worker) { |
if (use_processes && is_worker_process) { |
300 |
|
|
301 |
if (VERBOSE) |
if (VERBOSE) |
302 |
fprintf(stdout, "inside worker-process\n"); |
fprintf(stdout, "inside worker-process\n"); |
315 |
worker_args[0].pBitmap = pDataBitmap; |
worker_args[0].pBitmap = pDataBitmap; |
316 |
|
|
317 |
fractal_create_segment(&worker_args[0]); |
fractal_create_segment(&worker_args[0]); |
318 |
|
|
319 |
|
// cleanup mmap-handle |
320 |
|
if (!CloseHandle(hMap)) |
321 |
|
printErrorAndExit("Error at CloseHandle", GetLastError()); |
322 |
|
|
323 |
return 0; |
return 0; |
324 |
} |
} |
325 |
|
|
326 |
/* |
/* |
431 |
for (worker_index = 0; worker_index < workers; worker_index++) |
for (worker_index = 0; worker_index < workers; worker_index++) |
432 |
CloseHandle(worker_handles[worker_index]); |
CloseHandle(worker_handles[worker_index]); |
433 |
|
|
434 |
/* write the result into the file */ |
// write the result into the file |
435 |
if (!FlushViewOfFile(pData, 0)) { |
if (!FlushViewOfFile(pData, 0)) { |
436 |
err = GetLastError(); |
err = GetLastError(); |
437 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
438 |
} |
} |
439 |
|
|
440 |
/* remove the mapped file */ |
// remove the mapped file |
441 |
if (!UnmapViewOfFile(pData)) { |
if (!UnmapViewOfFile(pData)) { |
442 |
err = GetLastError(); |
err = GetLastError(); |
443 |
printErrorAndExit("Error at UnmapViewOfFile", err); |
printErrorAndExit("Error at UnmapViewOfFile", err); |
444 |
exit(err); |
exit(err); |
445 |
} |
} |
446 |
|
|
447 |
/* cleanup handles */ |
// cleanup handles |
448 |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
449 |
err = GetLastError(); |
err = GetLastError(); |
450 |
printErrorAndExit("Error at CloseHandle", err); |
printErrorAndExit("Error at CloseHandle", err); |