commit 7244d789c348a0dd16d446b38b828c7599c98baf Author: Tom Moguljak Date: Thu Sep 7 15:38:31 2023 +0200 Création du README diff --git a/Exo1/adresses_virtuelles.c b/Exo1/adresses_virtuelles.c new file mode 100644 index 0000000..fe6c824 --- /dev/null +++ b/Exo1/adresses_virtuelles.c @@ -0,0 +1,29 @@ +/* adresses virtuelles d'un processus */ + +#include +#include +#include +#include + +int t[1000] = {[0 ... 999] = 2}; + +int main(int argc, char * argv[]) +{ + int i=3; + static int j = 3; + char * m = (char*)malloc(1); + + printf("mon pid est %d\n\n",getpid()); + + /* ------- Affichage des adresses --------*/ + + printf("main\t=\t%p\n",main); + printf("&argc\t=\t%p\n",&argc); + printf("&i\t=\t%p\n",&i); + printf("&j\t=\t%p\n",&j); + printf("t\t=\t%p\n",t); + printf("m\t=\t%p\n",m); + + getchar(); +} + diff --git a/Exo1/ex1bis/Makefile b/Exo1/ex1bis/Makefile new file mode 100644 index 0000000..f6ddfde --- /dev/null +++ b/Exo1/ex1bis/Makefile @@ -0,0 +1,17 @@ +CFLAGS := -Wall -g -O0 +SRC=buf.c heap.c huge.c mmap.c null.c stack.c + +DEPENDHELPERS=helpers.o + +BINARIES=$(SRC:%.c=%) + +%.o : %c + gcc -c $+ + +$(BINARIES): % : %.o $(DEPENDHELPERS) + gcc -o $@ $+ + +all : $(BINARIES) + +clean: + rm -f *.o $(BINARIES) diff --git a/Exo1/ex1bis/buf.c b/Exo1/ex1bis/buf.c new file mode 100644 index 0000000..b6b1d0f --- /dev/null +++ b/Exo1/ex1bis/buf.c @@ -0,0 +1,10 @@ +#include "helpers.h" + +static char buf1[8 MB] = {0}; +static char buf2[16 MB] = {0}; + +int main(int argc, char **argv) +{ + randomize(buf2, 16 MB); + return interlude(); +} diff --git a/Exo1/ex1bis/data/256k b/Exo1/ex1bis/data/256k new file mode 100644 index 0000000..f3e3746 Binary files /dev/null and b/Exo1/ex1bis/data/256k differ diff --git a/Exo1/ex1bis/heap.c b/Exo1/ex1bis/heap.c new file mode 100644 index 0000000..196ecb9 --- /dev/null +++ b/Exo1/ex1bis/heap.c @@ -0,0 +1,8 @@ +#include "helpers.h" + +int main(int argc, char **argv) +{ + dirty(16 MB); + clean(32 MB); + return interlude(); +} diff --git a/Exo1/ex1bis/helpers.c b/Exo1/ex1bis/helpers.c new file mode 100644 index 0000000..1a2799f --- /dev/null +++ b/Exo1/ex1bis/helpers.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include "helpers.h" + +#include + +void randomize(char *buf, size_t n) +{ + assert(buf); + memset(buf, rand() & 0xff, n); +} + +void clean(size_t b) +{ + for (; b > 0; b -= 1 KB) + calloc(1 KB, sizeof(char)); +} + +void dirty(size_t b) +{ + for (; b > 0; b -= 1 KB) + randomize(calloc(1 KB, sizeof(char)), 1 KB); +} + +int interlude(void) +{ + pid_t pid = getpid(); + printf("pid %i\n", (int)pid); + printf("------------------------------------------\n" + "go check /proc/%i/smaps; I'll wait...\n" + "press when you're done\n", pid); + fgetc(stdin); + return 0; +} diff --git a/Exo1/ex1bis/helpers.h b/Exo1/ex1bis/helpers.h new file mode 100644 index 0000000..d8a774a --- /dev/null +++ b/Exo1/ex1bis/helpers.h @@ -0,0 +1,13 @@ +#ifndef _HELPERS_H +#define _HELPERS_H +#include + +#define KB * 1024 +#define MB * 1024 * 1024 + +void randomize(char *buf, size_t n); +void clean(size_t n); +void dirty(size_t n); +int interlude(void); + +#endif diff --git a/Exo1/ex1bis/huge.c b/Exo1/ex1bis/huge.c new file mode 100644 index 0000000..046aea0 --- /dev/null +++ b/Exo1/ex1bis/huge.c @@ -0,0 +1,12 @@ +#include "helpers.h" + +int main(int argc, char **argv) +{ + char *under = malloc(96 KB); + randomize(under, 96 KB); + + char *over = malloc(256 KB); + randomize(over, 256 KB); + + return interlude(); +} diff --git a/Exo1/ex1bis/mmap.c b/Exo1/ex1bis/mmap.c new file mode 100644 index 0000000..ac62144 --- /dev/null +++ b/Exo1/ex1bis/mmap.c @@ -0,0 +1,38 @@ +#include "helpers.h" +#include +#include +#include + + +int main(int argc, char **argv) +{ + /* inert map (never modified) */ + char *inert = mmap(NULL, 16 KB, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, + -1, 0); + /* anonymous, private mmap */ + char *anon_priv = mmap(NULL, 32 KB, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, + -1, 0); + randomize(anon_priv, 32 KB); + + /* anonymous, shared map */ + char *anon_shared = mmap(NULL, 64 KB, + PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_SHARED, + -1, 0); + randomize(anon_shared, 64 KB); + + /* private, file-backed map */ + int fd = open("data/256k", O_RDWR); + assert(fd >= 0); + char *file = mmap(NULL, 256 KB, + PROT_READ|PROT_WRITE, + MAP_PRIVATE, + fd, 0); + randomize(file, 128 KB); + + return interlude(); +} diff --git a/Exo1/ex1bis/null.c b/Exo1/ex1bis/null.c new file mode 100644 index 0000000..fbd46f7 --- /dev/null +++ b/Exo1/ex1bis/null.c @@ -0,0 +1,6 @@ +#include "helpers.h" + +int main(int argc, char **argv) +{ + return interlude(); +} diff --git a/Exo1/ex1bis/stack.c b/Exo1/ex1bis/stack.c new file mode 100644 index 0000000..700eef1 --- /dev/null +++ b/Exo1/ex1bis/stack.c @@ -0,0 +1,8 @@ +#include "helpers.h" + +int main (int argc, char **argv) +{ + char buf[28 KB] = {0}; + randomize(buf, 28 KB); + return interlude(); +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..e96bdae --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# TP 1 : Mémoire + +## Exercice 1 diff --git a/alignement.c b/alignement.c new file mode 100644 index 0000000..af17aeb --- /dev/null +++ b/alignement.c @@ -0,0 +1,55 @@ +/* alignement et objets */ + +struct exemple1 { + int x; + int y; + int z; + int w; +}; + +struct exemple2 { + char x; + char y; + char z; + char w; +}; + +struct exemple3 { + int x; + int y; + char z; + char w; +}; + +struct exemple4 { + int x; + char y; + int z; + char w; +}; + +union exemple5 { + int x; + char y; + int z; + char w; +}; + +int main() +{ + int a[4] = {1,2,3,4}; + char c[4] = {'a','b','c','d'}; + struct exemple1 ex1 = {1,2,3,4}; + struct exemple2 ex2 = {'a','b','c','d'}; + struct exemple3 ex3 = {1,2,'c','d'}; + struct exemple4 ex4 = {1,'c',2,'d'}; + union exemple5 ex5; + int x = 61; + char y = 62; + int z = 63; + char w = 64; + ex5.x=62;ex5.y=63;ex5.z=64;ex5.w=65; + + // appelez hexdump pour chaque variable +} + diff --git a/bss_data.c b/bss_data.c new file mode 100644 index 0000000..b34c3db --- /dev/null +++ b/bss_data.c @@ -0,0 +1,10 @@ +/* segment bss et data */ +#define N 1000 +int t[N]; /* version 1 */ +//int t[N]={1}; /* version 2 */ + +int main() +{ + return 0; +} + diff --git a/ij_ji.c b/ij_ji.c new file mode 100644 index 0000000..871aef9 --- /dev/null +++ b/ij_ji.c @@ -0,0 +1,38 @@ +/* accès mémoire */ + +#include +#include +#include +#define N 8192 + +int t[N][N]; + +static inline double tstamp(void) +{ + struct timespec tv; + clock_gettime(CLOCK_REALTIME, &tv); + return tv.tv_sec + tv.tv_nsec * 1.0e-9; +} + +int main() +{ + int i,j; + double t1,t2; + t1=tstamp(); + + /* version 1 */ + + for(i=0;i +#include "mem.h" + +int main(int argc,char * argv[]) +{ + int pages[] = {4,5,6,8,4,9,6,12,4,6,10}; + simu(4,pages,11); + +} diff --git a/memoire/mem.c b/memoire/mem.c new file mode 100644 index 0000000..3676a26 --- /dev/null +++ b/memoire/mem.c @@ -0,0 +1,174 @@ +#include "mem.h" +#include +#include +#include + +typedef struct frame { + int no; + int page; + int r; /* bit pour la seconde chance */ +} frame ; + + +typedef struct fifo { + int head; + int tail; + int nbelem; +} fifo; + +typedef struct mem { + int nbframes; + frame * frames; +} mem; + +static mem * memory=NULL; +static fifo FIFO; + + +/* Fifo est buffer circulaire + * head pointe sur la tete (element le plus ancien) + * tail pointe sur la queue (element le plus recent) + * nbelem represente le nombre d'element dans la file + * head et tail progressent modulo le nombre de frame + * dans la memoire */ +/* + * Memoire + ------------- + | frame 0 | <- head + ------------- + | frame 1 | + ------------- + | frame 2 | + ------------- + | frame 3 | + ------------- + | frame 4 | + ------------- + | ... | + ------------- + | ... | + ------------- + | ... | <- tail + ------------- + | ... | + ------------- +*/ + +static int init(int n) +{ + + int i; + memory = (mem*)calloc(1,sizeof(mem)); + memory-> nbframes = n; + memory-> frames = (frame *) calloc(n,sizeof(frame)); + if (memory -> frames == NULL) return -1; + + for(i=0;iframes[i].no = i; + memory->frames[i].page = -1; /* les frames sont vides initialement */ + memory->frames[i].r=0; + } + FIFO.head = FIFO.tail = 0; + FIFO.nbelem = 0; + return n; +} + +static int _page_is_in(int page) +{ +/* a ecrire */ +} + +static int _fifo_is_full() +{ + return (FIFO.nbelem == memory->nbframes); +} + +int get_page_2chance(int page) +{ +/* pour la deuxieme question */ +} + +int get_page_fifo(int page) +{ + /* a ecrire */ +} + +void simu(int nb_frame,int t[],int l) +{ + int i,j; + init(nb_frame); + frame ** trace; + trace = (frame **) malloc(sizeof(frame *) * l); + int * defaut = (int *) calloc(l,sizeof(int)); + for (i=0;iframes,sizeof(frame)*nb_frame); + } + printf("------------"); + for (i=0;iframes); + free(memory); + memory = NULL; + for(i=0;i +#include +#include +#include +#include + +static inline double tstamp(void) +{ + struct timespec tv; + clock_gettime(CLOCK_REALTIME, &tv); + return tv.tv_sec + tv.tv_nsec * 1.0e-9; +} + +void shuffle(int *array, size_t n) +{ + if (n > 1) + { + size_t i; + for (i = 0; i < n - 1; i++) +// for (i = 0; i < n ; i++) + { + size_t j = i + rand() / (RAND_MAX / (n - i) + 1); +// size_t j = rand()%n; + int t = array[j]; + array[j] = array[i]; + array[i] = t; + } + } +} + +void init_access_c(int access[],size_t size) +{ + int i; + for(i=0;i