update
This commit is contained in:
1
SCR/SCR2/TP02/Destination.txt~
Normal file
1
SCR/SCR2/TP02/Destination.txt~
Normal file
@@ -0,0 +1 @@
|
||||
Hello World !
|
34
SCR/SCR2/TP02/Q1/copy1.c
Normal file
34
SCR/SCR2/TP02/Q1/copy1.c
Normal 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/SCR2/TP02/Q1/copy1.c~
Normal file
34
SCR/SCR2/TP02/Q1/copy1.c~
Normal 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/SCR2/TP02/Q1/copy2.c
Normal file
30
SCR/SCR2/TP02/Q1/copy2.c
Normal 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/SCR2/TP02/Q1/dest.txt
Normal file
BIN
SCR/SCR2/TP02/Q1/dest.txt
Normal file
Binary file not shown.
BIN
SCR/SCR2/TP02/Q1/exe
Executable file
BIN
SCR/SCR2/TP02/Q1/exe
Executable file
Binary file not shown.
BIN
SCR/SCR2/TP02/Q1/origine.txt
Normal file
BIN
SCR/SCR2/TP02/Q1/origine.txt
Normal file
Binary file not shown.
1
SCR/SCR2/TP02/coherence-test.txt
Normal file
1
SCR/SCR2/TP02/coherence-test.txt
Normal file
@@ -0,0 +1 @@
|
||||
L'asr, c'est bien!!
|
96
SCR/SCR2/TP02/coherence.c
Normal file
96
SCR/SCR2/TP02/coherence.c
Normal 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/SCR2/TP02/copy.c~
Normal file
35
SCR/SCR2/TP02/copy.c~
Normal 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/SCR2/TP02/copy2.c~
Normal file
34
SCR/SCR2/TP02/copy2.c~
Normal 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/SCR2/TP02/exe
Executable file
BIN
SCR/SCR2/TP02/exe
Executable file
Binary file not shown.
155
SCR/SCR2/TP02/fadvise.c
Normal file
155
SCR/SCR2/TP02/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;
|
||||
|
||||
}
|
70
SCR/SCR2/TP02/my_cp_map.c
Normal file
70
SCR/SCR2/TP02/my_cp_map.c
Normal 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/SCR2/TP02/origine.txt~
Normal file
0
SCR/SCR2/TP02/origine.txt~
Normal file
10
SCR/SCR2/TP02/reponse.txt
Normal file
10
SCR/SCR2/TP02/reponse.txt
Normal 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/SCR2/TP02/reponse.txt~
Normal file
0
SCR/SCR2/TP02/reponse.txt~
Normal file
Reference in New Issue
Block a user