--- 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 12:41:56 1.18 @@ -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.18 2006/07/02 12:41:56 joko Exp $ #include #include @@ -15,7 +15,8 @@ #define YSIZE 500 #include "algorithm.h" -#define VERBOSE_HANDLE "stderr"; +#define MAX_COMMAND_LINE 65536 +#define MMAP_NAME "bmp_fractal" BOOL VERBOSE = FALSE; @@ -51,12 +52,12 @@ 0, NULL )) { - fprintf(stdout, "%s: %s\n", msg, lpMsgBuf); + fprintf(stderr, "%s: %s\n", msg, lpMsgBuf); LocalFree(lpMsgBuf); } else { - fprintf(stdout, "Error at FormatMesage: %d\n",err=GetLastError()); + fprintf(stderr, "Error at FormatMesage: %d\n",err=GetLastError()); } exit(err); } @@ -70,7 +71,7 @@ // open file handle fd = fopen(filename, "wb+"); if (NULL == fd) { - perror("open"); + perror("Error while opening file for writing"); exit(1); } @@ -79,7 +80,7 @@ // error checking if (-1 == len || len != sizeof(header)) { - perror("write"); + perror("Error while writing header to file"); exit(2); } @@ -88,13 +89,16 @@ for (i = 0; i < img_size; i++) { len = fwrite("\0\0\0", 1, 3, fd); if (-1 == len || len != 3) { - perror("write"); + perror("Error while writing data to file"); exit(4); } } // close file handle - fclose(fd); + if (fclose(fd) != 0) { + perror("Error while closing file handle"); + exit(1); + } } @@ -117,8 +121,6 @@ int x, y; char bgr[3]; - thread_id = GetCurrentThreadId(); - // get worker arguments args = (PWORKERARGS)lpParam; @@ -127,6 +129,7 @@ // debugging if (VERBOSE) { + thread_id = GetCurrentThreadId(); fprintf(stdout, "----------------------------------------------\n"); fprintf(stdout, "thread_id: %i\n", thread_id); fprintf(stdout, "arg.start_row: %i\n", args->start_row); @@ -138,6 +141,8 @@ for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { //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++) { + + // call to function in static linked library getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); // transfer color values to current pixel @@ -158,6 +163,26 @@ } +BOOL scan_argv(int argc, char *argv[], char opt_name[], char *opt_value) { + int i; + char * opt_current_name; + char * opt_current_value; + + //printf("searching for: '%s'\n", opt_name); + + for (i = 1; i < argc; i++) { + opt_current_name = argv[i]; + if (strcmp(opt_current_name, opt_name) == 0) { + opt_current_value = argv[i+1]; + if (opt_current_value != NULL) { + strcpy(opt_value, opt_current_value); + } + return TRUE; + } + } + return FALSE; +} + int main(int argc, char *argv[]) { @@ -168,66 +193,114 @@ unsigned char *pDataBitmap; // workers - int workers = 5; + int workers; int worker_index, worker_rows, worker_startrow; HANDLE *worker_handles; PWORKERARGS worker_args; + int worker_count; // threads or processes? - BOOL use_processes = TRUE; - BOOL is_worker = FALSE; + BOOL use_processes = FALSE; + BOOL is_worker_process = FALSE; // information for creating processes STARTUPINFO si; PROCESS_INFORMATION pi; - char szCmdline[1024]; + char szCmdline[MAX_COMMAND_LINE]; + + // command line stuff + char arg_option[1024]; + char arg_value[1024]; + char *bmp_filename; + char *verbose_option = ""; + + + // check command line arguments + if (argc < 2) { + fprintf(stderr, "Error: Can not run without arguments!\nPlease specify '-t {number of threads}' or '-p {number of processes}' and an image filename.\n"); + exit(EXIT_FAILURE); + } + + // parse command line arguments + if (scan_argv(argc, argv, "--verbose", arg_value)) { + VERBOSE = TRUE; + } - VERBOSE = TRUE; + if (scan_argv(argc, argv, "--worker", arg_value)) { + use_processes = TRUE; + is_worker_process = TRUE; - // "parse" command line arguments - if (argc >= 2) { - if (strcmp(argv[1], "--worker") == 0) { - is_worker = TRUE; + } else if (scan_argv(argc, argv, "-p", arg_value)) { + if (strlen(arg_value) == 0) { + fprintf(stderr, "Error: Please specify number of processes!\n"); + exit(EXIT_FAILURE); } + use_processes = TRUE; + is_worker_process = FALSE; + workers = atoi(arg_value); + + } else if (scan_argv(argc, argv, "-t", arg_value)) { + + if (strlen(arg_value) == 0) + fprintf(stderr, "Error: Please specify number of threads!\n"), + exit(EXIT_FAILURE); + + use_processes = FALSE; + is_worker_process = FALSE; + workers = atoi(arg_value); + } + + if (!is_worker_process && workers < 1) + fprintf(stderr, "Error: Number of threads/processes must be >0!\n"), + exit(EXIT_FAILURE); 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) - write_blank_file("test.bmp"); - if (!is_worker) { + + // master creates memory mapped file ("empty" image) + if (!is_worker_process) { + + if (argc < 4) { + fprintf(stderr, "Error: Must give filename of image as third argument!\n"); + exit(EXIT_FAILURE); + } - /* open file for reading and writing */ - hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + bmp_filename = argv[3]; + + // create empty bmp-file (black background) + write_blank_file(bmp_filename); + + // open file for reading and writing + hFile = CreateFile(bmp_filename, 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 */ - hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); + // create the file mapping object + hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, MMAP_NAME); if (NULL == hMap) { printErrorAndExit("Error at CreateFileMapping", GetLastError()); } - + + // worker uses existing memory mapped file } else { // open existing mapping object - hMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, "bmp_fractal"); + hMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, MMAP_NAME); if (NULL == hMap) 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 +317,7 @@ } - if (use_processes && is_worker) { + if (use_processes && is_worker_process) { if (VERBOSE) fprintf(stdout, "inside worker-process\n"); @@ -255,7 +328,7 @@ // allocate memory for one worker's arguments if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL) - perror("malloc"), exit(1); + perror("Error while allocating memory for worker arguments via malloc"), exit(1); // assign worker's arguments worker_args[0].start_row = worker_startrow; @@ -263,8 +336,12 @@ worker_args[0].pBitmap = pDataBitmap; fractal_create_segment(&worker_args[0]); + + // cleanup mmap-handle + if (!CloseHandle(hMap)) + printErrorAndExit("Error at CloseHandle", GetLastError()); - return 0; + return 0; } /* @@ -284,11 +361,11 @@ // allocate memory for table of all worker handles if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL) - perror("malloc"), exit(1); + perror("Error while allocating memory for worker handles via malloc"), exit(1); // allocate memory for table of all worker arguments if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL) - perror("malloc"), exit(1); + perror("Error while allocating memory for worker arguments via malloc"), exit(1); // calculate bitmap segment length for workers @@ -334,9 +411,13 @@ } else { - _snprintf(szCmdline, 1023, "%s %s %i %i", argv[0], "--worker", worker_startrow, worker_rows); + if (VERBOSE) + verbose_option = "--verbose"; + _snprintf(szCmdline, MAX_COMMAND_LINE - 1, "%s %s %i %i %s", argv[0], "--worker", worker_startrow, worker_rows, verbose_option); if (VERBOSE) fprintf(stdout, "starting worker process: %s\n", szCmdline); + + // prepare data structures for CreateProcess ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); @@ -361,38 +442,37 @@ } - // wait for all threads if (VERBOSE) fprintf(stdout, "waiting for workers to finish...\n"); - if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) - perror("WaitForMultipleObjects"); + + // wait for all workers + for (worker_index = 0; worker_index < workers; worker_index += MAXIMUM_WAIT_OBJECTS) { + worker_count = ((workers - worker_index) > MAXIMUM_WAIT_OBJECTS) ? MAXIMUM_WAIT_OBJECTS : (workers - worker_index); + if (WaitForMultipleObjects(worker_count, &worker_handles[worker_index], TRUE, INFINITE) == WAIT_FAILED) + printErrorAndExit("Error at WaitForMultipleObjects", GetLastError()); + } // debugging: just run single thread //if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) // perror("WaitForSingleObject"); // close all worker handles - for (worker_index = 0; worker_index < workers; worker_index++) - CloseHandle(worker_handles[worker_index]); - - /* write the result into the file */ - if (!FlushViewOfFile(pData, 0)) { - err = GetLastError(); - printErrorAndExit("Error at UnmapViewOfFile", err); - } - - /* remove the mapped file */ - if (!UnmapViewOfFile(pData)) { - err = GetLastError(); - printErrorAndExit("Error at UnmapViewOfFile", err); - exit(err); - } - - /* cleanup handles */ - if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { - err = GetLastError(); - printErrorAndExit("Error at CloseHandle", err); + for (worker_index = 0; worker_index < workers; worker_index++) { + if (!CloseHandle(worker_handles[worker_index])) + printErrorAndExit("Error at CloseHandle for worker handles", GetLastError()); } + + // write the result into the file + if (!FlushViewOfFile(pData, 0)) + printErrorAndExit("Error at UnmapViewOfFile", GetLastError()); + + // remove the mapped file + if (!UnmapViewOfFile(pData)) + printErrorAndExit("Error at UnmapViewOfFile", GetLastError()); + + // cleanup handles + if (!CloseHandle(hMap) || !CloseHandle(hFile) ) + printErrorAndExit("Error at CloseHandle for memory mapped file", GetLastError()); free(worker_args); free(worker_handles);