tkt
This commit is contained in:
35
SCR3.1/TP2/Ex1/copy.c
Normal file
35
SCR3.1/TP2/Ex1/copy.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#define BLOCK_SIZE 1
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int fin,
|
||||
fout;
|
||||
char buf[BLOCK_SIZE];
|
||||
|
||||
assert( argc == 3 );
|
||||
|
||||
fin = fopen(argv[1],"r");
|
||||
assert( fin >= 0 );
|
||||
|
||||
fout = fopen(argv[2],O_CREAT|O_WRONLY|O_TRUNC);
|
||||
assert( fout >= 0 );
|
||||
|
||||
while(1){
|
||||
ssize_t nb_read;
|
||||
nb_read = fread(fin,buf,BLOCK_SIZE);
|
||||
if (nb_read <= 0)
|
||||
break;
|
||||
fwrite(fout,buf,nb_read);
|
||||
}
|
||||
|
||||
fclose(fin);
|
||||
fclose(fout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
155
SCR3.1/TP2/Ex1/fadvise.c
Normal file
155
SCR3.1/TP2/Ex1/fadvise.c
Normal 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;
|
||||
|
||||
}
|
||||
BIN
SCR3.1/TP2/Ex1/test.data
Normal file
BIN
SCR3.1/TP2/Ex1/test.data
Normal file
Binary file not shown.
BIN
SCR3.1/TP2/Ex2/mmap_copy
Executable file
BIN
SCR3.1/TP2/Ex2/mmap_copy
Executable file
Binary file not shown.
75
SCR3.1/TP2/Ex2/mmap_copy.c
Normal file
75
SCR3.1/TP2/Ex2/mmap_copy.c
Normal file
@@ -0,0 +1,75 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 3) {
|
||||
fprintf(stderr, "Usage: %s <source_file> <destination_file>\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
const char *src_path = argv[1];
|
||||
const char *dst_path = argv[2];
|
||||
|
||||
int src_fd = open(src_path, O_RDONLY);
|
||||
if (src_fd == -1) {
|
||||
perror("Erreur lors de l'ouverture du fichier source");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct stat sb;
|
||||
if (fstat(src_fd, &sb) == -1) {
|
||||
perror("Erreur lors de fstat");
|
||||
close(src_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
size_t file_size = sb.st_size;
|
||||
|
||||
int dst_fd = open(dst_path, O_RDWR | O_CREAT | O_TRUNC, 0644);
|
||||
if (dst_fd == -1) {
|
||||
perror("Erreur lors de l'ouverture du fichier de destination");
|
||||
close(src_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (ftruncate(dst_fd, file_size) == -1) {
|
||||
perror("Erreur lors de ftruncate");
|
||||
close(src_fd);
|
||||
close(dst_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void *src_map = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, src_fd, 0);
|
||||
if (src_map == MAP_FAILED) {
|
||||
perror("Erreur lors de mmap (source)");
|
||||
close(src_fd);
|
||||
close(dst_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void *dst_map = mmap(NULL, file_size, PROT_WRITE, MAP_SHARED, dst_fd, 0);
|
||||
if (dst_map == MAP_FAILED) {
|
||||
perror("Erreur lors de mmap (destination)");
|
||||
munmap(src_map, file_size);
|
||||
close(src_fd);
|
||||
close(dst_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
memcpy(dst_map, src_map, file_size);
|
||||
|
||||
munmap(src_map, file_size);
|
||||
munmap(dst_map, file_size);
|
||||
close(src_fd);
|
||||
close(dst_fd);
|
||||
|
||||
printf("Copie réussie de '%s' vers '%s'\n", src_path, dst_path);
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
SCR3.1/TP2/Ex2/test.data
Normal file
BIN
SCR3.1/TP2/Ex2/test.data
Normal file
Binary file not shown.
BIN
SCR3.1/TP2/Ex3/coherence
Executable file
BIN
SCR3.1/TP2/Ex3/coherence
Executable file
Binary file not shown.
1
SCR3.1/TP2/Ex3/coherence-test.txt
Normal file
1
SCR3.1/TP2/Ex3/coherence-test.txt
Normal file
@@ -0,0 +1 @@
|
||||
L'asr, c'est bien!!
|
||||
90
SCR3.1/TP2/Ex3/coherence.c
Normal file
90
SCR3.1/TP2/Ex3/coherence.c
Normal file
@@ -0,0 +1,90 @@
|
||||
#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();
|
||||
|
||||
int fd = open(TESTFILE, O_RDONLY);
|
||||
|
||||
char buf[BUFSIZ];
|
||||
ssize_t nr1 = read(fd, buf, 12);
|
||||
assert(nr1 >= 0);
|
||||
|
||||
write_asr_c_bien();
|
||||
|
||||
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();
|
||||
|
||||
FILE* f = fopen(TESTFILE, "r");
|
||||
|
||||
char buf[BUFSIZ];
|
||||
size_t nr1 = fread(buf, 1, 12, f);
|
||||
|
||||
write_asr_c_bien();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
BIN
SCR3.1/TP2/Ex4/Comparaison
Executable file
BIN
SCR3.1/TP2/Ex4/Comparaison
Executable file
Binary file not shown.
74
SCR3.1/TP2/Ex4/Comparaison.c
Normal file
74
SCR3.1/TP2/Ex4/Comparaison.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 3) {
|
||||
fprintf(stderr, "Usage: %s <file1> <file2>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *file1 = argv[1];
|
||||
const char *file2 = argv[2];
|
||||
|
||||
struct stat stat1, stat2;
|
||||
|
||||
if (stat(file1, &stat1) == -1) {
|
||||
perror("Erreur stat file1");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (stat(file2, &stat2) == -1) {
|
||||
perror("Erreur stat file2");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (stat1.st_size != stat2.st_size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fd1 = open(file1, O_RDONLY);
|
||||
if (fd1 == -1) {
|
||||
perror("Erreur open file1");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fd2 = open(file2, O_RDONLY);
|
||||
if (fd2 == -1) {
|
||||
perror("Erreur open file2");
|
||||
close(fd1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char buffer1[BUFFER_SIZE];
|
||||
char buffer2[BUFFER_SIZE];
|
||||
ssize_t bytes_read1, bytes_read2;
|
||||
|
||||
while ((bytes_read1 = read(fd1, buffer1, BUFFER_SIZE)) > 0) {
|
||||
bytes_read2 = read(fd2, buffer2, BUFFER_SIZE);
|
||||
|
||||
if (bytes_read2 != bytes_read1 || memcmp(buffer1, buffer2, bytes_read1) != 0) {
|
||||
close(fd1);
|
||||
close(fd2);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (bytes_read1 == -1) {
|
||||
perror("Erreur lecture file1");
|
||||
close(fd1);
|
||||
close(fd2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
close(fd1);
|
||||
close(fd2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
SCR3.1/TP2/Ex4/test.data
Normal file
BIN
SCR3.1/TP2/Ex4/test.data
Normal file
Binary file not shown.
BIN
SCR3.1/TP2/Ex4/test.data2
Normal file
BIN
SCR3.1/TP2/Ex4/test.data2
Normal file
Binary file not shown.
12
SCR3.1/TP2/reponses.txt
Normal file
12
SCR3.1/TP2/reponses.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
Ex1 :
|
||||
|
||||
stdio utilise un buffer puis une fois remplie il write dans le fichier se qu'il y a dans la mémoire.
|
||||
Alors que les appels systèmes linux juste prend octet par octet dans le fichier
|
||||
|
||||
|
||||
Ex3 :
|
||||
quand on fait /coherence -s ca nous écrit L'asr, c'est pas bien!
|
||||
quand on fait /coherence -l ca nous écrit L'asr, c'est bien!!
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user