Add solutions for BSY
This commit is contained in:
parent
d5ada4ac61
commit
1d4fc85555
10 changed files with 9700 additions and 0 deletions
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 16 MiB |
|
@ -0,0 +1,180 @@
|
|||
/* ------------------------------------------------------------
|
||||
____ ________
|
||||
/ _/__ / __/ __/
|
||||
_/ // _ \/ _/_\ \
|
||||
/___/_//_/___/___/
|
||||
|
||||
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 <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <sched.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/sysinfo.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#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 );
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//***************************************************************************
|
Binary file not shown.
|
@ -0,0 +1,37 @@
|
|||
# ---------------------------------------------------------------------------
|
||||
# Makefile for Lab2 MPC - Multicore threads and processes
|
||||
# Module: MPC
|
||||
# Changes: donn
|
||||
# File: makefile
|
||||
# Version: v.fs21
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
# Macros
|
||||
|
||||
CC = gcc -std=gnu99
|
||||
CFLGS = -Wall
|
||||
LFLGS = -pthread
|
||||
|
||||
TARGET := out
|
||||
|
||||
source = sobel_rgb2g.c MEM_lab.c
|
||||
objects = sobel_rgb2g.o MEM_lab.o
|
||||
header = sobel_rgb2g.h makefile
|
||||
|
||||
|
||||
.PHONY : clean all out
|
||||
|
||||
out: $(source) $(objects) $(header)
|
||||
@echo "Linking"
|
||||
$(CC) $(CFLGS) $(objects) -o $@.t -lm
|
||||
|
||||
.c.o: $<
|
||||
$(CC) $(CFLGS) -c $< -o $*.o
|
||||
|
||||
clean:
|
||||
rm -f *.e *.o
|
||||
@echo "directory cleaned"
|
||||
|
||||
all:
|
||||
@rm -f *.o
|
||||
#-----------------------------------------------------------------------------
|
|
@ -0,0 +1,25 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#include "sobel_rgb2g.h"
|
||||
|
||||
void rgb2grayscale (int width, int height, uint8_t const *rgb_source,
|
||||
uint8_t *gs_dest )
|
||||
{
|
||||
int loop, j;
|
||||
uint8_t temp;
|
||||
unsigned int greyscale;
|
||||
int max_loop = width * height * 3;
|
||||
|
||||
for (loop = 0, j = 0; loop < max_loop; loop +=3, j++) {
|
||||
temp = rgb_source[loop];
|
||||
greyscale= (temp*30)/100;
|
||||
|
||||
temp = (rgb_source[loop + 1]);
|
||||
greyscale += (temp*59)/100;
|
||||
|
||||
temp = (rgb_source[loop + 2]);
|
||||
greyscale += (temp*11)/100;
|
||||
|
||||
gs_dest[j] = (uint8_t)greyscale;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef RGB2G
|
||||
#define RGB2G
|
||||
|
||||
void rgb2grayscale ( int, int, const uint8_t *, uint8_t *);
|
||||
|
||||
#endif
|
Binary file not shown.
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue