SCR
This commit is contained in:
6
TP2/TP2 reponses
Normal file
6
TP2/TP2 reponses
Normal file
@@ -0,0 +1,6 @@
|
||||
avec les appels systemes:
|
||||
modifier fichier
|
||||
lire fichier
|
||||
afficher
|
||||
modifier
|
||||
afficher
|
1
TP2/coherence-test.txt
Normal file
1
TP2/coherence-test.txt
Normal file
@@ -0,0 +1 @@
|
||||
L'asr, c'est bien!!
|
96
TP2/coherence.c
Normal file
96
TP2/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
TP2/copy.c
Normal file
35
TP2/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;
|
||||
}
|
||||
|
BIN
TP2/copyv1
Executable file
BIN
TP2/copyv1
Executable file
Binary file not shown.
BIN
TP2/copyv2
Executable file
BIN
TP2/copyv2
Executable file
Binary file not shown.
38
TP2/copyv2.c
Normal file
38
TP2/copyv2.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int copier_fichier(const char *source, const char *dest){
|
||||
int octet;
|
||||
FILE *fichier_source, *fichier_dest;
|
||||
fichier_source = fopen(source, "rb");
|
||||
if (fichier_source == NULL) {
|
||||
perror("Erreur lors de l'ouverture du fichier source");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fichier_dest = fopen(dest, "wb");
|
||||
if (fichier_dest == NULL) {
|
||||
perror("Erreur lors de l'ouverture du fichier source");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
while((octet = fgetc(fichier_source)) != EOF){
|
||||
fputc(octet, fichier_dest);
|
||||
}
|
||||
fclose(fichier_source);
|
||||
fclose(fichier_dest);
|
||||
printf("Copie terminé avec grand succès!\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
if (argc != 3) {
|
||||
printf("Usage : %s [-a] <fichier_source> <fichier_destination>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (copier_fichier(argv[1], argv[2]) == 0){
|
||||
printf("Fichier copié avec succès de %s vers %s\n", argv[1], argv[2]);
|
||||
return EXIT_SUCCESS;
|
||||
} else{
|
||||
printf("Echec de la copie");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
BIN
TP2/dess.data
Normal file
BIN
TP2/dess.data
Normal file
Binary file not shown.
BIN
TP2/dest.data
Normal file
BIN
TP2/dest.data
Normal file
Binary file not shown.
1
TP2/ecruos.txt
Normal file
1
TP2/ecruos.txt
Normal file
@@ -0,0 +1 @@
|
||||
GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG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ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
|
70
TP2/my_cp_map.c
Normal file
70
TP2/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;
|
||||
}
|
1
TP2/source.txt
Normal file
1
TP2/source.txt
Normal file
@@ -0,0 +1 @@
|
||||
GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGIIIIIIIIIIIIIIIOOOOOOOOOOOOYYYYYYYYYYYEEEEEEEEEEEEEEZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
|
0
TP2/source.txt~
Normal file
0
TP2/source.txt~
Normal file
BIN
TP2/test.data
Normal file
BIN
TP2/test.data
Normal file
Binary file not shown.
BIN
TP2/tset.data
Normal file
BIN
TP2/tset.data
Normal file
Binary file not shown.
Reference in New Issue
Block a user