Création du README

This commit is contained in:
Tom MOGULJAK 2023-09-07 15:38:31 +02:00
commit 7244d789c3
20 changed files with 587 additions and 0 deletions

View File

@ -0,0 +1,29 @@
/* adresses virtuelles d'un processus */
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
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();
}

17
Exo1/ex1bis/Makefile Normal file
View File

@ -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)

10
Exo1/ex1bis/buf.c Normal file
View File

@ -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();
}

BIN
Exo1/ex1bis/data/256k Normal file

Binary file not shown.

8
Exo1/ex1bis/heap.c Normal file
View File

@ -0,0 +1,8 @@
#include "helpers.h"
int main(int argc, char **argv)
{
dirty(16 MB);
clean(32 MB);
return interlude();
}

36
Exo1/ex1bis/helpers.c Normal file
View File

@ -0,0 +1,36 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "helpers.h"
#include <stdlib.h>
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 <Enter> when you're done\n", pid);
fgetc(stdin);
return 0;
}

13
Exo1/ex1bis/helpers.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef _HELPERS_H
#define _HELPERS_H
#include <stdlib.h>
#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

12
Exo1/ex1bis/huge.c Normal file
View File

@ -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();
}

38
Exo1/ex1bis/mmap.c Normal file
View File

@ -0,0 +1,38 @@
#include "helpers.h"
#include <sys/mman.h>
#include <assert.h>
#include <fcntl.h>
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();
}

6
Exo1/ex1bis/null.c Normal file
View File

@ -0,0 +1,6 @@
#include "helpers.h"
int main(int argc, char **argv)
{
return interlude();
}

8
Exo1/ex1bis/stack.c Normal file
View File

@ -0,0 +1,8 @@
#include "helpers.h"
int main (int argc, char **argv)
{
char buf[28 KB] = {0};
randomize(buf, 28 KB);
return interlude();
}

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# TP 1 : Mémoire
## Exercice 1

55
alignement.c Normal file
View File

@ -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
}

10
bss_data.c Normal file
View File

@ -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;
}

38
ij_ji.c Normal file
View File

@ -0,0 +1,38 @@
/* accès mémoire */
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
#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<N;i++) for(j=0;j<N;j++)
t[i][j] = 1;
/* version 2 */
// for(i=0;i<N;i++) for(j=0;j<N;j++)
//t[j][i] = 1;
//
t2=tstamp();
printf("time = %lf\n",t2-t1);
return 0;
}

5
memoire/Makefile Normal file
View File

@ -0,0 +1,5 @@
mem.o : mem.h mem.c
gcc -c mem.c
main : main.c mem.o
gcc -o main main.c mem.o

9
memoire/main.c Normal file
View File

@ -0,0 +1,9 @@
#include<stdio.h>
#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);
}

174
memoire/mem.c Normal file
View File

@ -0,0 +1,174 @@
#include "mem.h"
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
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;i<n;i++){
memory->frames[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;i<l+1;i++) trace[i] = (frame *) malloc(nb_frame * sizeof(frame));
for(i=0;i<l;i++){
defaut[i] = get_page_fifo(t[i]);
//defaut[i] = get_page_2chance(t[i]);
memcpy (trace[i],memory->frames,sizeof(frame)*nb_frame);
}
printf("------------");
for (i=0;i<l;i++){
printf("------",i+1);
}
printf("\n");
printf("|temps | ");
for (i=0;i<l;i++){
printf(" %3d |",i);
}
printf("\n");
printf("------------");
for (i=0;i<l;i++){
printf("------",i+1);
}
printf("\n");
printf("|page | ");
for (i=0;i<l;i++){
printf(" %3d |",t[i]);
}
printf("\n");
printf("------------");
for (i=0;i<l;i++){
printf("------",i+1);
}
printf("\n");
for(j=0;j<nb_frame;j++){
printf("|frame %3d| ",j);
for(i=0;i<l;i++){
if (trace[i][j].page !=-1) printf(" %3d |",trace[i][j].page);
else printf(" X |");
}
printf("\n");
printf("------------");
for (i=0;i<l;i++){
printf("------",i+1);
}
printf("\n");
}
printf("|default | ");
for (i=0;i<l;i++){
printf(" %3d |",!defaut[i]);
}
printf("\n");
printf("------------");
for (i=0;i<l;i++){
printf("------",i+1);
}
printf("\n");
free(memory->frames);
free(memory);
memory = NULL;
for(i=0;i<l;i++) free(trace[i]);
free(trace);
}

8
memoire/mem.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _MEM_H
#define _MEM_H
void simu(
int nb_frame, /* nombre de cadre */
int t[], /* pages de la simulation */
int l); /* nombres de pages pour la simulation */
#endif

108
sum_array.c Normal file
View File

@ -0,0 +1,108 @@
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
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<size;i++) access[i] = i;
}
void init_access_d(int access[],size_t size)
{
int i;
for(i=0;i<size;i++) access[i] = size-i-1;
}
void init_access_a(int access[],size_t size)
{
init_access_c(access,size);
shuffle(access,size);
}
void init_array(int t[],int N)
{
int i;
for(i=0;i<N;i++) t[i] = i ;
}
long int sum_array(int t[],int access[],size_t size)
{
long int S=0;
int i;
for(i=0;i<size;i++) S += t[access[i]];
return S;
}
int main(int argc,char * argv[])
{
double t1,t2;
int * array; // tableau à sommer (contient les tous les entiers [0,SIZE-1]
int * access;
int i,size;
long int S=0;
if (argc !=3) {
printf("%s -c|-d|-a SIZE\n",argv[0]);
return 1;
}
size=strtol(argv[2],NULL,0);
array=(int *)malloc(sizeof(int)*size);
assert(array != NULL);
access=(int *)malloc(sizeof(int)*size);
assert(access != NULL);
init_array(array,size);
if (strcmp(argv[1],"-c") == 0)
init_access_c(access,size);
if (strcmp(argv[1],"-d") == 0)
init_access_d(access,size);
if (strcmp(argv[1],"-a") == 0)
init_access_a(access,size);
/* On somme les elements en accedant au tableau
* sequentiellement (croissant/décroissant), ou
* de manière aléatoire
* */
t1=tstamp();
S= sum_array(array,access,size);
t2=tstamp();
printf("S=%ld %lf\n",S,(t2-t1));
}