From a237f8b2ed274cb9374f522e093c4b780c952273 Mon Sep 17 00:00:00 2001 From: Jonathan Twite <jonathan.twite@student.uclouvain.be> Date: Tue, 14 Apr 2020 01:01:16 +0200 Subject: [PATCH] Update prime_divs.c (commented for better readability) --- prime_divs.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/prime_divs.c b/prime_divs.c index a18db11..129ec9b 100644 --- a/prime_divs.c +++ b/prime_divs.c @@ -11,50 +11,39 @@ #include <string.h> #include "fileFeatures.h" - -// int max value: 2 147 483 647 -// long max value: 9223372 036 854 775 807 - - -// char *filepath = "/home/jtwite/Desktop/test"; - -// Remplacez-le contenu par le chemin relatif du fichier que vous souhaitez tester. - - - int main(int argc, char *argv[]) { struct timeval stop, start; gettimeofday(&start, NULL); - printf("argv[1]: %s\n", argv[1]); - printf("argv[2]: %s\n", argv[2]); + printf("argv[1]: %s\n", argv[1]); // argv[1] = input file path + printf("argv[2]: %s\n", argv[2]); // argv[2] = output file path - //open the file + //open the fileInput int fileInput = open(argv[1], O_RDONLY); if (fileInput <= 0) exit(1); - // get the fileInput size + // get the fileInput size, map it in the virtual &dress space and close fileInput directory struct stat sb; fstat(fileInput, &sb); char *file_in_memory = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fileInput, 0); close(fileInput); - // Create a new file struct in which the output is writen - FILE *fileOutput = fopen(argv[2], "w"); + + FILE *fileOutput = fopen(argv[2], "w"); // Create the file output if(fileOutput == NULL) exit(-2); - //go through the mapped file to get numbers and write them in fileOutput - + + //go through the mapped file, read numbers and write them in fileOutput factorize(file_in_memory, fileOutput); - //close everything + // unmap fileInput and close fileOutput munmap(file_in_memory, sb.st_size); - fclose(fileOutput); + // print out the computation performance gettimeofday(&stop, NULL); - printf("took %lu µs\n", (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); + printf("%s factorized into %s.\ntook %lu µs\n", argv[1], argv[2], (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec); return 0; } -- GitLab