septembre + octobre

This commit is contained in:
2023-10-12 16:39:49 +02:00
commit 06bf5f9488
389 changed files with 4233 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();
}

55
SCR/TP01/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
SCR/TP01/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;
}

10
SCR/TP01/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;
}

17
SCR/TP01/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)

BIN
SCR/TP01/ex1bis/buf Executable file

Binary file not shown.

10
SCR/TP01/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
SCR/TP01/ex1bis/buf.o Normal file

Binary file not shown.

BIN
SCR/TP01/ex1bis/data/256k Normal file

Binary file not shown.

8
SCR/TP01/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
SCR/TP01/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
SCR/TP01/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

BIN
SCR/TP01/ex1bis/helpers.o Normal file

Binary file not shown.

12
SCR/TP01/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
SCR/TP01/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
SCR/TP01/ex1bis/null.c Normal file
View File

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

8
SCR/TP01/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();
}

BIN
SCR/TP01/exe Executable file

Binary file not shown.

38
SCR/TP01/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;
}

38
SCR/TP01/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;
}

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
SCR/TP01/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
SCR/TP01/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);
}

174
SCR/TP01/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
SCR/TP01/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
SCR/TP01/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));
}

View File

@@ -0,0 +1 @@
Hello World !

34
SCR/TP02/Q1/copy1.c Normal file
View File

@@ -0,0 +1,34 @@
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#define BLOCK_SIZE 1
int main(int argc, char *argv[])
{
int fin, fout;
char buf[BLOCK_SIZE];
assert( argc == 3 );
fin = open(argv[1],O_RDONLY);
assert( fin >= 0 );
fout = open(argv[2],O_CREAT|O_WRONLY|O_TRUNC,0600);
assert( fout >= 0 );
while(1){
ssize_t nb_read;
nb_read = read(fin,buf,BLOCK_SIZE);
if (nb_read <= 0)
break;
write(fout,buf,nb_read);
}
close(fin);
close(fout);
return 0;
}

34
SCR/TP02/Q1/copy1.c~ Normal file
View File

@@ -0,0 +1,34 @@
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#define BLOCK_SIZE 1
int main(int argc, char *argv[])
{
int fin, fout;
char buf[BLOCK_SIZE];
assert( argc == 3 );
fin = open(argv[1],O_RDONLY);
assert( fin >= 0 );
fout = open(argv[2],O_CREAT|O_WRONLY|O_TRUNC,0600);
assert( fout >= 0 );
while(1){
ssize_t nb_read;
nb_read = read(fin,buf,BLOCK_SIZE);
if (nb_read <= 0)
break;
write(fout,buf,nb_read);
}
close(fin);
close(fout);
return 0;
}

30
SCR/TP02/Q1/copy2.c Normal file
View File

@@ -0,0 +1,30 @@
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char** argv){
FILE* fichierScr = NULL;
FILE* fichierDes = NULL;
char caractere[2];
int arg=0;
if (argc > 2){
fichierDes = fopen(argv[2],"w");
fichierScr = fopen(argv[1],"r");
if (fichierScr){
if (fichierDes){
while (fread(caractere,sizeof(char),1,fichierScr)!=0){
fwrite(caractere,sizeof(char),1,fichierDes);
}
return 0;
}
printf("%s n'existe pas ou ne peut pas s'ouvrir\n",argv[2]);
return 1;
}
printf("%s n'existe pas ou ne peut pas s'ouvrir\n",argv[1]);
return 1;
}
printf("Utilisation: argv[0] <fichier_source> <fichier_destination>\n");
return 0;
}

BIN
SCR/TP02/Q1/dest.txt Normal file

Binary file not shown.

BIN
SCR/TP02/Q1/exe Executable file

Binary file not shown.

BIN
SCR/TP02/Q1/origine.txt Normal file

Binary file not shown.

View File

@@ -0,0 +1 @@
L'asr, c'est bien!!

96
SCR/TP02/coherence.c Normal file
View File

@@ -0,0 +1,96 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <sys/fcntl.h>
#define TESTFILE "coherence-test.txt"
void write_asr_c_pas_bien()
{
FILE* f = fopen(TESTFILE, "w");
fprintf(f, "L'asr, c'est pas bien!\n");
fclose(f);
}
void write_asr_c_bien()
{
FILE* f = fopen(TESTFILE, "w");
fprintf(f, "L'asr, c'est bien!!\n");
fclose(f);
}
void read_using_syscalls()
{
write_asr_c_pas_bien();
// open file descriptor
int fd = open(TESTFILE, O_RDONLY);
// read first 12 bytes
char buf[BUFSIZ];
ssize_t nr1 = read(fd, buf, 12);
assert(nr1 >= 0);
write_asr_c_bien();
// read rest of file, print to stdout
ssize_t nr2 = read(fd, buf + nr1, BUFSIZ - nr1);
close(fd);
fwrite(buf, 1, nr1 + nr2, stdout);
}
void read_using_stdio()
{
write_asr_c_pas_bien();
// open stdio file
FILE* f = fopen(TESTFILE, "r");
// read first 12 bytes
char buf[BUFSIZ];
size_t nr1 = fread(buf, 1, 12, f);
write_asr_c_bien();
// read rest of file, print to stdout
size_t nr2 = fread(buf + nr1, 1, BUFSIZ - nr1, f);
fclose(f);
fwrite(buf, 1, nr1 + nr2, stdout);
}
static void usage()
{
fprintf(stderr, "Usage: ./coherence -l (linux syscalls) or ./coherence -s (stdio)\n");
exit(1);
}
int main(int argc, char** argv)
{
int which = 0;
int opt;
while ((opt = getopt(argc, argv, "ls")) != -1) {
switch (opt) {
case 's':
which = 's';
break;
case 'l':
which = 'l';
break;
default:
usage();
}
}
if (which == 's') {
read_using_stdio();
} else if (which == 'l') {
read_using_syscalls();
} else {
usage();
}
}

35
SCR/TP02/copy.c~ Normal file
View File

@@ -0,0 +1,35 @@
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#define BLOCK_SIZE 1
int main(int argc, char *argv[])
{
int fin,
fout;
char buf[BLOCK_SIZE];
assert( argc == 3 );
fin = open(argv[1],O_RDONLY);
assert( fin >= 0 );
fout = open(argv[2],O_CREAT|O_WRONLY|O_TRUNC,0600);
assert( fout >= 0 );
while(1){
ssize_t nb_read;
nb_read = read(fin,buf,BLOCK_SIZE);
if (nb_read <= 0)
break;
write(fout,buf,nb_read);
}
close(fin);
close(fout);
return 0;
}

34
SCR/TP02/copy2.c~ Normal file
View File

@@ -0,0 +1,34 @@
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#define BLOCK_SIZE 1
int main(int argc, char *argv[])
{
int fin, fout;
char buf[BLOCK_SIZE];
assert( argc == 3 );
fin = open(argv[1],O_RDONLY);
assert( fin >= 0 );
fout = open(argv[2],O_CREAT|O_WRONLY|O_TRUNC,0600);
assert( fout >= 0 );
while(1){
ssize_t nb_read;
nb_read = read(fin,buf,BLOCK_SIZE);
if (nb_read <= 0)
break;
write(fout,buf,nb_read);
}
close(fin);
close(fout);
return 0;
}

BIN
SCR/TP02/exe Executable file

Binary file not shown.

155
SCR/TP02/fadvise.c Normal file
View File

@@ -0,0 +1,155 @@
#include <linux/falloc.h>
#include <linux/fadvise.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <asm/unistd_64.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
//#include <linux-ftools.h>
#define LINUX_FTOOLS_VERSION "1.0.0"
/**
SYNTAX: filename mode [offset] [,length]
Where mode can be:
POSIX_FADV_NORMAL No further special treatment.
POSIX_FADV_RANDOM Expect random page references.
POSIX_FADV_SEQUENTIAL Expect sequential page references.
POSIX_FADV_WILLNEED Will need these pages.
POSIX_FADV_DONTNEED Don't need these pages.
POSIX_FADV_NOREUSE Data will be accessed once.
Allows an application to to tell the kernel how it expects to use a file handle,
so that the kernel can choose appropriate read-ahead and caching techniques for
access to the corresponding file. This is similar to the POSIX version of the
madvise system call, but for file access instead of memory access. The
sys_fadvise64() function is obsolete and corresponds to a broken glibc API,
sys_fadvise64_64() is the fixed version. The following are the values for the
advice parameter:
FADV_NORMAL
No special treatment.
FADV_RANDOM
Expect page references in random order.
FADV_SEQUENTIAL
Expect page references in sequential order.
FADV_WILLNEED
Expect access in the near future.
FADV_DONTNEED
Do not expect access in the near future. Subsequent access of pages in this
range will succeed, but will result either in reloading of the memory contents
from the underlying mapped file or zero-fill-in-demand pages for mappings
without an underlying file.
FADV_NOREUSE
Access data only once.
*/
int main(int argc, char *argv[]) {
if ( argc < 3 ) {
fprintf( stderr, "%s version %s\n", argv[0], LINUX_FTOOLS_VERSION );
printf( "SYNTAX: fadvise filename mode [offset] [,length]\n" );
printf( "Where mode can be:\n\n" );
printf( " POSIX_FADV_NORMAL No further special treatment. \n" );
printf( " POSIX_FADV_RANDOM Expect random page references. \n" );
printf( " POSIX_FADV_SEQUENTIAL Expect sequential page references. \n" );
printf( " POSIX_FADV_WILLNEED Will need these pages. \n" );
printf( " POSIX_FADV_DONTNEED Don't need these pages. \n" );
printf( " POSIX_FADV_NOREUSE Data will be accessed once. \n" );
exit( 1 );
}
char* path = argv[1];
char* param_mode = argv[2];
printf( "Going to fadvise %s\n", path );
int flags = O_RDWR;
int fd = open( path, flags );
if ( fd == -1 ) {
perror( NULL );
return 1;
}
struct stat fd_stat ;
if ( fstat( fd, &fd_stat ) < 0 ) {
perror( "Could not stat file: " );
return 1;
}
loff_t offset = 0;
loff_t length = fd_stat.st_size;
if ( argc >= 4 ) {
offset = strtol( argv[3], NULL, 10 );
}
if ( argc >= 5 ) {
length = strtol( argv[4], NULL, 10 );
}
printf( "offset: %ld\n", offset );
printf( "length: %ld\n", length );
int mode = -1;
if ( strcmp( param_mode , "POSIX_FADV_NORMAL" ) == 0 ) {
mode = POSIX_FADV_NORMAL;
} else if ( strcmp( param_mode , "POSIX_FADV_RANDOM" ) == 0 ) {
mode = POSIX_FADV_RANDOM;
} else if ( strcmp( param_mode , "POSIX_FADV_SEQUENTIAL" ) == 0 ) {
mode = POSIX_FADV_SEQUENTIAL;
} else if ( strcmp( param_mode , "POSIX_FADV_WILLNEED" ) == 0 ) {
mode = POSIX_FADV_DONTNEED;
} else if ( strcmp( param_mode , "POSIX_FADV_DONTNEED" ) == 0 ) {
mode = POSIX_FADV_DONTNEED;
} else if ( strcmp( param_mode , "POSIX_FADV_NOREUSE" ) == 0 ) {
mode = POSIX_FADV_NOREUSE;
} else {
printf( "Invalid mode %s\n", param_mode );
exit( 1 );
}
printf( "mode: %s\n", param_mode );
long result = syscall( SYS_fadvise64, fd, offset, length , mode );
if ( result != 0 ) {
if ( result != -1 ) {
errno=result;
perror( "Unable to fadvise" );
} else {
printf( "Unable to fadvise: %ld\n" , result );
}
return 1;
}
printf( "WIN\n" );
close( fd );
return 0;
}

70
SCR/TP02/my_cp_map.c Normal file
View File

@@ -0,0 +1,70 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <time.h>
#include <sys/mman.h>
#define BUFSIZE 1024
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 argc, char *argv[])
{
//char buf[BUFSIZE];
char * bufin = NULL,
* bufout = NULL;
int fin,
fout;
double start,
end;
size_t filesize = 0;
assert(argc == 3);
start = tstamp();
fin = open(argv[1],O_RDONLY);
assert(fin >=0);
fout = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
assert(fout >=0);
filesize = lseek(fin,0,SEEK_END);
ftruncate (fout,filesize);
bufin = mmap(NULL,filesize,PROT_READ,MAP_PRIVATE,fin,0);
assert(bufin != (void*)-1);
bufout = mmap(NULL,filesize,PROT_WRITE,MAP_SHARED,fout,0);
assert(bufout != (void*)-1);
memcpy(bufout,bufin,filesize);
//munmap(bufin,filesize);
//munmap(bufout,filesize);
//ssize_t nb_read = read(fin,buf,sizeof(buf));
close(fin);
close(fout);
end = tstamp();
printf("time = %.3lf\n",end - start);
return 0;
}

0
SCR/TP02/origine.txt~ Normal file
View File

10
SCR/TP02/reponse.txt Normal file
View File

@@ -0,0 +1,10 @@
time ./exe
=> affiche le temps d'execution
dd if=/dev/urandom of=toto.dat bs=1k count=1k
=> creer un fichier
diff file1 file2
=> compare 2 fichier
strace

0
SCR/TP02/reponse.txt~ Normal file
View File

45
SCR/TP03/copy1byte.c Normal file
View File

@@ -0,0 +1,45 @@
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
#include <stdlib.h>
#include<assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define msg1 "je suis le pere"
#define msg2 "je suis le fils !!!"
int main(int argc,char * argv[]){
int infd,outfd;
ssize_t nbread;
char buf[1];
pid_t p;
if (argc != 3){
printf("%s infile outfile\n",argv[0]);
exit(1);
}
infd = open(argv[1],O_RDONLY);
assert(infd >= 0);
outfd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644);
assert(outfd >= 0);
p=fork(); // <- decommentez cette ligne
while(1){
nbread=read(infd,buf,sizeof(buf));
if (nbread <=0 ) break;
write(outfd,buf,sizeof(buf));
}
close(infd);
close(outfd);
}

45
SCR/TP03/copy1byte.c~ Normal file
View File

@@ -0,0 +1,45 @@
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>
#include <stdlib.h>
#include<assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define msg1 "je suis le pere"
#define msg2 "je suis le fils !!!"
int main(int argc,char * argv[]){
int infd,outfd;
ssize_t nbread;
char buf[1];
pid_t p;
if (argc != 3){
printf("%s infile outfile\n",argv[0]);
exit(1);
}
infd = open(argv[1],O_RDONLY);
assert(infd >= 0);
outfd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644);
assert(outfd >= 0);
//p=fork(); // <- decommentez cette ligne
while(1){
nbread=read(infd,buf,sizeof(buf));
if (nbread <=0 ) break;
write(outfd,buf,sizeof(buf));
}
close(infd);
close(outfd);
}

12
SCR/TP03/ex1-stdio.c Normal file
View File

@@ -0,0 +1,12 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
printf("NON");
if (fork()) {
printf("OUI\n");
}
}

14
SCR/TP03/ex1-syscall.c Normal file
View File

@@ -0,0 +1,14 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
//printf("NON");
write(STDOUT_FILENO,"NON",3);
if (fork()) {
//printf("OUI\n");
write(STDOUT_FILENO,"OUI\n",4);
}
}

5
SCR/TP03/ex2.txt Normal file
View File

@@ -0,0 +1,5 @@
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:

1
SCR/TP03/ex2.txt~ Normal file
View File

@@ -0,0 +1 @@
je suis alexis et je suis vraiment crazy !!! (:

5
SCR/TP03/ex3.txt Normal file
View File

@@ -0,0 +1,5 @@
j esuis alexis et je suis vraiment crazy !!! (:
je suis alexis t jee suis vraiment crazy !!! (:
je uiss alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:
je suis alexis et je suis vraiment crazy !!! (:

Some files were not shown because too many files have changed in this diff Show More