--- joko/Uni/BSArch/04/bmp_fractal.c 2006/07/02 01:51:26 1.10 +++ 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.10 2006/07/02 01:51:26 joko Exp $ +// $Id: bmp_fractal.c,v 1.12 2006/07/02 09:27:14 joko Exp $ #include #include @@ -15,9 +15,7 @@ #define YSIZE 500 #include "algorithm.h" -#define MASTER -1 - -BOOL VERBOSE = TRUE; +BOOL VERBOSE = FALSE; /* BMP Header */ @@ -51,12 +49,12 @@ 0, NULL )) { - fprintf(stderr,"%s : %s\n",msg,lpMsgBuf); + fprintf(stdout, "%s: %s\n", msg, lpMsgBuf); LocalFree(lpMsgBuf); } else { - fprintf(stderr,"Error at FormatMesage: %d\n",err=GetLastError()); + fprintf(stdout, "Error at FormatMesage: %d\n",err=GetLastError()); } exit(err); } @@ -127,16 +125,16 @@ // debugging if (VERBOSE) { - printf("----------------------------------------------\n"); - printf("thread_id: %i\n", thread_id); - printf("arg.start_row: %i\n", args->start_row); - printf("arg.number_of_rows: %i\n", args->number_of_rows); - printf("segment_start: %p\n", pDataBitmapSegment); + fprintf(stdout, "----------------------------------------------\n"); + fprintf(stdout, "thread_id: %i\n", thread_id); + fprintf(stdout, "arg.start_row: %i\n", args->start_row); + fprintf(stdout, "arg.number_of_rows: %i\n", args->number_of_rows); + fprintf(stdout, "segment_start: %p\n", pDataBitmapSegment); } // calculate fractal for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { - //printf("calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); + //fprintf(stdout, "calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); for (x = 0; x < XSIZE; x++) { getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); @@ -153,7 +151,7 @@ } if (VERBOSE) - printf("thread finished: %i\n", thread_id); + fprintf(stdout, "thread finished: %i\n", thread_id); return 0; } @@ -175,7 +173,7 @@ // threads or processes? BOOL use_processes = TRUE; - int worker_id = MASTER; + BOOL is_worker_process = FALSE; // information for creating processes STARTUPINFO si; @@ -183,35 +181,44 @@ char szCmdline[1024]; + VERBOSE = TRUE; + // "parse" command line arguments if (argc >= 2) { - worker_id = atoi(argv[1]); + if (strcmp(argv[1], "--worker") == 0) { + is_worker_process = TRUE; + } } - if (VERBOSE) { - printf("==============================================================\n"); - printf("worker-id: %i\n", worker_id); + if (VERBOSE && use_processes) { + fprintf(stdout, "===================================================== "); + if (is_worker_process) + fprintf(stdout, "WORKER-PROCESS\n"); + else + fprintf(stdout, "MASTER-PROCESS\n"); } // create empty bmp-file (black background) - if (worker_id == MASTER) + if (!is_worker_process) write_blank_file("test.bmp"); - if (worker_id == MASTER) { + // 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 @@ -220,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()); @@ -232,18 +239,17 @@ // debugging if (VERBOSE) { - printf("pos. of file: %p\n", pData); - printf("pos. of bitmap: %p\n", pDataBitmap); + fprintf(stdout, "pos. of file: %p\n", pData); + fprintf(stdout, "pos. of bitmap: %p\n", pDataBitmap); } - if (use_processes && worker_id != MASTER) { + if (use_processes && is_worker_process) { if (VERBOSE) - printf("inside worker-process\n"); - - worker_index = worker_id; + fprintf(stdout, "inside worker-process\n"); + // get segment information from command line worker_startrow = atoi(argv[2]); worker_rows = atoi(argv[3]); @@ -257,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; } @@ -288,7 +298,7 @@ // calculate bitmap segment length for workers worker_rows = YSIZE / workers; if (VERBOSE) - printf("rows for each worker: %i\n", worker_rows); + fprintf(stdout, "rows for each worker: %i\n", worker_rows); // start workers for (worker_index = 0; worker_index < workers; worker_index++) { @@ -304,7 +314,7 @@ if (worker_index == workers - 1) { worker_rows = YSIZE - worker_startrow; if (VERBOSE) - printf("rows for last worker: %i\n", worker_rows); + fprintf(stdout, "rows for last worker: %i\n", worker_rows); } // assign each worker's arguments @@ -328,7 +338,9 @@ } else { - _snprintf(szCmdline, 1023, "%s %i %i %i", argv[0], worker_index + 1, worker_startrow, worker_rows); + _snprintf(szCmdline, 1023, "%s %s %i %i", argv[0], "--worker", worker_startrow, worker_rows); + if (VERBOSE) + fprintf(stdout, "starting worker process: %s\n", szCmdline); ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); @@ -355,7 +367,7 @@ // wait for all threads if (VERBOSE) - printf("waiting for workers to finish...\n"); + fprintf(stdout, "waiting for workers to finish...\n"); if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) perror("WaitForMultipleObjects"); @@ -367,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);