--- joko/Uni/BSArch/04/bmp_fractal.c 2006/07/02 02:12:27 1.11 +++ joko/Uni/BSArch/04/bmp_fractal.c 2006/07/02 09:27:14 1.12 @@ -5,7 +5,7 @@ * Uebung 4.4 */ -// $Id: bmp_fractal.c,v 1.11 2006/07/02 02:12:27 joko Exp $ +// $Id: bmp_fractal.c,v 1.12 2006/07/02 09:27:14 joko Exp $ #include #include @@ -15,8 +15,6 @@ #define YSIZE 500 #include "algorithm.h" -#define VERBOSE_HANDLE "stderr"; - BOOL VERBOSE = FALSE; @@ -175,7 +173,7 @@ // threads or processes? BOOL use_processes = TRUE; - BOOL is_worker = FALSE; + BOOL is_worker_process = FALSE; // information for creating processes STARTUPINFO si; @@ -188,37 +186,39 @@ // "parse" command line arguments if (argc >= 2) { if (strcmp(argv[1], "--worker") == 0) { - is_worker = TRUE; + is_worker_process = TRUE; } } if (VERBOSE && use_processes) { fprintf(stdout, "===================================================== "); - if (is_worker) + if (is_worker_process) fprintf(stdout, "WORKER-PROCESS\n"); else fprintf(stdout, "MASTER-PROCESS\n"); } // create empty bmp-file (black background) - if (!is_worker) + if (!is_worker_process) write_blank_file("test.bmp"); - if (!is_worker) { + // master creates memory mapped file + if (!is_worker_process) { - /* open file for reading and writing */ + // open file for reading and writing hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hFile) { err = GetLastError(); printErrorAndExit("Error at CreateFile",err); } - /* create the file mapping object */ + // create the file mapping object hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); if (NULL == hMap) { printErrorAndExit("Error at CreateFileMapping", GetLastError()); } - + + // worker uses existing memory mapped file } else { // open existing mapping object @@ -227,7 +227,7 @@ printErrorAndExit("Error at OpenFileMapping", GetLastError()); } - /* map the whole file into the process context */ + // map the whole file into the process context pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); if (NULL == pData) { printErrorAndExit("Error at MapViewOfFile", GetLastError()); @@ -244,7 +244,7 @@ } - if (use_processes && is_worker) { + if (use_processes && is_worker_process) { if (VERBOSE) fprintf(stdout, "inside worker-process\n"); @@ -263,6 +263,10 @@ worker_args[0].pBitmap = pDataBitmap; fractal_create_segment(&worker_args[0]); + + // cleanup mmap-handle + if (!CloseHandle(hMap)) + printErrorAndExit("Error at CloseHandle", GetLastError()); return 0; } @@ -375,20 +379,20 @@ for (worker_index = 0; worker_index < workers; worker_index++) CloseHandle(worker_handles[worker_index]); - /* write the result into the file */ + // write the result into the file if (!FlushViewOfFile(pData, 0)) { err = GetLastError(); printErrorAndExit("Error at UnmapViewOfFile", err); } - /* remove the mapped file */ + // remove the mapped file if (!UnmapViewOfFile(pData)) { err = GetLastError(); printErrorAndExit("Error at UnmapViewOfFile", err); exit(err); } - /* cleanup handles */ + // cleanup handles if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { err = GetLastError(); printErrorAndExit("Error at CloseHandle", err);