Finalisation de l'exo 2
This commit is contained in:
parent
8357f6a4c5
commit
ccf0833955
@ -304,3 +304,7 @@ XXXXXXXX BB BB BB BB BB BB BB BB BB BB BB BB BB BB BB BB |CCCCCCCCCCCCCCCC|
|
||||
- utilisez strace pour tracer les appels systèmes de vos deux programmes.
|
||||
|
||||
>Le deuxième programme est beaucoup plus rapide que le premier car il utilise des buffers, alors que le premier programme lit et écrit octet par octet et fait plus d'appels systèmes.
|
||||
|
||||
## Exercice 2
|
||||
|
||||
>Écrire un programme qui copie un fichier en utilisant `mmap`.
|
46
TP2/Exo2/exo2.c
Normal file
46
TP2/Exo2/exo2.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BLOCK_SIZE 1
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int fin,
|
||||
fout;
|
||||
char buf[BLOCK_SIZE];
|
||||
struct stat st;
|
||||
|
||||
assert( argc == 3 );
|
||||
|
||||
fin = open(argv[1],O_RDONLY);
|
||||
assert( fin >= 0 );
|
||||
|
||||
fstat(fin, &st);
|
||||
|
||||
char *src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fin, 0);
|
||||
assert(src != MAP_FAILED);
|
||||
|
||||
fout = open(argv[2],O_CREAT|O_RDWR|O_TRUNC,0600);
|
||||
assert( fout >= 0 );
|
||||
|
||||
ftruncate(fout, st.st_size);
|
||||
|
||||
char *dst = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fout, 0);
|
||||
assert(dst != MAP_FAILED);
|
||||
|
||||
memcpy(dst, src, st.st_size);
|
||||
|
||||
munmap(src, st.st_size);
|
||||
munmap(dst, st.st_size);
|
||||
|
||||
close(fin);
|
||||
close(fout);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user