diff --git a/TP_PROG_SYS_COPYBENCH/Makefile b/TP_PROG_SYS_COPYBENCH/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..f4ba2f0849f65b9f737970277c2b8e3c763b64e2 --- /dev/null +++ b/TP_PROG_SYS_COPYBENCH/Makefile @@ -0,0 +1,19 @@ +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 + diff --git a/TP_PROG_SYS_COPYBENCH/TP_prog_sys.c b/TP_PROG_SYS_COPYBENCH/TP_prog_sys.c new file mode 100644 index 0000000000000000000000000000000000000000..bc3fda2ac84e182e09c8c6600d3d0dd13dd5092e --- /dev/null +++ b/TP_PROG_SYS_COPYBENCH/TP_prog_sys.c @@ -0,0 +1,157 @@ +#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; +} diff --git a/TP_PROG_SYS_COPYBENCH/copy.c b/TP_PROG_SYS_COPYBENCH/copy.c new file mode 100644 index 0000000000000000000000000000000000000000..2a730e29cf295f7a09538a6c31fa5a3d3d94744d --- /dev/null +++ b/TP_PROG_SYS_COPYBENCH/copy.c @@ -0,0 +1,58 @@ +#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 diff --git a/TP_PROG_SYS_COPYBENCH/copy.h b/TP_PROG_SYS_COPYBENCH/copy.h new file mode 100644 index 0000000000000000000000000000000000000000..f3a9a60f9fee43e1396e946c01a42c27aeaed79a --- /dev/null +++ b/TP_PROG_SYS_COPYBENCH/copy.h @@ -0,0 +1,16 @@ +#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 diff --git a/TP_PROG_SYS_COPYBENCH/copy.o b/TP_PROG_SYS_COPYBENCH/copy.o new file mode 100644 index 0000000000000000000000000000000000000000..4412a55248c7001b4b61ce431ae6d54087379ccf Binary files /dev/null and b/TP_PROG_SYS_COPYBENCH/copy.o differ diff --git a/TP_PROG_SYS_COPYBENCH/copybench b/TP_PROG_SYS_COPYBENCH/copybench new file mode 100755 index 0000000000000000000000000000000000000000..a5f961be56b1695f588aae9db47b9a5d6c59f5ca Binary files /dev/null and b/TP_PROG_SYS_COPYBENCH/copybench differ diff --git a/TP_PROG_SYS_COPYBENCH/copybench.c b/TP_PROG_SYS_COPYBENCH/copybench.c new file mode 100644 index 0000000000000000000000000000000000000000..c56400b576af40a07c4511de318acb10d40c7ff3 --- /dev/null +++ b/TP_PROG_SYS_COPYBENCH/copybench.c @@ -0,0 +1,46 @@ +#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 diff --git a/TP_PROG_SYS_COPYBENCH/copyf.c b/TP_PROG_SYS_COPYBENCH/copyf.c new file mode 100644 index 0000000000000000000000000000000000000000..b228aa3a3952d246dc7264d9765ec22f5c72aed8 --- /dev/null +++ b/TP_PROG_SYS_COPYBENCH/copyf.c @@ -0,0 +1,58 @@ +#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 diff --git a/TP_PROG_SYS_COPYBENCH/copyf.o b/TP_PROG_SYS_COPYBENCH/copyf.o new file mode 100644 index 0000000000000000000000000000000000000000..bf3860d0cf92cc3ded8b8e3ed12ff83e4a73b907 Binary files /dev/null and b/TP_PROG_SYS_COPYBENCH/copyf.o differ diff --git a/TP_PROG_SYS_COPYBENCH/copyimage.jpg b/TP_PROG_SYS_COPYBENCH/copyimage.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/TP_PROG_SYS_COPYBENCH/image.jpg b/TP_PROG_SYS_COPYBENCH/image.jpg new file mode 100644 index 0000000000000000000000000000000000000000..af13b3af9631d80f064068ad484f9bfbd434c5cb Binary files /dev/null and b/TP_PROG_SYS_COPYBENCH/image.jpg differ diff --git a/TP_PROG_SYS_COPYBENCH/main.o b/TP_PROG_SYS_COPYBENCH/main.o new file mode 100644 index 0000000000000000000000000000000000000000..a5f5b00c6c5540bb86417a5ed912b6807bdbc2b4 Binary files /dev/null and b/TP_PROG_SYS_COPYBENCH/main.o differ