| 1 |
joko |
1.1 |
/* Betriebssystem & Middleware |
| 2 |
|
|
* |
| 3 |
|
|
* Betriebssystemarchitektur SS 2006 |
| 4 |
|
|
* |
| 5 |
|
|
* Uebung 4.4 |
| 6 |
|
|
*/ |
| 7 |
joko |
1.3 |
|
| 8 |
joko |
1.6 |
// $Id: bmp_fractal.c,v 1.5 2006/07/01 12:19:46 joko Exp $ |
| 9 |
joko |
1.3 |
|
| 10 |
|
|
#include <windows.h> |
| 11 |
joko |
1.1 |
#include <stdio.h> |
| 12 |
|
|
#include <errno.h> |
| 13 |
|
|
|
| 14 |
|
|
#define XSIZE 500 |
| 15 |
|
|
#define YSIZE 500 |
| 16 |
|
|
#include "algorithm.h" |
| 17 |
|
|
|
| 18 |
joko |
1.3 |
|
| 19 |
joko |
1.2 |
/* BMP Header */ |
| 20 |
joko |
1.1 |
unsigned char header[54]={0x42,0x4d, // signature BM |
| 21 |
|
|
0xe6,0x71,0x0b,0x0, // filesize 750054 |
| 22 |
|
|
0x0,0x0,0x0,0x0, // reserved |
| 23 |
|
|
0x36,0x0,0x0,0x0, // image offset 54 |
| 24 |
|
|
0x28,0x0,0x0,0x0, // size of header follows 40 |
| 25 |
|
|
0xf4,0x1,0x0,0x0, // with of image 500 |
| 26 |
|
|
0xf4,0x1,0x0,0x0, // height of image 500 |
| 27 |
|
|
0x1,0x0, // number of planes 1 |
| 28 |
|
|
0x18,0x0, // number of pixel 24 |
| 29 |
|
|
0x0,0x0,0x0,0x0, // compression |
| 30 |
|
|
0xb0,0x71,0x0b,0x0, // size of image 750000 |
| 31 |
|
|
0x0,0x0,0x0,0x0, // xres |
| 32 |
|
|
0x0,0x0,0x0,0x0, // yres |
| 33 |
|
|
0x0,0x0,0x0,0x0, // number of colortables |
| 34 |
|
|
0x0,0x0,0x0,0x0 // number of important colors |
| 35 |
|
|
}; |
| 36 |
|
|
|
| 37 |
joko |
1.3 |
|
| 38 |
|
|
void printErrorAndExit(const char *msg, DWORD err) { |
| 39 |
|
|
LPSTR lpMsgBuf; |
| 40 |
|
|
if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| |
| 41 |
|
|
FORMAT_MESSAGE_FROM_SYSTEM | |
| 42 |
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 43 |
|
|
NULL, |
| 44 |
|
|
err, |
| 45 |
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
| 46 |
|
|
(LPTSTR) &lpMsgBuf, |
| 47 |
|
|
0, |
| 48 |
|
|
NULL )) |
| 49 |
|
|
{ |
| 50 |
|
|
fprintf(stderr,"%s : %s\n",msg,lpMsgBuf); |
| 51 |
|
|
LocalFree(lpMsgBuf); |
| 52 |
|
|
} |
| 53 |
|
|
else |
| 54 |
|
|
{ |
| 55 |
|
|
fprintf(stderr,"Error at FormatMesage: %d\n",err=GetLastError()); |
| 56 |
|
|
} |
| 57 |
|
|
exit(err); |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
|
| 61 |
|
|
void write_blank_file(char *filename) { |
| 62 |
|
|
|
| 63 |
|
|
FILE *fd; |
| 64 |
|
|
int len, i, img_size; |
| 65 |
|
|
|
| 66 |
|
|
// open file handle |
| 67 |
|
|
fd = fopen(filename, "wb+"); |
| 68 |
|
|
if (NULL == fd) { |
| 69 |
|
|
perror("open"); |
| 70 |
|
|
exit(1); |
| 71 |
|
|
} |
| 72 |
|
|
|
| 73 |
|
|
// write bmp header to file |
| 74 |
|
|
len = fwrite(header, 1, sizeof(header), fd); |
| 75 |
|
|
|
| 76 |
|
|
// error checking |
| 77 |
|
|
if (-1 == len || len != sizeof(header)) { |
| 78 |
|
|
perror("write"); |
| 79 |
|
|
exit(2); |
| 80 |
|
|
} |
| 81 |
|
|
|
| 82 |
|
|
// write three null-bytes for each pixel to file to create a black picture |
| 83 |
|
|
img_size = XSIZE * YSIZE; |
| 84 |
|
|
for (i = 0; i < img_size; i++) { |
| 85 |
|
|
len = fwrite("\0\0\0", 1, 3, fd); |
| 86 |
|
|
if (-1 == len || len != 3) { |
| 87 |
|
|
perror("write"); |
| 88 |
|
|
exit(4); |
| 89 |
joko |
1.1 |
} |
| 90 |
joko |
1.3 |
} |
| 91 |
|
|
|
| 92 |
|
|
// close file handle |
| 93 |
|
|
fclose(fd); |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
|
| 97 |
joko |
1.6 |
// arguments for each thread |
| 98 |
|
|
typedef struct _WorkerArguments { |
| 99 |
|
|
unsigned int start_row; |
| 100 |
|
|
unsigned int number_of_rows; |
| 101 |
|
|
unsigned char * pBitmap; |
| 102 |
|
|
} WORKERARGS, *PWORKERARGS; |
| 103 |
|
|
|
| 104 |
|
|
// worker thread - main entry function |
| 105 |
|
|
DWORD WINAPI fractal_thread (LPVOID lpParam) { |
| 106 |
|
|
|
| 107 |
|
|
// thread stuff |
| 108 |
|
|
int thread_id; |
| 109 |
|
|
PWORKERARGS args; |
| 110 |
|
|
|
| 111 |
|
|
// fractal calculation |
| 112 |
|
|
int x, y; |
| 113 |
|
|
char bgr[3]; |
| 114 |
|
|
|
| 115 |
|
|
thread_id = GetCurrentThreadId(); |
| 116 |
|
|
|
| 117 |
|
|
// get worker arguments |
| 118 |
|
|
args = (PWORKERARGS)lpParam; |
| 119 |
|
|
|
| 120 |
|
|
printf("thread_id: %i\n", thread_id); |
| 121 |
|
|
printf("arg.start_row: %i\n", args->start_row); |
| 122 |
|
|
printf("arg.number_of_rows: %i\n", args->number_of_rows); |
| 123 |
|
|
|
| 124 |
|
|
// calculate fractal |
| 125 |
|
|
for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { |
| 126 |
|
|
for (x = 0; x < XSIZE; x++) { |
| 127 |
|
|
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
| 128 |
|
|
|
| 129 |
|
|
// debugging |
| 130 |
|
|
//printf("pointer: %p\n", pDataBitmapCurrent); |
| 131 |
|
|
|
| 132 |
|
|
// transfer color values to current pixel |
| 133 |
|
|
args->pBitmap[0] = bgr[0]; |
| 134 |
|
|
args->pBitmap[1] = bgr[1]; |
| 135 |
|
|
args->pBitmap[2] = bgr[2]; |
| 136 |
|
|
|
| 137 |
|
|
// move pointer to next pixel |
| 138 |
|
|
args->pBitmap += 3; |
| 139 |
|
|
|
| 140 |
|
|
} |
| 141 |
|
|
//no padding required because 1500%4 =0 |
| 142 |
|
|
} |
| 143 |
|
|
|
| 144 |
|
|
return 0; |
| 145 |
|
|
|
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
|
|
|
| 149 |
joko |
1.3 |
int main(int argc, char *argv[]) { |
| 150 |
|
|
|
| 151 |
joko |
1.4 |
// MMF support |
| 152 |
|
|
DWORD err; |
| 153 |
joko |
1.3 |
HANDLE hMap, hFile; |
| 154 |
|
|
LPVOID pData; |
| 155 |
|
|
unsigned char *pDataBitmap, *pDataBitmapCurrent; |
| 156 |
joko |
1.4 |
|
| 157 |
joko |
1.5 |
// workers |
| 158 |
|
|
unsigned int workers = 3; |
| 159 |
|
|
unsigned int worker_index, worker_rows, worker_startrow; |
| 160 |
joko |
1.6 |
HANDLE *worker_handles; |
| 161 |
|
|
//struct WorkerArguments *worker_args; |
| 162 |
|
|
PWORKERARGS worker_args; |
| 163 |
|
|
|
| 164 |
joko |
1.3 |
|
| 165 |
joko |
1.4 |
// create empty bmp-file (black background) |
| 166 |
joko |
1.3 |
write_blank_file("test.bmp"); |
| 167 |
|
|
|
| 168 |
|
|
/* open file for reading and writing */ |
| 169 |
|
|
hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 170 |
|
|
if (INVALID_HANDLE_VALUE == hFile) { |
| 171 |
|
|
err = GetLastError(); |
| 172 |
|
|
printErrorAndExit("Error at CreateFile",err); |
| 173 |
|
|
} |
| 174 |
|
|
|
| 175 |
|
|
/* create the file mapping object */ |
| 176 |
|
|
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); |
| 177 |
|
|
if (NULL == hMap) { |
| 178 |
|
|
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
| 179 |
|
|
} |
| 180 |
|
|
|
| 181 |
|
|
/* map the whole file into the process context */ |
| 182 |
|
|
pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0); |
| 183 |
|
|
if (NULL == pData) { |
| 184 |
|
|
printErrorAndExit("Error at MapViewOfFile", GetLastError()); |
| 185 |
|
|
} |
| 186 |
|
|
|
| 187 |
|
|
|
| 188 |
|
|
// calculate pointer to beginning of bitmap |
| 189 |
|
|
pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); |
| 190 |
|
|
|
| 191 |
|
|
// debugging |
| 192 |
|
|
printf("pos. of file: %p\n", pData); |
| 193 |
|
|
printf("pos. of bitmap: %p\n", pDataBitmap); |
| 194 |
|
|
|
| 195 |
|
|
// pointer to current pixel |
| 196 |
|
|
pDataBitmapCurrent = pDataBitmap; |
| 197 |
|
|
|
| 198 |
|
|
/* |
| 199 |
|
|
// turn bitmap into white wand |
| 200 |
|
|
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
| 201 |
|
|
//pDataBitmap[offset] = 255; |
| 202 |
|
|
*pDataBitmapCurrent = 255; |
| 203 |
|
|
pDataBitmapCurrent++; |
| 204 |
|
|
} |
| 205 |
|
|
*/ |
| 206 |
|
|
|
| 207 |
joko |
1.6 |
|
| 208 |
|
|
// allocate memory for table of all worker handles |
| 209 |
|
|
if ((worker_handles = malloc(workers * sizeof worker_handles[0])) == NULL) |
| 210 |
|
|
perror("malloc"), exit(1); |
| 211 |
|
|
|
| 212 |
|
|
/* |
| 213 |
|
|
// allocate memory for table of all worker arguments |
| 214 |
|
|
if ((worker_args = malloc(workers * sizeof worker_args[0])) == NULL) |
| 215 |
|
|
perror("malloc"), exit(1); |
| 216 |
|
|
*/ |
| 217 |
|
|
// allocate memory for table of all worker arguments |
| 218 |
|
|
worker_args = (PWORKERARGS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, workers * sizeof(WORKERARGS)); |
| 219 |
|
|
|
| 220 |
|
|
// calculate segments of bitmap for worker threads/processes and start them |
| 221 |
joko |
1.5 |
worker_rows = YSIZE / workers; |
| 222 |
|
|
printf("rows for each worker: %i\n", worker_rows); |
| 223 |
|
|
for (worker_index = 0; worker_index < workers; worker_index++) { |
| 224 |
|
|
|
| 225 |
|
|
// number of row to start for each worker |
| 226 |
|
|
worker_startrow = worker_index * worker_rows; |
| 227 |
|
|
|
| 228 |
|
|
// recalculate number of rows for last worker if (YSIZE mod workers) != 0 |
| 229 |
|
|
if (worker_index == workers - 1) { |
| 230 |
|
|
worker_rows = YSIZE - worker_startrow; |
| 231 |
|
|
printf("rows for last worker: %i\n", worker_rows); |
| 232 |
|
|
} |
| 233 |
joko |
1.6 |
|
| 234 |
|
|
worker_args[worker_index].start_row = worker_startrow; |
| 235 |
|
|
worker_args[worker_index].number_of_rows = worker_rows; |
| 236 |
|
|
worker_args[worker_index].pBitmap = pDataBitmap; |
| 237 |
|
|
|
| 238 |
|
|
worker_handles[worker_index] = CreateThread( |
| 239 |
|
|
NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes, |
| 240 |
|
|
0, // SIZE_T dwStackSize, |
| 241 |
|
|
fractal_thread, // LPTHREAD_START_ROUTINE lpStartAddress, |
| 242 |
|
|
&worker_args[worker_index], // LPVOID lpParameter, |
| 243 |
|
|
0, // DWORD dwCreationFlags, |
| 244 |
|
|
NULL // LPDWORD lpThreadId |
| 245 |
|
|
); |
| 246 |
|
|
|
| 247 |
joko |
1.5 |
} |
| 248 |
|
|
|
| 249 |
joko |
1.6 |
// wait for all threads |
| 250 |
|
|
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
| 251 |
|
|
perror("WaitForMultipleObjects"); |
| 252 |
joko |
1.3 |
|
| 253 |
|
|
/* write the result into the file */ |
| 254 |
|
|
if (!FlushViewOfFile(pData, 0)) { |
| 255 |
|
|
err = GetLastError(); |
| 256 |
|
|
printErrorAndExit("Error at UnmapViewOfFile", err); |
| 257 |
|
|
} |
| 258 |
|
|
|
| 259 |
|
|
/* remove the mapped file */ |
| 260 |
|
|
if (!UnmapViewOfFile(pData)) { |
| 261 |
|
|
err = GetLastError(); |
| 262 |
|
|
printErrorAndExit("Error at UnmapViewOfFile", err); |
| 263 |
|
|
exit(err); |
| 264 |
|
|
} |
| 265 |
|
|
|
| 266 |
|
|
/* cleanup handles */ |
| 267 |
|
|
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
| 268 |
|
|
err = GetLastError(); |
| 269 |
|
|
printErrorAndExit("Error at CloseHandle", err); |
| 270 |
|
|
} |
| 271 |
joko |
1.1 |
|
| 272 |
|
|
} |