Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

to be completed in the C language! please show all your work and provide detailed explanations. finna learn to be completed in the C language

image text in transcribed

to be completed in the C language! please show all your work and provide detailed explanations. finna learn

image text in transcribed

to be completed in the C language please.

image text in transcribed

//HERE IS THE CODE WHICH MUST BE MODIFIED #include  #include  #include  // heap declaration #define _1MB 1048576 unsigned char myheap[_1MB]; #define PAGESIZE 1024 // chunkhead data structure typedef struct chunkhead { unsigned int size; unsigned int info; unsigned char *next, *prev; } chunkhead; // function declarations unsigned char *mymalloc(unsigned int size); void myfree(unsigned char *address); void analyse(); // main void main() { // testing code unsigned char *a, *b, *c; a = mymalloc(1000); b = mymalloc(1000); c = mymalloc(1000); myfree(a); myfree(b); myfree(c); // analysis analyse(); // exiting exit(EXIT_SUCCESS); } // function definitions unsigned char *mymalloc(unsigned int size) { // head of the chunkhead list chunkhead *list = (chunkhead *) myheap; // run only the first time mymalloc is called // to initialize myheap static bool initialized = false; if(!initialized) { *list = (chunkhead) { .size = sizeof(myheap) - sizeof(chunkhead), .info = 0, .next = 0, .prev = 0, }; initialized = true; } // checking list head for size if(list->info == 0) { // memory is free if(size == list->size) { // perfect fit list->info = 1; // marking memory as occupied // returning chunk address return (unsigned char *) list + sizeof(chunkhead); } else if(size + sizeof(chunkhead) size) { // chunkhead of next chunk can fit in // shrinking chunk unsigned int oldSize = list->size; list->size = size; // splitting memory chunkhead *oldNext = (chunkhead *) list->next; chunkhead *newNext = (chunkhead *) list + sizeof(chunkhead) + size; *newNext = (chunkhead) { .size = oldSize - sizeof(chunkhead) - size, .info = 0, .next = (unsigned char *) oldNext, .prev = (unsigned char *) list, }; list->next = (unsigned char *) newNext; if(oldNext != NULL) { oldNext->prev = (unsigned char *) newNext; } // marking memory as occupied list->info = 1; // returning chunk address return (unsigned char *) list + sizeof(chunkhead); } } // set to false since the chunk // has not yet been allocated bool allocated = false; // cannot allocate in head, // find available chunk chunkhead *chunk = (chunkhead *) list->next; // traversing the list while(!allocated && chunk != NULL) { if(chunk->info == 0) { // memory is free if(size == chunk->size) { // perfect fit chunk->info = 1;// marking memory as occupied allocated = true; break; } else if(size + sizeof(chunkhead) size) { // chunkhead of next chunk can fit in // shrinking chunk unsigned int oldSize = chunk->size; chunk->size = size; // splitting memory chunkhead *oldNext = (chunkhead *) chunk->next; chunkhead *newNext = (chunkhead *) chunk + sizeof(chunkhead) + size; *newNext = (chunkhead) { .size = oldSize - sizeof(chunkhead) - size, .info = 0, .next = (unsigned char *) oldNext, .prev = (unsigned char *) chunk, }; chunk->next = (unsigned char *) newNext; if(oldNext != NULL) { oldNext->prev = (unsigned char *) newNext; } // marking memory as occupied chunk->info = 1; allocated = true; break; } } // moving to next chunk chunk = (chunkhead *) chunk->next; } if(!allocated) { // could not be allocated return NULL; } else { // returning chunk address return (unsigned char *) chunk + sizeof(chunkhead); } } void myfree(unsigned char *address) { // NULL address is not freed if(address == NULL) { return; } // getting chunk chunkhead *chunk = (chunkhead *) (address - sizeof(chunkhead)); // freeing chunk chunk->info = 0; if(chunk == (chunkhead *) myheap) { // head chunk return; } // getting previous and next chunks chunkhead *next = (chunkhead *) chunk->next; chunkhead *prev = (chunkhead *) chunk->prev; if(prev != NULL && prev->info == 0 && next != NULL && next->info == 0) { // previous and next chunks are free // so, remove this chunk and link both ends prev->next = (unsigned char *) next; next->prev = (unsigned char *) prev; } } void analyse() { // head of the chunkhead list const chunkhead *list = (chunkhead *) myheap; // traversing the list int chunkNumber = 1; while(list != NULL) { // printing chunk info printf("Chunk #%d ", chunkNumber); printf("Size = %d bytes ", list->size); printf("%s ", list->info ? "Occupied" : "Free"); printf("Next = 0x%08x ", (unsigned int) list->next); printf("Prev = 0x%08x ", (unsigned int) list->prev); // moving to next chunk ++chunkNumber; list = (chunkhead *) list->next; } }
Exercise: Lets speed up working on a file: Read any bitmap file and change its brightness. However, do this in 2 processes to speed up the CPU worktime. Measure the time with and without fork(). Howto: Similar to program 1, read a bitmap (BMP) file into the memory. Allocate your memory with mmap() and don't forget to set the shared memory flag. Fork the process with fork(). Check which process you are. Are you the parent process? Work on the upper half of all pixels. Child? Work on the lower half. Having an odd number of rows? Catch that problem! Measure the time with and without fork() and print the result time! Program call: ./yourprogram [IMAGEFILE) (BRIGHTNESS) (PARALLEL) (OUTPUTFILE) [IMAGEFILE) that's your bitmap [BRIGHTNESS) a number between 0 and 1 [PARALLEL] O for working without fork() and 1 with fork() (OUTPUTFILE] te output file For the brightness: Every color must add BRIGHTNESS * 255, but make sure not to exceed 255! How we test Very simple: We run your code with and without fork(), check the time gained and check the output image. Submission: Submit the source code file, the executable and the bitmap zipped to: fork.zip Write a memory simulation with malloc() and free() Howto: // Have a global array of one MB unsigned char myheap [1048576]; // Have a pagesize #define PAGESIZE 1024 //or 2048, or 4096 This is your heap simulation (yes, its in the uninitialized space, but we are simulating the heap!). Right now, you have all the space free, that means one big chunk of free space. In general, every chunk of space should start with a chunk head: typedef struct chunkhead unsigned int size; unsigned int info; unsigned char *next, *prev; }chunkhead; How much bytes are these? Remember later to add this to new-to-allocate memory! Obviously, you can set the first chunkhead to: size = 1048576 - sizeof(chunkhead); info = 0;//means its free space next = prev = 0; Write a malloc and a free function: unsigned char *mymalloc(unsigned int size); void myfree(unsigned char *address); Also, write a code analysis function: void analyse(); + mymalloc function (unsigned int size) This function should search in your list for a free chunk that is big enough for the requested memory (parameter size). It should return the address of the mem after the chunkhead. Change the chunkhead to be occupied (info=1), the correct size and the correct next chunkhead, in case you need to split the memory. Return NULL (0) if no chunk is big enough. Allow only multiples of PAGESIZE!! myfree function (unsigned char address) This function should sets the given chunk to "free", meaning info=0; if the previous or next chunk is free as well, it should remove the chunk(s) and link both ends together. Don't remove the first one of such a chain though! optional but necessary at program 2 analyse function This function should go through the list of chunks and print for every chunk its content of the structure. E.g.: (this is just a printing example, don't take the numbers serious) Chunk #1: Size = 1024 bytes //mockup number Occupied Next = 0x23423434 //mockup number Prev = 0x00000000 Chunk #2: Size = 1047528 bytes //mockup number Free Next = 0x00000000 Prev = 0x2342ffa0 //mockup number If you have a better style in mind, its fine not to follow this printing style. Just have all information printed! How we test Please prepare your function for testing, by writing a void main with following body: unsigned char *a*b,*c; a = mymalloc(1000); b = mymalloc(1000); c = mymalloc(1000); myfree(b); myfree(a); analyse(); We will test your program with combinations of mymalloc() and myfree() and check with analyse() if everything is as it should. We will try to go to the mem limit! Submission: Submit the source code file to canvas: dma.c. Points? For each working function 20% and additional 40% if everyting works fine

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions