TP2
This commit is contained in:
parent
c6bf69ab23
commit
d2bd0410c0
158
SCR3.1/TP2/,
Normal file
158
SCR3.1/TP2/,
Normal file
@ -0,0 +1,158 @@
|
||||
[4mDD[24m(1) User Commands [4mDD[24m(1)
|
||||
|
||||
[1mNAME[0m
|
||||
dd - convert and copy a file
|
||||
|
||||
[1mSYNOPSIS[0m
|
||||
[1mdd [22m[[4mOPERAND[24m]...
|
||||
[1mdd [4m[22mOPTION[0m
|
||||
|
||||
[1mDESCRIPTION[0m
|
||||
Copy a file, converting and formatting according to the operands.
|
||||
|
||||
bs=BYTES
|
||||
read and write up to BYTES bytes at a time (default: 512); over‐
|
||||
rides ibs and obs
|
||||
|
||||
cbs=BYTES
|
||||
convert BYTES bytes at a time
|
||||
|
||||
conv=CONVS
|
||||
convert the file as per the comma separated symbol list
|
||||
|
||||
count=N
|
||||
copy only N input blocks
|
||||
|
||||
ibs=BYTES
|
||||
read up to BYTES bytes at a time (default: 512)
|
||||
|
||||
if=FILE
|
||||
read from FILE instead of stdin
|
||||
|
||||
iflag=FLAGS
|
||||
read as per the comma separated symbol list
|
||||
|
||||
obs=BYTES
|
||||
write BYTES bytes at a time (default: 512)
|
||||
|
||||
of=FILE
|
||||
write to FILE instead of stdout
|
||||
|
||||
oflag=FLAGS
|
||||
write as per the comma separated symbol list
|
||||
|
||||
seek=N (or oseek=N) skip N obs-sized output blocks
|
||||
|
||||
skip=N (or iseek=N) skip N ibs-sized input blocks
|
||||
|
||||
status=LEVEL
|
||||
The LEVEL of information to print to stderr; ’none’ suppresses
|
||||
everything but error messages, ’noxfer’ suppresses the final
|
||||
transfer statistics, ’progress’ shows periodic transfer statis‐
|
||||
tics
|
||||
|
||||
N and BYTES may be followed by the following multiplicative suffixes:
|
||||
c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024, xM=M,
|
||||
GB=1000*1000*1000, G=1024*1024*1024, and so on for T, P, E, Z, Y, R, Q.
|
||||
Binary prefixes can be used, too: KiB=K, MiB=M, and so on. If N ends
|
||||
in ’B’, it counts bytes not blocks.
|
||||
|
||||
Each CONV symbol may be:
|
||||
|
||||
ascii from EBCDIC to ASCII
|
||||
|
||||
ebcdic from ASCII to EBCDIC
|
||||
|
||||
ibm from ASCII to alternate EBCDIC
|
||||
|
||||
block pad newline-terminated records with spaces to cbs-size
|
||||
|
||||
unblock
|
||||
replace trailing spaces in cbs-size records with newline
|
||||
|
||||
lcase change upper case to lower case
|
||||
|
||||
ucase change lower case to upper case
|
||||
|
||||
sparse try to seek rather than write all-NUL output blocks
|
||||
|
||||
swab swap every pair of input bytes
|
||||
|
||||
sync pad every input block with NULs to ibs-size; when used with
|
||||
block or unblock, pad with spaces rather than NULs
|
||||
|
||||
excl fail if the output file already exists
|
||||
|
||||
nocreat
|
||||
do not create the output file
|
||||
|
||||
notrunc
|
||||
do not truncate the output file
|
||||
|
||||
noerror
|
||||
continue after read errors
|
||||
|
||||
fdatasync
|
||||
physically write output file data before finishing
|
||||
|
||||
fsync likewise, but also write metadata
|
||||
|
||||
Each FLAG symbol may be:
|
||||
|
||||
append append mode (makes sense only for output; conv=notrunc sug‐
|
||||
gested)
|
||||
|
||||
direct use direct I/O for data
|
||||
|
||||
directory
|
||||
fail unless a directory
|
||||
|
||||
dsync use synchronized I/O for data
|
||||
|
||||
sync likewise, but also for metadata
|
||||
|
||||
fullblock
|
||||
accumulate full blocks of input (iflag only)
|
||||
|
||||
nonblock
|
||||
use non-blocking I/O
|
||||
|
||||
noatime
|
||||
do not update access time
|
||||
|
||||
nocache
|
||||
Request to drop cache. See also oflag=sync
|
||||
|
||||
noctty do not assign controlling terminal from file
|
||||
|
||||
nofollow
|
||||
do not follow symlinks
|
||||
|
||||
Sending a USR1 signal to a running ’dd’ process makes it print I/O sta‐
|
||||
tistics to standard error and then resume copying.
|
||||
|
||||
Options are:
|
||||
|
||||
[1m--help [22mdisplay this help and exit
|
||||
|
||||
[1m--version[0m
|
||||
output version information and exit
|
||||
|
||||
[1mAUTHOR[0m
|
||||
Written by Paul Rubin, David MacKenzie, and Stuart Kemp.
|
||||
|
||||
[1mREPORTING BUGS[0m
|
||||
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
|
||||
Report any translation bugs to <https://translationproject.org/team/>
|
||||
|
||||
[1mCOPYRIGHT[0m
|
||||
Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU
|
||||
GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
|
||||
This is free software: you are free to change and redistribute it.
|
||||
There is NO WARRANTY, to the extent permitted by law.
|
||||
|
||||
[1mSEE ALSO[0m
|
||||
Full documentation <https://www.gnu.org/software/coreutils/dd>
|
||||
or available locally via: info '(coreutils) dd invocation'
|
||||
|
||||
GNU coreutils 9.3 April 2023 [4mDD[24m(1)
|
1
SCR3.1/TP2/coherence-test.txt
Normal file
1
SCR3.1/TP2/coherence-test.txt
Normal file
@ -0,0 +1 @@
|
||||
L'asr, c'est bien!!
|
96
SCR3.1/TP2/coherence.c
Normal file
96
SCR3.1/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();
|
||||
}
|
||||
}
|
BIN
SCR3.1/TP2/copy
Executable file
BIN
SCR3.1/TP2/copy
Executable file
Binary file not shown.
35
SCR3.1/TP2/copy.c
Normal file
35
SCR3.1/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
SCR3.1/TP2/copy_std
Executable file
BIN
SCR3.1/TP2/copy_std
Executable file
Binary file not shown.
32
SCR3.1/TP2/eraser.c
Normal file
32
SCR3.1/TP2/eraser.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv){
|
||||
int fd, read_bytes, wrote_bytes;
|
||||
if (argc != 2){
|
||||
printf("Usage : %s <path_to_the_file_to_erase>", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
struct stat sf;
|
||||
if (stat(argv[1], &sb) == -1){
|
||||
perror("stat");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (sb.st_nlink >= (nlink_t) 1) {
|
||||
unlink(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else {
|
||||
fd = open(argv[1], O_SYNC|O_RDONLY);
|
||||
if (fd == -1){
|
||||
perror("fd");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
155
SCR3.1/TP2/fadvise.c
Normal file
155
SCR3.1/TP2/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
SCR3.1/TP2/my_cp_map.c
Normal file
70
SCR3.1/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;
|
||||
}
|
36
SCR3.1/TP2/stdcopy.c
Normal file
36
SCR3.1/TP2/stdcopy.c
Normal file
@ -0,0 +1,36 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define BLOCK_SIZE 1
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fin, *fout;
|
||||
char buf[BLOCK_SIZE];
|
||||
|
||||
assert( argc == 3 );
|
||||
|
||||
fin = fopen(argv[1],"r");
|
||||
assert( fin >= 0 );
|
||||
|
||||
fout = fopen(argv[2],"w");
|
||||
assert( fout >= 0 );
|
||||
|
||||
while(1){
|
||||
ssize_t nb_read;
|
||||
nb_read = fread(buf,sizeof(char),BLOCK_SIZE,fin);
|
||||
if (nb_read <= 0)
|
||||
break;
|
||||
fwrite(buf,sizeof(char),BLOCK_SIZE,fout);
|
||||
}
|
||||
|
||||
fclose(fin);
|
||||
fclose(fout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
BIN
SCR3.1/TP2/toto
Normal file
BIN
SCR3.1/TP2/toto
Normal file
Binary file not shown.
BIN
SCR3.1/TP2/toto.dut
Normal file
BIN
SCR3.1/TP2/toto.dut
Normal file
Binary file not shown.
BIN
SCR3.1/TP2/toto2
Normal file
BIN
SCR3.1/TP2/toto2
Normal file
Binary file not shown.
BIN
SCR3.1/TP2/totos
Normal file
BIN
SCR3.1/TP2/totos
Normal file
Binary file not shown.
5
SCR3.1/TP2/tp2_reponses.txt
Normal file
5
SCR3.1/TP2/tp2_reponses.txt
Normal file
@ -0,0 +1,5 @@
|
||||
Exercice 1)
|
||||
Lors de la copie, fread créé un buffer de 8192 octets, ce qui accèlere la vitesse d'échange
|
||||
|
||||
Exercice 2)
|
||||
|
Loading…
Reference in New Issue
Block a user