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