--- 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 02:12:27 1.11 @@ -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.11 2006/07/02 02:12:27 joko Exp $ #include #include @@ -15,9 +15,9 @@ #define YSIZE 500 #include "algorithm.h" -#define MASTER -1 +#define VERBOSE_HANDLE "stderr"; -BOOL VERBOSE = TRUE; +BOOL VERBOSE = FALSE; /* BMP Header */ @@ -51,12 +51,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 +127,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 +153,7 @@ } if (VERBOSE) - printf("thread finished: %i\n", thread_id); + fprintf(stdout, "thread finished: %i\n", thread_id); return 0; } @@ -175,7 +175,7 @@ // threads or processes? BOOL use_processes = TRUE; - int worker_id = MASTER; + BOOL is_worker = FALSE; // information for creating processes STARTUPINFO si; @@ -183,21 +183,28 @@ 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 = TRUE; + } } - if (VERBOSE) { - printf("==============================================================\n"); - printf("worker-id: %i\n", worker_id); + if (VERBOSE && use_processes) { + fprintf(stdout, "===================================================== "); + if (is_worker) + fprintf(stdout, "WORKER-PROCESS\n"); + else + fprintf(stdout, "MASTER-PROCESS\n"); } // create empty bmp-file (black background) - if (worker_id == MASTER) + if (!is_worker) write_blank_file("test.bmp"); - if (worker_id == MASTER) { + if (!is_worker) { /* open file for reading and writing */ hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); @@ -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) { 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]); @@ -288,7 +294,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 +310,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 +334,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 +363,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");