/* ------------------------------------------------------------ ____ ________ / _/__ / __/ __/ _/ // _ \/ _/_\ \ /___/_//_/___/___/ Institute of Embedded Systems Zurich University of Applied Sciences 8401 Winterthur, Switzerland Project: BSY Labs File: $RCSfile$ Purpose: example answer for the BSY lab MEM Remarks: main routine Author: donn Date: K20 2021 Version: 0.9 FS21 $Id$ ------------------------------------------------------------ */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PAGE_SIZE 4096 // image library stuff #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" #include "sobel_rgb2g.h" extern int errno; //*************************************************************************** // Function: main(), parameter: none //*************************************************************************** int main(void) { int result; int pageSize; char bufferLength = 3; char *buffer; unsigned char bufferState[bufferLength]; int section = 1; (void) printf( "Hello MEM Lab\n" ); // ---------- section (void) printf( "------------ Part %i: Simple program check memory of process\n", section++ ); // ToDO 1: insert function to get and printout the process pid. then put program in an endless loop (void) printf( "----- the PID of this process is %i\n", getpid() ); (void) printf( "\nPress enter to continue\n" ); (void) getchar(); // ---------- section (void) printf( "------------ Part %i: whats the page size\n", section++ ); // ToDo 2: insert function to get and read the page size pageSize = getpagesize(); (void) printf( "----- the current page size is %d\n", pageSize); (void) printf( "Press return to continue\n" ); (void) getchar(); // ---------- section (void) printf( "------------ Part %i: generate some memory area\n", section++ ); // ToDo 3: insert code to reserve memory (void) malloc(3 * PAGE_SIZE); (void) printf( "\nPress return to continue\n" ); (void) getchar(); // ---------- section (void) printf( "------------ Part %i: Are these pages in memory?\n", section++ ); // ToDO 4: reserve memory with aligned_alloc this is necessary for the mincore function to function properly buffer = aligned_alloc(PAGE_SIZE, bufferLength * PAGE_SIZE); void inMemory() { (void) mincore(buffer, bufferLength * PAGE_SIZE, bufferState); if (bufferState[0] & 1) { (void) printf("In Memory\n"); } else { (void) printf("Not in Memory\n"); } } inMemory(); (void) printf( "Press return to continue\n" ); (void) getchar(); // ToDo 5: write something into the reserved buffer (void) sprintf(buffer, "Hello World! It's %s beautiful day!", "1"); (void) printf("Now in memory?\n"); inMemory(); (void) printf( "Press return to continue\n" ); (void) getchar(); // ---------- section (void) printf( "------------ Part %i: Lets limit the available memory\n", section++ ); // prepare variables and load a large file int width=0, height=0, bpp=0; uint8_t *rgb_image = stbi_load("Hopper.png", &width, &height, &bpp, 3); // in case of issues if (rgb_image == NULL) { perror("File read failed\n"); exit(EXIT_FAILURE); } // reserve memory for the greyscale image // the rgb png comes in 8-bit format uint8_t *gry_image = malloc ( width * height * sizeof(uint8_t) ); // lets exercise rgb2grayscale ( width, height, rgb_image, gry_image ); (void) printf( "Check the number of page faults\n" ); (void) printf( "Press return to continue\n" ); (void) getchar(); (void) printf( "Carry out the instructions in the lab guide to limit the available memory and repeat \n" ); (void) printf( "Bye MEM Lab\n" ); exit( 0 ); } //***************************************************************************