Skip to content
Snippets Groups Projects
Commit 1019f8ed authored by stephane.chappuis1's avatar stephane.chappuis1
Browse files

Ajout pour TP

parent 3e23711e
No related branches found
No related tags found
No related merge requests found
CC = gcc
OBJS = main.o copy.o copyf.o
CFLAGS = -g -Wall -c
copybench: $(OBJS)
$(CC) main.o copy.o copyf.o -o copybench
main.o: copybench.c copy.h
$(CC) -c copybench.c -o main.o
copy.o: copy.c copy.h
$(CC) -c copy.c -o copy.o
copyf.o: copyf.c copy.h
$(CC) -c copyf.c -o copyf.o
clean:
rm -f $(OBJS) copybench.o copybench
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <time.h>
int copy(char *src, char *dst, unsigned int buf_size);
int copyf(char *src, char *dst, unsigned int buf_size);
int copy(char *src, char *dst, unsigned int buf_size)
{
int src_file, dest_file;
size_t byte_read, byte_written;
char *buffer;
int total_byte = 0;
src_file = open(src, O_RDONLY);
if (src_file < 0)
{
fprintf(stderr, "Error opening source file %s: %s\n", src, strerror(errno));
return -1;
}
dest_file = open(dst, O_CREAT | O_RDONLY | O_TRUNC, 0644);
if (dest_file < 0)
{
fprintf(stderr, "Error opening destination file %s: %s\n", dst, strerror(errno));
close(src_file);
return -1;
}
buffer = (char *)malloc(buf_size);
if (!buffer)
{
fprintf(stderr, "Err allocating buffer : %s\n", strerror(errno));
close(dest_file);
close(src_file);
return -1;
}
while ((byte_read = read(src_file, buffer, buf_size)) > 0)
{
byte_written = write(dest_file, buffer, byte_read);
if (byte_written != byte_read)
{
fprintf(stderr, "Err dest file : %s\n", strerror(errno));
free(buffer);
close(dest_file);
close(src_file);
return -1;
}
total_byte += byte_written;
}
if (byte_read < 0)
{
fprintf(stderr, "Err src file %s\n", strerror(errno));
}
free(buffer);
close(dest_file);
close(src_file);
return 0;
}
int copyf(char *src, char *dst, unsigned int buf_size){
FILE *src_file, *dest_file;
size_t byte_read, byte_written;
char *buffer;
int total_byte = 0;
src_file = fopen(src, "rb");
if (src_file < 0)
{
fprintf(stderr, "Error opening source file %s: %s\n", src, strerror(errno));
return -1;
}
dest_file = fopen(dst,"wb") ;
if (dest_file < 0)
{
fprintf(stderr, "Error opening destination file %s: %s\n", dst, strerror(errno));
fclose(src_file);
return -1;
}
buffer = (char *)malloc(buf_size);
if (!buffer)
{
fprintf(stderr, "Err allocating buffer : %s\n", strerror(errno));
fclose(dest_file);
fclose(src_file);
return -1;
}
while ((byte_read = fread(buffer,1,buf_size,src_file )) > 0)
{
byte_written = fwrite(buffer,1,buf_size,dest_file);
if (byte_written != byte_read)
{
fprintf(stderr, "Err dest file : %s\n", strerror(errno));
free(buffer);
fclose(dest_file);
fclose(src_file);
return -1;
}
total_byte += byte_written;
}
if (ferror(src_file))
{
fprintf(stderr, "Err src file %s\n", strerror(errno));
}
free(buffer);
fclose(dest_file);
fclose(src_file);
return 0;
}
void measure_copy_time(char *src, char *dst, unsigned int buf_size, int use_fread) {
struct timespec start, end;
int copied_bytes;
double elapsed_time;
clock_gettime(CLOCK_MONOTONIC, &start);
if (use_fread) {
copied_bytes = copyf(src, dst, buf_size);
} else {
copied_bytes = copy(src, dst, buf_size);
}
clock_gettime(CLOCK_MONOTONIC, &end);
elapsed_time = (end.tv_sec - start.tv_sec) * 1000.0;
elapsed_time += (end.tv_nsec - start.tv_nsec) / 1000000.0;
if (use_fread) {
printf("Copied (fread/fwrite) \"%s\" into \"%s\" (%dKB in chunks of %d bytes) in %.2f ms\n",
src, dst, copied_bytes / 1024, buf_size, elapsed_time);
} else {
printf("Copied (read/write) \"%s\" into \"%s\" (%dKB in chunks of %d bytes) in %.2f ms\n",
src, dst, copied_bytes / 1024, buf_size, elapsed_time);
}
}
int main()
{
int buffer[10000];
return 0;
}
#include "copy.h"
int copy(char *src, char *dst, unsigned int buf_size)
{
int src_file, dest_file;
size_t byte_read, byte_written;
char *buffer;
int total_byte = 0;
src_file = open(src, O_RDONLY);
if (src_file < 0)
{
fprintf(stderr, "Error opening source file %s: %s\n", src, strerror(errno));
return -1;
}
dest_file = open(dst, O_CREAT | O_RDONLY | O_TRUNC, 0644);
if (dest_file < 0)
{
fprintf(stderr, "Error opening destination file %s: %s\n", dst, strerror(errno));
close(src_file);
return -1;
}
buffer = (char *)malloc(buf_size);
if (!buffer)
{
fprintf(stderr, "Err allocating buffer : %s\n", strerror(errno));
close(dest_file);
close(src_file);
return -1;
}
while ((byte_read = read(src_file, buffer, buf_size)) > 0)
{
byte_written = write(dest_file, buffer, byte_read);
if (byte_written != byte_read)
{
fprintf(stderr, "Err dest file : %s\n", strerror(errno));
free(buffer);
close(dest_file);
close(src_file);
return -1;
}
total_byte += byte_written;
}
if (byte_read < 0)
{
fprintf(stderr, "Err src file %s\n", strerror(errno));
}
free(buffer);
close(dest_file);
close(src_file);
return 0;
}
\ No newline at end of file
#ifndef _COPY_H_
#define _COPY_H_
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <time.h>
int copy(char *src, char *dst, unsigned int buf_size);
int copyf(char *src, char *dst, unsigned int buf_size);
void measure_copy_time(char *src, char *dst, unsigned int buf_size, int use_fread);
#endif
\ No newline at end of file
File added
File added
#include "copy.h"
void measure_copy_time(char *src, char *dst, unsigned int buf_size, int use_fread) {
struct timespec start, end;
int copied_bytes;
double elapsed_time;
clock_gettime(CLOCK_MONOTONIC, &start);
if (use_fread) {
copied_bytes = copyf(src, dst, buf_size);
} else {
copied_bytes = copy(src, dst, buf_size);
}
clock_gettime(CLOCK_MONOTONIC, &end);
elapsed_time = (end.tv_sec - start.tv_sec) * 1000.0;
elapsed_time += (end.tv_nsec - start.tv_nsec) / 1000000.0;
if (use_fread) {
printf("Copied (fread/fwrite) \"%s\" into \"%s\" (%dKB in chunks of %d bytes) in %.2f ms\n",
src, dst, copied_bytes / 1024, buf_size, elapsed_time);
} else {
printf("Copied (read/write) \"%s\" into \"%s\" (%dKB in chunks of %d bytes) in %.2f ms\n",
src, dst, copied_bytes / 1024, buf_size, elapsed_time);
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s SRC DST\n", argv[0]);
return 1;
}
char *src = argv[1];
char *dst = argv[2];
measure_copy_time(src, dst, 1, 0);
// measure_copy_time(src, dst, 32768, 0);
// measure_copy_time(src, dst, 1, 1);
// measure_copy_time(src, dst, 32768, 1);
return 0;
}
\ No newline at end of file
#include "copy.h"
int copyf(char *src, char *dst, unsigned int buf_size){
FILE *src_file, *dest_file;
size_t byte_read, byte_written;
char *buffer;
int total_byte = 0;
src_file = fopen(src, "wb");
if (src_file != NULL)
{
fprintf(stderr, "Error opening source file %s: %s\n", src, strerror(errno));
return -1;
}
dest_file = fopen(dst,"w") ;
if (dest_file != NULL)
{
fprintf(stderr, "Error opening destination file %s: %s\n", dst, strerror(errno));
fclose(src_file);
return -1;
}
buffer = malloc(buf_size);
if (!buffer)
{
fprintf(stderr, "Err allocating buffer : %s\n", strerror(errno));
fclose(dest_file);
fclose(src_file);
return -1;
}
while ((byte_read = fread(buffer,1,buf_size,src_file )) > 0)
{
byte_written = fwrite(buffer,1,buf_size,dest_file);
if (byte_written != byte_read)
{
fprintf(stderr, "Err dest file : %s\n", strerror(errno));
free(buffer);
fclose(dest_file);
fclose(src_file);
return -1;
}
total_byte += byte_written;
}
if (ferror(src_file))
{
fprintf(stderr, "Err src file %s\n", strerror(errno));
}
free(buffer);
fclose(dest_file);
fclose(src_file);
return 0;
}
\ No newline at end of file
File added
TP_PROG_SYS_COPYBENCH/image.jpg

749 KiB

File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment