From ac814daa017b7d82a07d65e2b0cb087d74d00058 Mon Sep 17 00:00:00 2001 From: erik770 Date: Sat, 30 Oct 2021 01:05:15 +0300 Subject: [PATCH 01/28] 2 working libs --- .gitignore | 2 + CMakeLists.txt | 22 +++ README.md | 2 + lib/utils/CMakeLists.txt | 4 + lib/utils/include/utils.h | 36 ++++ lib/utils/include/word_search_interface.h | 5 + lib/utils/src/utils.c | 155 ++++++++++++++++++ lib/word_search/CMakeLists.txt | 4 + lib/word_search/src/word_search.c | 35 ++++ lib/word_search_procs/CMakeLists.txt | 4 + lib/word_search_procs/src/word_search_procs.c | 125 ++++++++++++++ main.c | 19 +++ 12 files changed, 413 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 lib/utils/CMakeLists.txt create mode 100644 lib/utils/include/utils.h create mode 100644 lib/utils/include/word_search_interface.h create mode 100644 lib/utils/src/utils.c create mode 100644 lib/word_search/CMakeLists.txt create mode 100644 lib/word_search/src/word_search.c create mode 100644 lib/word_search_procs/CMakeLists.txt create mode 100644 lib/word_search_procs/src/word_search_procs.c create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6c3f3e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build +/cmake-build-debug diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4e70640 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.17) +project(HW2) +set(CMAKE_C_STANDARD 99) +#set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -pthread") +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -pthread") + +#add_compile_options("--coverage") + +add_subdirectory(lib/utils) +add_subdirectory(lib/word_search) +add_subdirectory(lib/word_search_procs) + +add_executable(IW_2 main.c) +target_link_libraries(IW_2 utils word_search) + +#enable_testing() +#find_package(GTest REQUIRED) +#add_executable(gtests tests/test.cpp) +#include_directories(${GTEST_INCLUDE_DIRS}) +#target_link_libraries(gtests ${GTEST_LIBRARIES} utils gcov) \ No newline at end of file diff --git a/README.md b/README.md index bcf8eb8..f48b6c5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # HW2 Решение второго ИЗ из курса по углубленному программированию на языке C/C++ в технопарке вк компани! лучшего технического во вселенной +###Вариант #17 +Сравните и выведите в консоль время работы последовательного и параллельного с использованием нескольких процессов алгоритмов, каждый из которых выделяет в динамической памяти символьный массив размером 100 Мб и выполняет поиск самого длинного слова в тексте. Словом считается последовательность, состоящая из букв и ограниченная пробелами. \ No newline at end of file diff --git a/lib/utils/CMakeLists.txt b/lib/utils/CMakeLists.txt new file mode 100644 index 0000000..ed43a60 --- /dev/null +++ b/lib/utils/CMakeLists.txt @@ -0,0 +1,4 @@ +add_library(utils STATIC src/utils.c include/utils.h) + +target_include_directories(utils PUBLIC include) +#target_link_libraries(word_search PUBLIC utils) \ No newline at end of file diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h new file mode 100644 index 0000000..6823ab2 --- /dev/null +++ b/lib/utils/include/utils.h @@ -0,0 +1,36 @@ +#pragma once + +#include "stdio.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SYMBOLS "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm " +#define NUMB_OF_SPACES 5 +//#define MIN_SIZE 131072 // 128*1024 128 кБайт +//#define MAX_SIZE 1048576 //Мбайт +#define MIN_SIZE 50 // 128*1024 128 кБайт +#define MAX_SIZE 150 //Мбайт +#define BUFF_SIZE 500 + +char *create_words(); + +bool close_file(FILE *f); + +FILE *open_file(const char *path); + +size_t word_counter(const char *string_of_words); + +char **create_array_of_words(char *string_of_words); + +int write_words_to_file(char *words, const char *file_path); + + + + diff --git a/lib/utils/include/word_search_interface.h b/lib/utils/include/word_search_interface.h new file mode 100644 index 0000000..331a81d --- /dev/null +++ b/lib/utils/include/word_search_interface.h @@ -0,0 +1,5 @@ +#pragma once + +#include "utils.h" + +char *find_longest_word(char *string_of_words); diff --git a/lib/utils/src/utils.c b/lib/utils/src/utils.c new file mode 100644 index 0000000..d74e8af --- /dev/null +++ b/lib/utils/src/utils.c @@ -0,0 +1,155 @@ +#include "utils.h" + +char *create_words() { + srand(time(NULL)); + size_t size = rand() % (MAX_SIZE - MIN_SIZE + 1) + MIN_SIZE; +// printf("%d\n", size); + char *words = (char *) malloc(size * sizeof(char)); + if (words == NULL) { + return NULL; + } + words[0] = SYMBOLS[rand() % (strlen(SYMBOLS) - NUMB_OF_SPACES)]; + for (size_t i = 1; i < size - 1; i++) { + words[i] = SYMBOLS[rand() % strlen(SYMBOLS)]; + if(words[i] == ' ' && words[i-1] == ' '){ + words[i] = SYMBOLS[rand() % (strlen(SYMBOLS) - NUMB_OF_SPACES)]; + } + } + if (words[size - 2] == ' '){ + words[size - 2] = '\0'; + }else { + words[size - 1] = '\0'; + } + return words; +} + +size_t word_counter(const char* string_of_words) { + if(string_of_words == NULL) { + return -1; + } +// printf("%s\n", string_of_words); + char* copy = (char*)malloc(sizeof(char) *(strlen(string_of_words)+1)); + if (copy == NULL) { + return -1; + } + strcpy(copy,string_of_words); + size_t word_counter = 0; + + char* word = strtok(copy, " "); + if (word == NULL) { + free(copy); + return -1; + } + while (word != NULL) { + + word = strtok(NULL, " "); + word_counter++; + } + free(copy); +// printf("%lu", word_counter); + return word_counter; +} + +//char* delete_spaces (char* string_of_words){ +// +//} + +char** create_array_of_words(char* string_of_words) { +// if(string_of_words == NULL) { +// return NULL; +// } +// size_t number_of_words = word_counter(string_of_words); +// if (number_of_words == -1) { +// free(string_of_words); +// return NULL; +// } +// +// char** array_of_words = (char**) malloc(number_of_words * sizeof (char*)); +// if(array_of_words == NULL) { +// free(string_of_words); +// return NULL; +// } +// for (size_t i = 0; i < number_of_words; i++) { +// array_of_words[i] = (char*) malloc(BUFF_SIZE *sizeof(char)); +// if(array_of_words[i] == NULL){ +// for (; i > 0 ; i--){ +// free(array_of_words[i-1]); +// } +// free(array_of_words); +// free(string_of_words); +// return NULL; +// } +// } +// +// char* copy = (char*)malloc(sizeof(char) *(strlen(string_of_words)+1)); +// if (copy == NULL) { +// for (size_t i = 0; i < number_of_words ; i++){ +// free(array_of_words[i]); +// } +// free(array_of_words); +// free(string_of_words); +// return NULL; +// } +// strcpy(copy,string_of_words); +// size_t counter = 0; +// array_of_words[counter] = strtok(copy, " "); +// if(array_of_words[0] == NULL) { +// for (size_t i = 0; i < number_of_words; i++){ +// free(array_of_words[0]); +// } +// free(array_of_words); +// return NULL; +// } +// +// while (array_of_words[counter] != NULL){ +// counter++; +// array_of_words[counter] = strtok(NULL, " "); +// } +// +// if (counter != number_of_words) { +// for (; counter > 0 ; counter--){ +// free(array_of_words[counter-1]); +// } +// free(array_of_words); +// return NULL; +// } +//// free(string_of_words); +// printf("%lu", counter); +// for(size_t i = 0; i = max_len) { + max_len = strlen(words_array[j]); + if(longest_word != NULL) { + free(longest_word); + } + longest_word = (char*)malloc(BUFF_SIZE * sizeof(char)); + strcpy(longest_word, words_array[j]); + } + } + + + }else { + for(size_t j = k * part_size; j < number_of_words; ++j) { + if(strlen(words_array[j]) >= max_len) { + max_len = strlen(words_array[j]); + if(longest_word != NULL) { + free(longest_word); + } + longest_word = (char*)malloc(BUFF_SIZE * sizeof(char)); + strcpy(longest_word, words_array[j]); + } + } + } + + if(longest_word == NULL) { + exit(EXIT_FAILURE); + } + + message_buff q_buff = {1, ""}; + strcpy(q_buff.mtext, longest_word); + + if(msgsnd(q_id, (struct msgbuf *)&q_buff, strlen(q_buff.mtext) + 1, 0) == -1) { + exit(EXIT_FAILURE); + } + + + free(longest_word); + for(size_t i = 0; i < number_of_words; i++) { + if(words_array[i] != NULL) + free(words_array[i]); + } + free(words_array); + free(string_of_words); + exit(EXIT_SUCCESS); + } + } + + for (size_t i = 0; i < number_of_procs; ++i) { + if (waitpid(pids[i], &status, 0) != pids[i]) { + return NULL; + } + } + + char* longest_word = (char*)malloc(BUFF_SIZE * sizeof(char)); + size_t max_len = 0; + + for (size_t i = 0; i < number_of_procs; ++i) { + message_buff q_buff; + + if(msgrcv(q_id, (struct msgbuf *)&q_buff, BUFF_SIZE, 1, 0) == -1) { + return NULL; + } + + if(q_buff.mtext[0] == '\0') { + printf("can't receive max_word\n"); + return NULL; + } + + if(max_len <= strlen(q_buff.mtext)) { + free(longest_word); + longest_word = (char*)malloc(BUFF_SIZE * sizeof(char)); + + if(longest_word == NULL) { + printf("can't allocate mem to no_procs\n"); + return NULL; + } + + strcpy(longest_word, q_buff.mtext); + max_len = strlen(q_buff.mtext); + } + } + + for(size_t i = 0; i < number_of_words; i++) { + if(words_array[i] != NULL) + free(words_array[i]); + } + free(words_array); + + return longest_word; +} + diff --git a/main.c b/main.c new file mode 100644 index 0000000..9d6d315 --- /dev/null +++ b/main.c @@ -0,0 +1,19 @@ +#include "word_search_interface.h" + +int main () { +// char*a = find_longest_word(create_words()); +// char* a = find_longest_word(create_words()); + char* words = create_words(); + char* a = find_longest_word(words); + printf("%s", a); + +// char** a = create_array_of_words(words); +// size_t b =word_counter(words); +// for(size_t i = 0; i < b; i++) { +// free(a[i]); +// } + free(a); +// word_counter(words); + free(words); + return 0; +} \ No newline at end of file From aec1a62e4482f6b6c9bb21408d650bfc529e2ac0 Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 31 Oct 2021 14:47:01 +0300 Subject: [PATCH 02/28] add test --- .gitignore | 3 +- CMakeLists.txt | 32 ++-- README.md | 2 +- lib/utils/include/utils.h | 23 ++- lib/utils/include/word_search_interface.h | 8 + lib/utils/src/utils.c | 139 +++++------------- lib/word_search/src/word_search.c | 18 +-- lib/word_search_procs/src/word_search_procs.c | 119 ++++++++++----- main.c | 19 --- tests/stress_test.cpp | 25 ++++ tests/test.cpp | 84 +++++++++++ 11 files changed, 283 insertions(+), 189 deletions(-) delete mode 100644 main.c create mode 100644 tests/stress_test.cpp create mode 100644 tests/test.cpp diff --git a/.gitignore b/.gitignore index 6c3f3e3..be1c665 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -/build /cmake-build-debug +/build +/linters \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e70640..f39138f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,22 +1,32 @@ cmake_minimum_required(VERSION 3.17) project(HW2) set(CMAKE_C_STANDARD 99) -#set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 17) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -pthread") -#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -pthread") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -pthread --coverage") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -pthread --coverage") -#add_compile_options("--coverage") +add_compile_options("--coverage") add_subdirectory(lib/utils) add_subdirectory(lib/word_search) add_subdirectory(lib/word_search_procs) -add_executable(IW_2 main.c) -target_link_libraries(IW_2 utils word_search) +enable_testing() +find_package(GTest REQUIRED) +add_executable(tests tests/test.cpp) +include_directories(${GTEST_INCLUDE_DIRS}) +target_link_libraries(tests ${GTEST_LIBRARIES} utils word_search gcov) -#enable_testing() -#find_package(GTest REQUIRED) -#add_executable(gtests tests/test.cpp) -#include_directories(${GTEST_INCLUDE_DIRS}) -#target_link_libraries(gtests ${GTEST_LIBRARIES} utils gcov) \ No newline at end of file +add_executable(tests_procs tests/test.cpp) +include_directories(${GTEST_INCLUDE_DIRS}) +target_link_libraries(tests_procs ${GTEST_LIBRARIES} utils word_search_procs gcov) + + +add_executable(stress_test tests/stress_test.cpp) +include_directories(${GTEST_INCLUDE_DIRS}) +target_link_libraries(stress_test ${GTEST_LIBRARIES} utils word_search gcov) + +add_executable(stress_test_procs tests/stress_test.cpp) +include_directories(${GTEST_INCLUDE_DIRS}) +target_link_libraries(stress_test_procs ${GTEST_LIBRARIES} utils word_search_procs gcov) diff --git a/README.md b/README.md index f48b6c5..1216aff 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ # HW2 Решение второго ИЗ из курса по углубленному программированию на языке C/C++ в технопарке вк компани! лучшего технического во вселенной -###Вариант #17 +### Вариант #17 Сравните и выведите в консоль время работы последовательного и параллельного с использованием нескольких процессов алгоритмов, каждый из которых выделяет в динамической памяти символьный массив размером 100 Мб и выполняет поиск самого длинного слова в тексте. Словом считается последовательность, состоящая из букв и ограниченная пробелами. \ No newline at end of file diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index 6823ab2..d80a7a9 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -1,5 +1,9 @@ #pragma once +#if defined(__cplusplus) +extern "C" { +#endif + #include "stdio.h" #include #include @@ -13,24 +17,19 @@ #define SYMBOLS "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm " #define NUMB_OF_SPACES 5 -//#define MIN_SIZE 131072 // 128*1024 128 кБайт -//#define MAX_SIZE 1048576 //Мбайт -#define MIN_SIZE 50 // 128*1024 128 кБайт -#define MAX_SIZE 150 //Мбайт -#define BUFF_SIZE 500 -char *create_words(); +#define MIN_SIZE 131072 // 128*1024 128 кБайт +#define MAX_SIZE 1048576 // 100Мбайт -bool close_file(FILE *f); +#define BUFF_SIZE 500 -FILE *open_file(const char *path); +char *create_words(); size_t word_counter(const char *string_of_words); char **create_array_of_words(char *string_of_words); -int write_words_to_file(char *words, const char *file_path); - - - +#if defined(__cplusplus) +} +#endif diff --git a/lib/utils/include/word_search_interface.h b/lib/utils/include/word_search_interface.h index 331a81d..c0944a9 100644 --- a/lib/utils/include/word_search_interface.h +++ b/lib/utils/include/word_search_interface.h @@ -1,5 +1,13 @@ #pragma once +#if defined(__cplusplus) +extern "C" { +#endif + #include "utils.h" char *find_longest_word(char *string_of_words); + +#if defined(__cplusplus) +} +#endif diff --git a/lib/utils/src/utils.c b/lib/utils/src/utils.c index d74e8af..ecc8fb7 100644 --- a/lib/utils/src/utils.c +++ b/lib/utils/src/utils.c @@ -3,7 +3,7 @@ char *create_words() { srand(time(NULL)); size_t size = rand() % (MAX_SIZE - MIN_SIZE + 1) + MIN_SIZE; -// printf("%d\n", size); + char *words = (char *) malloc(size * sizeof(char)); if (words == NULL) { return NULL; @@ -11,144 +11,83 @@ char *create_words() { words[0] = SYMBOLS[rand() % (strlen(SYMBOLS) - NUMB_OF_SPACES)]; for (size_t i = 1; i < size - 1; i++) { words[i] = SYMBOLS[rand() % strlen(SYMBOLS)]; - if(words[i] == ' ' && words[i-1] == ' '){ + if (words[i] == ' ' && words[i - 1] == ' ') { words[i] = SYMBOLS[rand() % (strlen(SYMBOLS) - NUMB_OF_SPACES)]; } } - if (words[size - 2] == ' '){ + if (words[size - 2] == ' ') { words[size - 2] = '\0'; - }else { + } else { words[size - 1] = '\0'; } return words; } -size_t word_counter(const char* string_of_words) { - if(string_of_words == NULL) { - return -1; +size_t word_counter(const char *string_of_words) { + if (string_of_words == NULL) { + return 0; } -// printf("%s\n", string_of_words); - char* copy = (char*)malloc(sizeof(char) *(strlen(string_of_words)+1)); + + char *copy = (char *) malloc(sizeof(char) * (strlen(string_of_words) + 1)); if (copy == NULL) { - return -1; + return 0; } - strcpy(copy,string_of_words); + strcpy(copy, string_of_words); size_t word_counter = 0; - char* word = strtok(copy, " "); + char *word = strtok(copy, " "); if (word == NULL) { free(copy); - return -1; + return 0; } while (word != NULL) { - word = strtok(NULL, " "); word_counter++; } free(copy); -// printf("%lu", word_counter); return word_counter; } -//char* delete_spaces (char* string_of_words){ -// -//} - -char** create_array_of_words(char* string_of_words) { -// if(string_of_words == NULL) { -// return NULL; -// } -// size_t number_of_words = word_counter(string_of_words); -// if (number_of_words == -1) { -// free(string_of_words); -// return NULL; -// } -// -// char** array_of_words = (char**) malloc(number_of_words * sizeof (char*)); -// if(array_of_words == NULL) { -// free(string_of_words); -// return NULL; -// } -// for (size_t i = 0; i < number_of_words; i++) { -// array_of_words[i] = (char*) malloc(BUFF_SIZE *sizeof(char)); -// if(array_of_words[i] == NULL){ -// for (; i > 0 ; i--){ -// free(array_of_words[i-1]); -// } -// free(array_of_words); -// free(string_of_words); -// return NULL; -// } -// } -// -// char* copy = (char*)malloc(sizeof(char) *(strlen(string_of_words)+1)); -// if (copy == NULL) { -// for (size_t i = 0; i < number_of_words ; i++){ -// free(array_of_words[i]); -// } -// free(array_of_words); -// free(string_of_words); -// return NULL; -// } -// strcpy(copy,string_of_words); -// size_t counter = 0; -// array_of_words[counter] = strtok(copy, " "); -// if(array_of_words[0] == NULL) { -// for (size_t i = 0; i < number_of_words; i++){ -// free(array_of_words[0]); -// } -// free(array_of_words); -// return NULL; -// } -// -// while (array_of_words[counter] != NULL){ -// counter++; -// array_of_words[counter] = strtok(NULL, " "); -// } -// -// if (counter != number_of_words) { -// for (; counter > 0 ; counter--){ -// free(array_of_words[counter-1]); -// } -// free(array_of_words); -// return NULL; -// } -//// free(string_of_words); -// printf("%lu", counter); -// for(size_t i = 0; i 0; l--) { + free(words_arr[l - 1]); + } + free(string_of_words); + free(words_arr); + return NULL; + } } - while(string_of_words[j] != '\0') { - if(string_of_words[j] == ' ') { + while (string_of_words[j] != '\0') { + if (string_of_words[j] == ' ') { words_arr[k][h] = '\0'; ++j; ++k; h = 0; - } - else { + } else { words_arr[k][h++] = string_of_words[j++]; } } words_arr[k++][h++] = '\0'; - - for(size_t i = 0; i< words_count;i++) { - printf("%s\n",words_arr[i]); - } - return words_arr; } diff --git a/lib/word_search/src/word_search.c b/lib/word_search/src/word_search.c index f480af7..61ea65e 100644 --- a/lib/word_search/src/word_search.c +++ b/lib/word_search/src/word_search.c @@ -1,35 +1,31 @@ #include "word_search_interface.h" -char* find_longest_word(char* string_of_words) { - if(string_of_words == NULL) { +char *find_longest_word(char *string_of_words) { + if (string_of_words == NULL) { return NULL; } - printf("%s\n", string_of_words); - char* longest_word = (char*) malloc(strlen(string_of_words) * sizeof(char)); + char *longest_word = (char *) malloc(strlen(string_of_words) * sizeof(char)); if (longest_word == NULL) { free(string_of_words); return NULL; } - char* word = strtok(string_of_words, " "); + char *word = strtok(string_of_words, " "); if (word == NULL) { free(longest_word); return string_of_words; } strcpy(longest_word, word); while (word != NULL) { - - if(strlen(longest_word) < strlen(word)){ + if (strlen(longest_word) < strlen(word)) { strcpy(longest_word, word); } - if (!strcmp(longest_word, "")){ + if (!strcmp(longest_word, "")) { continue; } word = strtok(NULL, " "); } -// free(string_of_words); -// printf("\n%lu\n%s\n", strlen(longest_word), longest_word); return longest_word; -} \ No newline at end of file +} diff --git a/lib/word_search_procs/src/word_search_procs.c b/lib/word_search_procs/src/word_search_procs.c index 5cdc971..0282968 100644 --- a/lib/word_search_procs/src/word_search_procs.c +++ b/lib/word_search_procs/src/word_search_procs.c @@ -6,19 +6,18 @@ typedef struct { char mtext[BUFF_SIZE]; } message_buff; -char* find_longest_word(char* string_of_words) { - if(string_of_words == NULL) { - printf("err"); +char *find_longest_word(char *string_of_words) { + if (string_of_words == NULL) { return NULL; } - char** words_array = create_array_of_words(string_of_words); - if(words_array == NULL) { + char **words_array = create_array_of_words(string_of_words); + if (words_array == NULL) { free(string_of_words); return NULL; } size_t number_of_words = word_counter(string_of_words); size_t number_of_procs = sysconf(_SC_NPROCESSORS_ONLN); - size_t part_size = number_of_words / number_of_procs; + size_t part_size = number_of_words / number_of_procs; int status; key_t key = IPC_PRIVATE; @@ -28,49 +27,79 @@ char* find_longest_word(char* string_of_words) { for (size_t k = 0; k < number_of_procs; ++k) { pids[k] = fork(); if (pids[k] == 0) { - char* longest_word = NULL; + char *longest_word = NULL; size_t max_len = 0; - if (k != number_of_procs - 1){ - for(size_t j = k * part_size; j < (k + 1) * part_size; ++j) { - if(strlen(words_array[j]) >= max_len) { + if (k != number_of_procs - 1) { + for (size_t j = k * part_size; j < (k + 1) * part_size; ++j) { + if (strlen(words_array[j]) >= max_len) { max_len = strlen(words_array[j]); - if(longest_word != NULL) { + if (longest_word != NULL) { free(longest_word); } - longest_word = (char*)malloc(BUFF_SIZE * sizeof(char)); + longest_word = (char *) malloc(BUFF_SIZE * sizeof(char)); + if (longest_word == NULL) { + for (size_t i = 0; i < number_of_words; i++) { + if (words_array[i] != NULL) + free(words_array[i]); + } + free(words_array); + free(string_of_words); + exit(EXIT_FAILURE); + } strcpy(longest_word, words_array[j]); } } - }else { - for(size_t j = k * part_size; j < number_of_words; ++j) { - if(strlen(words_array[j]) >= max_len) { + } else { + for (size_t j = k * part_size; j < number_of_words; ++j) { + if (strlen(words_array[j]) >= max_len) { max_len = strlen(words_array[j]); - if(longest_word != NULL) { + if (longest_word != NULL) { free(longest_word); } - longest_word = (char*)malloc(BUFF_SIZE * sizeof(char)); + longest_word = (char *) malloc(BUFF_SIZE * sizeof(char)); + if (longest_word == NULL) { + for (size_t i = 0; i < number_of_words; i++) { + if (words_array[i] != NULL) + free(words_array[i]); + } + free(words_array); + free(string_of_words); + exit(EXIT_FAILURE); + } strcpy(longest_word, words_array[j]); } } } - if(longest_word == NULL) { + if (longest_word == NULL) { + for (size_t i = 0; i < number_of_words; i++) { + if (words_array[i] != NULL) + free(words_array[i]); + } + free(words_array); + free(string_of_words); exit(EXIT_FAILURE); } - message_buff q_buff = {1, ""}; + message_buff q_buff = {1,""}; strcpy(q_buff.mtext, longest_word); - if(msgsnd(q_id, (struct msgbuf *)&q_buff, strlen(q_buff.mtext) + 1, 0) == -1) { + if (msgsnd(q_id, (struct msgbuf *) &q_buff, strlen(q_buff.mtext) + 1, 0) == -1) { + free(longest_word); + for (size_t i = 0; i < number_of_words; i++) { + if (words_array[i] != NULL) + free(words_array[i]); + } + free(words_array); + free(string_of_words); exit(EXIT_FAILURE); } - free(longest_word); - for(size_t i = 0; i < number_of_words; i++) { - if(words_array[i] != NULL) + for (size_t i = 0; i < number_of_words; i++) { + if (words_array[i] != NULL) free(words_array[i]); } free(words_array); @@ -81,31 +110,53 @@ char* find_longest_word(char* string_of_words) { for (size_t i = 0; i < number_of_procs; ++i) { if (waitpid(pids[i], &status, 0) != pids[i]) { + for (size_t j = 0; j < number_of_words; j++) { + if (words_array[j] != NULL) + free(words_array[j]); + } + free(words_array); + free(string_of_words); return NULL; } } - char* longest_word = (char*)malloc(BUFF_SIZE * sizeof(char)); + char *longest_word = (char *) malloc(BUFF_SIZE * sizeof(char)); size_t max_len = 0; for (size_t i = 0; i < number_of_procs; ++i) { message_buff q_buff; - if(msgrcv(q_id, (struct msgbuf *)&q_buff, BUFF_SIZE, 1, 0) == -1) { + if (msgrcv(q_id, (struct msgbuf *) &q_buff, BUFF_SIZE, 1, 0) == -1) { + for (size_t j = 0; j < number_of_words; j++) { + if (words_array[j] != NULL) + free(words_array[j]); + } + free(words_array); + free(string_of_words); return NULL; } - if(q_buff.mtext[0] == '\0') { - printf("can't receive max_word\n"); + if (q_buff.mtext[0] == '\0') { + for (size_t j = 0; j < number_of_words; j++) { + if (words_array[j] != NULL) + free(words_array[j]); + } + free(words_array); + free(string_of_words); return NULL; } - if(max_len <= strlen(q_buff.mtext)) { + if (max_len <= strlen(q_buff.mtext)) { free(longest_word); - longest_word = (char*)malloc(BUFF_SIZE * sizeof(char)); + longest_word = (char *) malloc(BUFF_SIZE * sizeof(char)); - if(longest_word == NULL) { - printf("can't allocate mem to no_procs\n"); + if (longest_word == NULL) { + for (size_t j = 0; j < number_of_words; j++) { + if (words_array[j] != NULL) + free(words_array[j]); + } + free(words_array); + free(string_of_words); return NULL; } @@ -114,11 +165,11 @@ char* find_longest_word(char* string_of_words) { } } - for(size_t i = 0; i < number_of_words; i++) { - if(words_array[i] != NULL) + for (size_t i = 0; i < number_of_words; i++) { + if (words_array[i] != NULL) free(words_array[i]); } - free(words_array); + free(words_array); return longest_word; } diff --git a/main.c b/main.c deleted file mode 100644 index 9d6d315..0000000 --- a/main.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "word_search_interface.h" - -int main () { -// char*a = find_longest_word(create_words()); -// char* a = find_longest_word(create_words()); - char* words = create_words(); - char* a = find_longest_word(words); - printf("%s", a); - -// char** a = create_array_of_words(words); -// size_t b =word_counter(words); -// for(size_t i = 0; i < b; i++) { -// free(a[i]); -// } - free(a); -// word_counter(words); - free(words); - return 0; -} \ No newline at end of file diff --git a/tests/stress_test.cpp b/tests/stress_test.cpp new file mode 100644 index 0000000..52e31f3 --- /dev/null +++ b/tests/stress_test.cpp @@ -0,0 +1,25 @@ +#include +#include "word_search_interface.h" +#include + +#define NUM_OF_TESTS 10 + +int main() { + double timer = 0; + + for (size_t i = 0; i < NUM_OF_TESTS; i++) { + char *string_of_words = create_words(); + + clock_t begin = clock(); + char *longest_word = find_longest_word(string_of_words); + clock_t end = clock(); + + timer += (double) (end - begin) / CLOCKS_PER_SEC; + + free(string_of_words); + free(longest_word); + } + double avg_time = timer / NUM_OF_TESTS; + std::cout << "average time: " << avg_time << std::endl; + return 0; +} diff --git a/tests/test.cpp b/tests/test.cpp new file mode 100644 index 0000000..ebf0370 --- /dev/null +++ b/tests/test.cpp @@ -0,0 +1,84 @@ +#include +#include "word_search_interface.h" + +TEST(word_generation, word_generation) { + char *string_of_words = create_words(); + EXPECT_FALSE(string_of_words == NULL); + free(string_of_words); +} + +TEST(word_generation, word_generation_size) { + char *string_of_words = create_words(); + EXPECT_TRUE((strlen(string_of_words) > MIN_SIZE) && (strlen(string_of_words) < MAX_SIZE)); + free(string_of_words); +} + +TEST(word_counter, word_counter_invalid_input) { + char *pseudo_string = NULL; + size_t number_of_words = word_counter(pseudo_string); + EXPECT_TRUE(number_of_words == 0); +} + +TEST(array_creating, array_creating_invalid_input) { + char *pseudo_string = NULL; + char** arr = create_array_of_words(pseudo_string); + EXPECT_TRUE(arr == NULL); +} + +TEST(array_creating, array_creating) { + char *string_of_words = create_words(); + size_t number_of_words = word_counter(string_of_words); + char** arr = create_array_of_words(string_of_words); + for (size_t i = 0; i < number_of_words; ++i) { + EXPECT_TRUE(arr[i] != NULL); + } + EXPECT_TRUE(arr != NULL); + for (size_t i = 0; i < number_of_words; ++i) { + free(arr[i]); + } + free(string_of_words); + free(arr); +} + +TEST(array_creating, array_creating_const) { + char *string_of_words = create_words(); + size_t number_of_words = word_counter(string_of_words); + char* copy = (char*)malloc(MAX_SIZE* sizeof(char )); + copy = strcpy(copy, string_of_words); + char** arr = create_array_of_words(string_of_words); + EXPECT_TRUE(strcmp(copy, string_of_words) == 0); + for (size_t i = 0; i < number_of_words; ++i) { + free(arr[i]); + } + free(arr); + free(copy); + free(string_of_words); +} + +TEST(max_len_word, max_len_word_invalid_input) { + char *pseudo_string = NULL; + char* longest_word = find_longest_word(pseudo_string); + EXPECT_TRUE(longest_word == NULL); +} + + +TEST(max_len_word, max_len_word) { + char *string_of_words = create_words(); + size_t number_of_words = word_counter(string_of_words); + char** arr = create_array_of_words(string_of_words); + char* longest_word = find_longest_word(string_of_words); + for (size_t i = 0; i < number_of_words; ++i) { + EXPECT_TRUE(strlen(longest_word) >= strlen(arr[i])); + } + for (size_t i = 0; i < number_of_words; ++i) { + free(arr[i]); + } + free(arr); + free(longest_word); + free(string_of_words); +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} From a67e2acc0e57c47147f0dd562eed283ae41a0c6c Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 31 Oct 2021 15:09:55 +0300 Subject: [PATCH 03/28] add CI --- .github/workflows/actions.yml | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/actions.yml diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml new file mode 100644 index 0000000..ee7ca02 --- /dev/null +++ b/.github/workflows/actions.yml @@ -0,0 +1,38 @@ +name: CI + +on: + push: + branches: [ dev ] + pull_request: + branches: + - dev + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: install references + run: sudo apt-get install libgtest-dev -y && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && cd lib && sudo cp *.a /usr/lib && sudo apt install build-essential && sudo apt install git && sudo apt-get install -y cppcheck && sudo apt-get install valgrind && sudo apt-get install -y lcov && sudo apt-get install -y gcovr + + - name: build + run: mkdir build && cd build && cmake .. && make + + - name: tests_under_valgrind + run: valgrind --leak-check=full --track-origins=yes ./build/tests && valgrind --leak-check=full --track-origins=yes --child-silent-after-fork=yes ./build/tests_procs + + - name: stress_tests_avg_time + run: ./build/stress_test && ./build/stress_test_procs + + - name: code coverage + run: lcov -t "HW_2" -o coverage.txt -c -d . && cp coverage.txt ../ + + - name: upload coverage to Codecov + uses: codecov/codecov-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} + file: ./coverage.txt + flags: unittests \ No newline at end of file From 68b71292502fe394e50da473cd4d07a5733fe322 Mon Sep 17 00:00:00 2001 From: erik770 <80068140+erik770@users.noreply.github.com> Date: Sun, 31 Oct 2021 15:15:49 +0300 Subject: [PATCH 04/28] coverage upload --- .github/workflows/actions.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index ee7ca02..2f08187 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -33,6 +33,6 @@ jobs: - name: upload coverage to Codecov uses: codecov/codecov-action@v1 with: - token: ${{ secrets.CODECOV_TOKEN }} + token: 18e3c0bb-f437-4bf4-a063-7cf00c7b31ad file: ./coverage.txt - flags: unittests \ No newline at end of file + flags: unittests From 27fadf54debd73853caf713651993e164f11118a Mon Sep 17 00:00:00 2001 From: erik770 <80068140+erik770@users.noreply.github.com> Date: Sun, 31 Oct 2021 15:29:23 +0300 Subject: [PATCH 05/28] codecov badge --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1216aff..bf0a556 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ # HW2 Решение второго ИЗ из курса по углубленному программированию на языке C/C++ в технопарке вк компани! лучшего технического во вселенной + +[![codecov](https://codecov.io/gh/erik770/HW2/branch/main/graph/badge.svg?token=OLUXHW9U6S)](https://codecov.io/gh/erik770/HW2) + ### Вариант #17 -Сравните и выведите в консоль время работы последовательного и параллельного с использованием нескольких процессов алгоритмов, каждый из которых выделяет в динамической памяти символьный массив размером 100 Мб и выполняет поиск самого длинного слова в тексте. Словом считается последовательность, состоящая из букв и ограниченная пробелами. \ No newline at end of file +Сравните и выведите в консоль время работы последовательного и параллельного с использованием нескольких процессов алгоритмов, каждый из которых выделяет в динамической памяти символьный массив размером 100 Мб и выполняет поиск самого длинного слова в тексте. Словом считается последовательность, состоящая из букв и ограниченная пробелами. From 172f458cd40af1452bc8aee04d78bbbb9895e43c Mon Sep 17 00:00:00 2001 From: erik770 <80068140+erik770@users.noreply.github.com> Date: Sun, 31 Oct 2021 15:42:29 +0300 Subject: [PATCH 06/28] codecov fix --- .github/workflows/actions.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 2f08187..d1444d1 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -33,6 +33,5 @@ jobs: - name: upload coverage to Codecov uses: codecov/codecov-action@v1 with: - token: 18e3c0bb-f437-4bf4-a063-7cf00c7b31ad file: ./coverage.txt flags: unittests From b6fbf7b168e68389366ebbc40e79482587ce687d Mon Sep 17 00:00:00 2001 From: erik770 <80068140+erik770@users.noreply.github.com> Date: Sun, 31 Oct 2021 15:49:55 +0300 Subject: [PATCH 07/28] another one --- .github/workflows/actions.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index d1444d1..0fbfca7 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -33,5 +33,6 @@ jobs: - name: upload coverage to Codecov uses: codecov/codecov-action@v1 with: + token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.txt flags: unittests From 3ff60978fcbad49b9e21183cf9c4fec0be796578 Mon Sep 17 00:00:00 2001 From: erik770 <80068140+erik770@users.noreply.github.com> Date: Sun, 31 Oct 2021 16:13:44 +0300 Subject: [PATCH 08/28] delete badge --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bf0a556..af7ab42 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # HW2 Решение второго ИЗ из курса по углубленному программированию на языке C/C++ в технопарке вк компани! лучшего технического во вселенной -[![codecov](https://codecov.io/gh/erik770/HW2/branch/main/graph/badge.svg?token=OLUXHW9U6S)](https://codecov.io/gh/erik770/HW2) - ### Вариант #17 Сравните и выведите в консоль время работы последовательного и параллельного с использованием нескольких процессов алгоритмов, каждый из которых выделяет в динамической памяти символьный массив размером 100 Мб и выполняет поиск самого длинного слова в тексте. Словом считается последовательность, состоящая из букв и ограниченная пробелами. + +### Coverage +https://app.codecov.io/gh/erik770/HW2/commits?page=1 + From a24039962324e72d9c521338f7fa7e091d1a703d Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 31 Oct 2021 16:17:01 +0300 Subject: [PATCH 09/28] stress test is more stress --- lib/utils/include/utils.h | 4 ++-- tests/stress_test.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index d80a7a9..946c08e 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -18,8 +18,8 @@ extern "C" { #define SYMBOLS "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm " #define NUMB_OF_SPACES 5 -#define MIN_SIZE 131072 // 128*1024 128 кБайт -#define MAX_SIZE 1048576 // 100Мбайт +#define MIN_SIZE 1048576 // 1MБайт +#define MAX_SIZE 104857600 // 100Мбайт #define BUFF_SIZE 500 diff --git a/tests/stress_test.cpp b/tests/stress_test.cpp index 52e31f3..6de07e8 100644 --- a/tests/stress_test.cpp +++ b/tests/stress_test.cpp @@ -2,7 +2,7 @@ #include "word_search_interface.h" #include -#define NUM_OF_TESTS 10 +#define NUM_OF_TESTS 50 int main() { double timer = 0; From 4d7760bd92fe251845bf9c429143f7d199a01ce6 Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 31 Oct 2021 16:25:39 +0300 Subject: [PATCH 10/28] stress test is less stress --- lib/utils/include/utils.h | 2 +- tests/stress_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index 946c08e..715f6a0 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -18,7 +18,7 @@ extern "C" { #define SYMBOLS "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm " #define NUMB_OF_SPACES 5 -#define MIN_SIZE 1048576 // 1MБайт +#define MIN_SIZE 131072 // 128КБайт #define MAX_SIZE 104857600 // 100Мбайт #define BUFF_SIZE 500 diff --git a/tests/stress_test.cpp b/tests/stress_test.cpp index 6de07e8..9ebc0a6 100644 --- a/tests/stress_test.cpp +++ b/tests/stress_test.cpp @@ -2,7 +2,7 @@ #include "word_search_interface.h" #include -#define NUM_OF_TESTS 50 +#define NUM_OF_TESTS 15 int main() { double timer = 0; From b28cacc96cb428ecf9ded99a9eb81b17023cd4cd Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 31 Oct 2021 16:33:31 +0300 Subject: [PATCH 11/28] small fixes --- .gitignore | 2 +- lib/utils/CMakeLists.txt | 1 - lib/word_search/CMakeLists.txt | 2 +- lib/word_search_procs/CMakeLists.txt | 2 +- lib/word_search_procs/src/word_search_procs.c | 1 - tests/test.cpp | 16 ++++++++-------- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index be1c665..088d412 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /cmake-build-debug /build -/linters \ No newline at end of file +/linters diff --git a/lib/utils/CMakeLists.txt b/lib/utils/CMakeLists.txt index ed43a60..24cc298 100644 --- a/lib/utils/CMakeLists.txt +++ b/lib/utils/CMakeLists.txt @@ -1,4 +1,3 @@ add_library(utils STATIC src/utils.c include/utils.h) target_include_directories(utils PUBLIC include) -#target_link_libraries(word_search PUBLIC utils) \ No newline at end of file diff --git a/lib/word_search/CMakeLists.txt b/lib/word_search/CMakeLists.txt index 8b85f6f..47b457a 100644 --- a/lib/word_search/CMakeLists.txt +++ b/lib/word_search/CMakeLists.txt @@ -1,4 +1,4 @@ add_library(word_search STATIC src/word_search.c) target_include_directories(word_search PUBLIC include) -target_link_libraries(word_search PUBLIC utils) \ No newline at end of file +target_link_libraries(word_search PUBLIC utils) diff --git a/lib/word_search_procs/CMakeLists.txt b/lib/word_search_procs/CMakeLists.txt index 86acfa3..2c6f600 100644 --- a/lib/word_search_procs/CMakeLists.txt +++ b/lib/word_search_procs/CMakeLists.txt @@ -1,4 +1,4 @@ add_library(word_search_procs SHARED src/word_search_procs.c) target_include_directories(word_search_procs PUBLIC include) -target_link_libraries(word_search_procs PUBLIC utils) \ No newline at end of file +target_link_libraries(word_search_procs PUBLIC utils) diff --git a/lib/word_search_procs/src/word_search_procs.c b/lib/word_search_procs/src/word_search_procs.c index 0282968..2bdc43b 100644 --- a/lib/word_search_procs/src/word_search_procs.c +++ b/lib/word_search_procs/src/word_search_procs.c @@ -173,4 +173,3 @@ char *find_longest_word(char *string_of_words) { return longest_word; } - diff --git a/tests/test.cpp b/tests/test.cpp index ebf0370..7251583 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -21,20 +21,20 @@ TEST(word_counter, word_counter_invalid_input) { TEST(array_creating, array_creating_invalid_input) { char *pseudo_string = NULL; - char** arr = create_array_of_words(pseudo_string); + char **arr = create_array_of_words(pseudo_string); EXPECT_TRUE(arr == NULL); } TEST(array_creating, array_creating) { char *string_of_words = create_words(); size_t number_of_words = word_counter(string_of_words); - char** arr = create_array_of_words(string_of_words); + char **arr = create_array_of_words(string_of_words); for (size_t i = 0; i < number_of_words; ++i) { EXPECT_TRUE(arr[i] != NULL); } EXPECT_TRUE(arr != NULL); for (size_t i = 0; i < number_of_words; ++i) { - free(arr[i]); + free(arr[i]); } free(string_of_words); free(arr); @@ -43,9 +43,9 @@ TEST(array_creating, array_creating) { TEST(array_creating, array_creating_const) { char *string_of_words = create_words(); size_t number_of_words = word_counter(string_of_words); - char* copy = (char*)malloc(MAX_SIZE* sizeof(char )); + char *copy = (char *) malloc(MAX_SIZE * sizeof(char)); copy = strcpy(copy, string_of_words); - char** arr = create_array_of_words(string_of_words); + char **arr = create_array_of_words(string_of_words); EXPECT_TRUE(strcmp(copy, string_of_words) == 0); for (size_t i = 0; i < number_of_words; ++i) { free(arr[i]); @@ -57,7 +57,7 @@ TEST(array_creating, array_creating_const) { TEST(max_len_word, max_len_word_invalid_input) { char *pseudo_string = NULL; - char* longest_word = find_longest_word(pseudo_string); + char *longest_word = find_longest_word(pseudo_string); EXPECT_TRUE(longest_word == NULL); } @@ -65,8 +65,8 @@ TEST(max_len_word, max_len_word_invalid_input) { TEST(max_len_word, max_len_word) { char *string_of_words = create_words(); size_t number_of_words = word_counter(string_of_words); - char** arr = create_array_of_words(string_of_words); - char* longest_word = find_longest_word(string_of_words); + char **arr = create_array_of_words(string_of_words); + char *longest_word = find_longest_word(string_of_words); for (size_t i = 0; i < number_of_words; ++i) { EXPECT_TRUE(strlen(longest_word) >= strlen(arr[i])); } From 582479bc98e2e54e1a633397120cd4116b455d9e Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 31 Oct 2021 17:38:23 +0300 Subject: [PATCH 12/28] less stress --- lib/utils/include/utils.h | 2 +- tests/stress_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index 715f6a0..0325a3c 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -19,7 +19,7 @@ extern "C" { #define NUMB_OF_SPACES 5 #define MIN_SIZE 131072 // 128КБайт -#define MAX_SIZE 104857600 // 100Мбайт +#define MAX_SIZE 10485760 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) #define BUFF_SIZE 500 diff --git a/tests/stress_test.cpp b/tests/stress_test.cpp index 9ebc0a6..8761007 100644 --- a/tests/stress_test.cpp +++ b/tests/stress_test.cpp @@ -2,7 +2,7 @@ #include "word_search_interface.h" #include -#define NUM_OF_TESTS 15 +#define NUM_OF_TESTS 5 int main() { double timer = 0; From 8b2c0885db87eeb27414caf84fe7d6cff68e21fb Mon Sep 17 00:00:00 2001 From: erik770 <80068140+erik770@users.noreply.github.com> Date: Sun, 31 Oct 2021 17:46:45 +0300 Subject: [PATCH 13/28] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index af7ab42..a08e4a0 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,9 @@ ### Coverage https://app.codecov.io/gh/erik770/HW2/commits?page=1 +### Time +``` +Run ./build/stress_test && ./build/stress_test_procs +average time: 0.0286566 -w/o forks +average time: 0.203058 - with forks +``` From 53ebf422d8dca9c8c26b5ba5f70980d8b31c187b Mon Sep 17 00:00:00 2001 From: erik770 <80068140+erik770@users.noreply.github.com> Date: Tue, 2 Nov 2021 17:34:11 +0300 Subject: [PATCH 14/28] readme fix --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a08e4a0..bd93508 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,6 @@ https://app.codecov.io/gh/erik770/HW2/commits?page=1 ### Time ``` Run ./build/stress_test && ./build/stress_test_procs -average time: 0.0286566 -w/o forks -average time: 0.203058 - with forks +average time: 0.0286566 - with forks +average time: 0.203058 - w/o forks ``` From 013a1d88b426475bd5cfdfe24e41e2050b2d73c7 Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 01:13:09 +0300 Subject: [PATCH 15/28] word count and word search fix --- lib/utils/include/utils.h | 4 ++-- lib/utils/src/utils.c | 28 +++++++++------------------- lib/word_search/src/word_search.c | 31 ++++++++++++++++++------------- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index 0325a3c..f7c90a8 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -21,11 +21,11 @@ extern "C" { #define MIN_SIZE 131072 // 128КБайт #define MAX_SIZE 10485760 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) -#define BUFF_SIZE 500 +#define BUFF_SIZE 1024 char *create_words(); -size_t word_counter(const char *string_of_words); +size_t word_counter(char *string_of_words); char **create_array_of_words(char *string_of_words); diff --git a/lib/utils/src/utils.c b/lib/utils/src/utils.c index ecc8fb7..12f45a2 100644 --- a/lib/utils/src/utils.c +++ b/lib/utils/src/utils.c @@ -23,28 +23,21 @@ char *create_words() { return words; } -size_t word_counter(const char *string_of_words) { +size_t word_counter(char *string_of_words) { if (string_of_words == NULL) { return 0; } - char *copy = (char *) malloc(sizeof(char) * (strlen(string_of_words) + 1)); - if (copy == NULL) { - return 0; - } - strcpy(copy, string_of_words); size_t word_counter = 0; - - char *word = strtok(copy, " "); - if (word == NULL) { - free(copy); - return 0; - } - while (word != NULL) { - word = strtok(NULL, " "); - word_counter++; + size_t i = 0; + while(string_of_words[i] != '\0'){ + if (string_of_words[i] == ' '){ + word_counter++; + } + i++; } - free(copy); + word_counter++; + return word_counter; } @@ -55,13 +48,11 @@ char **create_array_of_words(char *string_of_words) { size_t words_count = word_counter(string_of_words); if (words_count == 0) { - free(string_of_words); return NULL; } size_t j = 0, k = 0, h = 0; char **words_arr = (char **) malloc(words_count * sizeof(char *)); if (words_arr == NULL) { - free(string_of_words); return NULL; } @@ -71,7 +62,6 @@ char **create_array_of_words(char *string_of_words) { for (size_t l = i; l > 0; l--) { free(words_arr[l - 1]); } - free(string_of_words); free(words_arr); return NULL; } diff --git a/lib/word_search/src/word_search.c b/lib/word_search/src/word_search.c index 61ea65e..5308f29 100644 --- a/lib/word_search/src/word_search.c +++ b/lib/word_search/src/word_search.c @@ -5,27 +5,32 @@ char *find_longest_word(char *string_of_words) { return NULL; } - char *longest_word = (char *) malloc(strlen(string_of_words) * sizeof(char)); + char *longest_word = (char *) calloc(BUFF_SIZE, sizeof(char)); if (longest_word == NULL) { - free(string_of_words); return NULL; } - char *word = strtok(string_of_words, " "); - if (word == NULL) { - free(longest_word); - return string_of_words; + char *current_word = (char *) malloc(BUFF_SIZE * sizeof(char)); + if (current_word == NULL) { + return NULL; } - strcpy(longest_word, word); - while (word != NULL) { - if (strlen(longest_word) < strlen(word)) { - strcpy(longest_word, word); + + size_t i = 0, j = 0; + for (size_t k = 0; k < word_counter(string_of_words); k++) { + while (string_of_words[i] != ' ' && string_of_words[i] != '\0') { + current_word[j] = string_of_words[i]; + i++; + j++; } - if (!strcmp(longest_word, "")) { - continue; + current_word[j] = '\0'; + j = 0; + i++; + if (strlen(current_word) > strlen(longest_word)) { + strcpy(longest_word, current_word); } - word = strtok(NULL, " "); + } + free(current_word); return longest_word; } From 95dacfb05e002a1c69048501478189217c55a659 Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 14:36:01 +0300 Subject: [PATCH 16/28] procs search fix --- CMakeLists.txt | 4 + lib/utils/include/utils.h | 4 +- lib/utils/src/utils.c | 6 +- lib/word_search_procs/src/word_search_procs.c | 184 +++++++----------- tests/stress_test.cpp | 2 +- 5 files changed, 78 insertions(+), 122 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f39138f..444e006 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,10 @@ add_subdirectory(lib/utils) add_subdirectory(lib/word_search) add_subdirectory(lib/word_search_procs) +add_executable(main main.c) +target_link_libraries(main utils word_search_procs) + + enable_testing() find_package(GTest REQUIRED) add_executable(tests tests/test.cpp) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index f7c90a8..221779f 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -18,8 +18,8 @@ extern "C" { #define SYMBOLS "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm " #define NUMB_OF_SPACES 5 -#define MIN_SIZE 131072 // 128КБайт -#define MAX_SIZE 10485760 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) +#define MIN_SIZE 1048576 // 1МБайт +#define MAX_SIZE 104857600 // 100Мбайт #define BUFF_SIZE 1024 diff --git a/lib/utils/src/utils.c b/lib/utils/src/utils.c index 12f45a2..01fdf19 100644 --- a/lib/utils/src/utils.c +++ b/lib/utils/src/utils.c @@ -30,8 +30,8 @@ size_t word_counter(char *string_of_words) { size_t word_counter = 0; size_t i = 0; - while(string_of_words[i] != '\0'){ - if (string_of_words[i] == ' '){ + while (string_of_words[i] != '\0') { + if (string_of_words[i] == ' ') { word_counter++; } i++; @@ -80,5 +80,3 @@ char **create_array_of_words(char *string_of_words) { words_arr[k++][h++] = '\0'; return words_arr; } - - diff --git a/lib/word_search_procs/src/word_search_procs.c b/lib/word_search_procs/src/word_search_procs.c index 2bdc43b..8719805 100644 --- a/lib/word_search_procs/src/word_search_procs.c +++ b/lib/word_search_procs/src/word_search_procs.c @@ -6,19 +6,79 @@ typedef struct { char mtext[BUFF_SIZE]; } message_buff; -char *find_longest_word(char *string_of_words) { - if (string_of_words == NULL) { - return NULL; + +void child_procs_work(char *string_of_words, size_t current_procs_numb, size_t number_of_procs, int q_id) { + char *longest_word = (char *) calloc(BUFF_SIZE, sizeof(char)); + if (longest_word == NULL) { + exit(EXIT_FAILURE); + } + char *current_word = (char *) calloc(BUFF_SIZE, sizeof(char)); + if (current_word == NULL) { + free(longest_word); + exit(EXIT_FAILURE); + } + + size_t part_size = strlen(string_of_words) / number_of_procs; + size_t right_border = 0; + if (current_procs_numb != number_of_procs - 1) { + right_border = (current_procs_numb + 1) * part_size; + } else { + right_border = strlen(string_of_words); + } + + size_t j = current_procs_numb * part_size; + size_t skipped_letters_of_word_on_border = 0; + if (j != 0) { + while (string_of_words[j] != ' ') { + j++; + skipped_letters_of_word_on_border++; + } + j++; + skipped_letters_of_word_on_border++; + } + + if (current_procs_numb == number_of_procs - 1) { + skipped_letters_of_word_on_border = 0; } - char **words_array = create_array_of_words(string_of_words); - if (words_array == NULL) { + size_t i = 0; + while (j < right_border + skipped_letters_of_word_on_border) { + while (string_of_words[j] != ' ' && string_of_words[j] != '\0') { + current_word[i] = string_of_words[j]; + i++; + j++; + } + current_word[i] = '\0'; + i = 0; + j++; + if (strlen(current_word) > strlen(longest_word)) { + strcpy(longest_word, current_word); + } + } + + message_buff q_buff = {1, ""}; + strcpy(q_buff.mtext, longest_word); + + if (msgsnd(q_id, (struct msgbuf *) &q_buff, strlen(q_buff.mtext) + 1, 0) == -1) { + free(longest_word); + free(current_word); free(string_of_words); + exit(EXIT_FAILURE); + } + + free(string_of_words); + free(current_word); + free(longest_word); + exit(EXIT_SUCCESS); +} + + +char *find_longest_word(char *string_of_words) { + if (string_of_words == NULL) { return NULL; } - size_t number_of_words = word_counter(string_of_words); + size_t number_of_procs = sysconf(_SC_NPROCESSORS_ONLN); - size_t part_size = number_of_words / number_of_procs; - int status; + int status = 0; key_t key = IPC_PRIVATE; int q_id = msgget(key, 0660 | IPC_CREAT); @@ -27,95 +87,12 @@ char *find_longest_word(char *string_of_words) { for (size_t k = 0; k < number_of_procs; ++k) { pids[k] = fork(); if (pids[k] == 0) { - char *longest_word = NULL; - size_t max_len = 0; - if (k != number_of_procs - 1) { - for (size_t j = k * part_size; j < (k + 1) * part_size; ++j) { - if (strlen(words_array[j]) >= max_len) { - max_len = strlen(words_array[j]); - if (longest_word != NULL) { - free(longest_word); - } - longest_word = (char *) malloc(BUFF_SIZE * sizeof(char)); - if (longest_word == NULL) { - for (size_t i = 0; i < number_of_words; i++) { - if (words_array[i] != NULL) - free(words_array[i]); - } - free(words_array); - free(string_of_words); - exit(EXIT_FAILURE); - } - strcpy(longest_word, words_array[j]); - } - } - - - } else { - for (size_t j = k * part_size; j < number_of_words; ++j) { - if (strlen(words_array[j]) >= max_len) { - max_len = strlen(words_array[j]); - if (longest_word != NULL) { - free(longest_word); - } - longest_word = (char *) malloc(BUFF_SIZE * sizeof(char)); - if (longest_word == NULL) { - for (size_t i = 0; i < number_of_words; i++) { - if (words_array[i] != NULL) - free(words_array[i]); - } - free(words_array); - free(string_of_words); - exit(EXIT_FAILURE); - } - strcpy(longest_word, words_array[j]); - } - } - } - - if (longest_word == NULL) { - for (size_t i = 0; i < number_of_words; i++) { - if (words_array[i] != NULL) - free(words_array[i]); - } - free(words_array); - free(string_of_words); - exit(EXIT_FAILURE); - } - - message_buff q_buff = {1,""}; - strcpy(q_buff.mtext, longest_word); - - if (msgsnd(q_id, (struct msgbuf *) &q_buff, strlen(q_buff.mtext) + 1, 0) == -1) { - free(longest_word); - for (size_t i = 0; i < number_of_words; i++) { - if (words_array[i] != NULL) - free(words_array[i]); - } - free(words_array); - free(string_of_words); - exit(EXIT_FAILURE); - } - - free(longest_word); - for (size_t i = 0; i < number_of_words; i++) { - if (words_array[i] != NULL) - free(words_array[i]); - } - free(words_array); - free(string_of_words); - exit(EXIT_SUCCESS); + child_procs_work(string_of_words, k, number_of_procs, q_id); } } for (size_t i = 0; i < number_of_procs; ++i) { if (waitpid(pids[i], &status, 0) != pids[i]) { - for (size_t j = 0; j < number_of_words; j++) { - if (words_array[j] != NULL) - free(words_array[j]); - } - free(words_array); - free(string_of_words); return NULL; } } @@ -127,22 +104,11 @@ char *find_longest_word(char *string_of_words) { message_buff q_buff; if (msgrcv(q_id, (struct msgbuf *) &q_buff, BUFF_SIZE, 1, 0) == -1) { - for (size_t j = 0; j < number_of_words; j++) { - if (words_array[j] != NULL) - free(words_array[j]); - } - free(words_array); free(string_of_words); return NULL; } if (q_buff.mtext[0] == '\0') { - for (size_t j = 0; j < number_of_words; j++) { - if (words_array[j] != NULL) - free(words_array[j]); - } - free(words_array); - free(string_of_words); return NULL; } @@ -151,11 +117,6 @@ char *find_longest_word(char *string_of_words) { longest_word = (char *) malloc(BUFF_SIZE * sizeof(char)); if (longest_word == NULL) { - for (size_t j = 0; j < number_of_words; j++) { - if (words_array[j] != NULL) - free(words_array[j]); - } - free(words_array); free(string_of_words); return NULL; } @@ -164,12 +125,5 @@ char *find_longest_word(char *string_of_words) { max_len = strlen(q_buff.mtext); } } - - for (size_t i = 0; i < number_of_words; i++) { - if (words_array[i] != NULL) - free(words_array[i]); - } - free(words_array); - return longest_word; } diff --git a/tests/stress_test.cpp b/tests/stress_test.cpp index 8761007..9ebc0a6 100644 --- a/tests/stress_test.cpp +++ b/tests/stress_test.cpp @@ -2,7 +2,7 @@ #include "word_search_interface.h" #include -#define NUM_OF_TESTS 5 +#define NUM_OF_TESTS 15 int main() { double timer = 0; From 6455708f95e99b5324ae1396edcf31611b2932d6 Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 14:38:13 +0300 Subject: [PATCH 17/28] CMakeLists fix --- CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 444e006..f39138f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,10 +12,6 @@ add_subdirectory(lib/utils) add_subdirectory(lib/word_search) add_subdirectory(lib/word_search_procs) -add_executable(main main.c) -target_link_libraries(main utils word_search_procs) - - enable_testing() find_package(GTest REQUIRED) add_executable(tests tests/test.cpp) From 6f70739ffa177fc7a5255340a76c5fbca3753883 Mon Sep 17 00:00:00 2001 From: erik770 <80068140+erik770@users.noreply.github.com> Date: Sun, 7 Nov 2021 14:39:34 +0300 Subject: [PATCH 18/28] Update actions.yml --- .github/workflows/actions.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 0fbfca7..308f9f2 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -3,10 +3,6 @@ name: CI on: push: branches: [ dev ] - pull_request: - branches: - - dev - - main jobs: build: From fd5a293d8916fad65e8406d30366a6e34508e23e Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 14:46:57 +0300 Subject: [PATCH 19/28] test less stress --- lib/utils/include/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index 221779f..398acdb 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -19,7 +19,7 @@ extern "C" { #define NUMB_OF_SPACES 5 #define MIN_SIZE 1048576 // 1МБайт -#define MAX_SIZE 104857600 // 100Мбайт +#define MAX_SIZE 10485760 // 10Мбайт 10 а не 100 чтобы тесты не долго крутились #define BUFF_SIZE 1024 From 7c93f312de01b90eea3da0dfd97c0caa217165cc Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 15:47:01 +0300 Subject: [PATCH 20/28] test less stress --- lib/utils/include/utils.h | 5 +++-- tests/stress_test.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index 398acdb..c29cf95 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -18,8 +18,8 @@ extern "C" { #define SYMBOLS "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm " #define NUMB_OF_SPACES 5 -#define MIN_SIZE 1048576 // 1МБайт -#define MAX_SIZE 10485760 // 10Мбайт 10 а не 100 чтобы тесты не долго крутились +#define MIN_SIZE 131072 // 128КБайт +#define MAX_SIZE 10485760 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) #define BUFF_SIZE 1024 @@ -30,6 +30,7 @@ size_t word_counter(char *string_of_words); char **create_array_of_words(char *string_of_words); + #if defined(__cplusplus) } #endif diff --git a/tests/stress_test.cpp b/tests/stress_test.cpp index 9ebc0a6..52e31f3 100644 --- a/tests/stress_test.cpp +++ b/tests/stress_test.cpp @@ -2,7 +2,7 @@ #include "word_search_interface.h" #include -#define NUM_OF_TESTS 15 +#define NUM_OF_TESTS 10 int main() { double timer = 0; From 151e574f7daffcbab1896f8b7c0a51ac8ad3ec92 Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 18:36:05 +0300 Subject: [PATCH 21/28] test less stress --- CMakeLists.txt | 4 ++++ lib/utils/include/utils.h | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f39138f..03f427b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,10 @@ add_subdirectory(lib/utils) add_subdirectory(lib/word_search) add_subdirectory(lib/word_search_procs) +add_executable(main main.c) +target_link_libraries(main utils word_search) + + enable_testing() find_package(GTest REQUIRED) add_executable(tests tests/test.cpp) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index c29cf95..d84cef1 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -18,8 +18,9 @@ extern "C" { #define SYMBOLS "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm " #define NUMB_OF_SPACES 5 -#define MIN_SIZE 131072 // 128КБайт -#define MAX_SIZE 10485760 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) +#define MIN_SIZE 13107 // 128КБайт +#define MAX_SIZE 104857 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) + #define BUFF_SIZE 1024 From f8db6efb44c60db0e3bf449024684004a58e8d58 Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 18:37:54 +0300 Subject: [PATCH 22/28] bup dup --- CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 03f427b..f39138f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,10 +12,6 @@ add_subdirectory(lib/utils) add_subdirectory(lib/word_search) add_subdirectory(lib/word_search_procs) -add_executable(main main.c) -target_link_libraries(main utils word_search) - - enable_testing() find_package(GTest REQUIRED) add_executable(tests tests/test.cpp) From 06d606436556e9a2313edf2b8bd57261c07930ed Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 18:49:04 +0300 Subject: [PATCH 23/28] word search fix --- lib/utils/include/utils.h | 5 ++--- lib/word_search/src/word_search.c | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index d84cef1..01743d5 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -18,9 +18,8 @@ extern "C" { #define SYMBOLS "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm " #define NUMB_OF_SPACES 5 -#define MIN_SIZE 13107 // 128КБайт -#define MAX_SIZE 104857 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) - +#define MIN_SIZE 131072 // 128КБайт +#define MAX_SIZE 104857600 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) #define BUFF_SIZE 1024 diff --git a/lib/word_search/src/word_search.c b/lib/word_search/src/word_search.c index 5308f29..3040422 100644 --- a/lib/word_search/src/word_search.c +++ b/lib/word_search/src/word_search.c @@ -16,8 +16,9 @@ char *find_longest_word(char *string_of_words) { } size_t i = 0, j = 0; - for (size_t k = 0; k < word_counter(string_of_words); k++) { - while (string_of_words[i] != ' ' && string_of_words[i] != '\0') { + + while (string_of_words[i] != '\0') { + while (string_of_words[i] != ' ') { current_word[j] = string_of_words[i]; i++; j++; @@ -28,7 +29,6 @@ char *find_longest_word(char *string_of_words) { if (strlen(current_word) > strlen(longest_word)) { strcpy(longest_word, current_word); } - } free(current_word); From 60f8202bad44883ab2e3f7017b4de3eec9ec45c2 Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 19:09:04 +0300 Subject: [PATCH 24/28] word search fix2 --- lib/utils/include/utils.h | 2 +- lib/word_search/src/word_search.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index 01743d5..c29cf95 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -19,7 +19,7 @@ extern "C" { #define NUMB_OF_SPACES 5 #define MIN_SIZE 131072 // 128КБайт -#define MAX_SIZE 104857600 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) +#define MAX_SIZE 10485760 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) #define BUFF_SIZE 1024 diff --git a/lib/word_search/src/word_search.c b/lib/word_search/src/word_search.c index 3040422..b67dd6b 100644 --- a/lib/word_search/src/word_search.c +++ b/lib/word_search/src/word_search.c @@ -17,8 +17,8 @@ char *find_longest_word(char *string_of_words) { size_t i = 0, j = 0; - while (string_of_words[i] != '\0') { - while (string_of_words[i] != ' ') { + while (true) { + while (string_of_words[i] != ' ' && string_of_words[i] !='\0') { current_word[j] = string_of_words[i]; i++; j++; @@ -26,9 +26,13 @@ char *find_longest_word(char *string_of_words) { current_word[j] = '\0'; j = 0; i++; + if (strlen(current_word) > strlen(longest_word)) { strcpy(longest_word, current_word); } + if(string_of_words[i-1] == '\0') { + break; + } } free(current_word); From bbc036f836d280b32c0fa2d690ebada8db9d43bf Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 19:13:58 +0300 Subject: [PATCH 25/28] stress --- lib/utils/include/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index c29cf95..7b7ebea 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -19,7 +19,7 @@ extern "C" { #define NUMB_OF_SPACES 5 #define MIN_SIZE 131072 // 128КБайт -#define MAX_SIZE 10485760 // 10Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) +#define MAX_SIZE 104857600 // 100Мбайт #define BUFF_SIZE 1024 From 647c9f3bc4847aac9c17a708b5ba100ab248abfb Mon Sep 17 00:00:00 2001 From: erik770 Date: Sun, 7 Nov 2021 19:20:49 +0300 Subject: [PATCH 26/28] less stress) --- lib/utils/include/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index 7b7ebea..83e6507 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -19,7 +19,7 @@ extern "C" { #define NUMB_OF_SPACES 5 #define MIN_SIZE 131072 // 128КБайт -#define MAX_SIZE 104857600 // 100Мбайт +#define MAX_SIZE 10485760 // 100Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз) #define BUFF_SIZE 1024 From 52fa36b24b11c8a46c9e95386cd34cb6839dcfd0 Mon Sep 17 00:00:00 2001 From: erik770 <80068140+erik770@users.noreply.github.com> Date: Sun, 7 Nov 2021 19:26:29 +0300 Subject: [PATCH 27/28] time add to readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bd93508..42012b4 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,6 @@ https://app.codecov.io/gh/erik770/HW2/commits?page=1 ### Time ``` Run ./build/stress_test && ./build/stress_test_procs -average time: 0.0286566 - with forks -average time: 0.203058 - w/o forks +average time: 0.0613759 -w/o forks +average time: 0.0006113 -with forks ``` From 0a4aa2f4faabc0560bcca406162b216b80a5c57c Mon Sep 17 00:00:00 2001 From: erik770 Date: Mon, 8 Nov 2021 18:22:54 +0300 Subject: [PATCH 28/28] word_cpy add --- lib/utils/include/utils.h | 3 ++- lib/utils/src/utils.c | 19 ++++++++++++++++++- lib/word_search/src/word_search.c | 17 ++++++++--------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/utils/include/utils.h b/lib/utils/include/utils.h index 83e6507..056b149 100644 --- a/lib/utils/include/utils.h +++ b/lib/utils/include/utils.h @@ -25,10 +25,11 @@ extern "C" { char *create_words(); -size_t word_counter(char *string_of_words); +size_t word_counter(const char *string_of_words); char **create_array_of_words(char *string_of_words); +int word_cpy(char *dest, const char *src, size_t shift); #if defined(__cplusplus) diff --git a/lib/utils/src/utils.c b/lib/utils/src/utils.c index 01fdf19..76d73bd 100644 --- a/lib/utils/src/utils.c +++ b/lib/utils/src/utils.c @@ -23,7 +23,7 @@ char *create_words() { return words; } -size_t word_counter(char *string_of_words) { +size_t word_counter(const char *string_of_words) { if (string_of_words == NULL) { return 0; } @@ -41,6 +41,23 @@ size_t word_counter(char *string_of_words) { return word_counter; } +int word_cpy(char *dest, const char *src, size_t shift) { + if(src == NULL || dest == NULL){ + return -1; + } + + size_t j = 0; + size_t i = 0; + while (src[shift + i] != ' ' && src[shift + i] != '\0') { + dest[j] = src[shift + i]; + i++; + j++; + } + dest[j] = '\0'; + return 0; +} + + char **create_array_of_words(char *string_of_words) { if (string_of_words == NULL) { return NULL; diff --git a/lib/word_search/src/word_search.c b/lib/word_search/src/word_search.c index b67dd6b..6aa5fa5 100644 --- a/lib/word_search/src/word_search.c +++ b/lib/word_search/src/word_search.c @@ -12,25 +12,24 @@ char *find_longest_word(char *string_of_words) { char *current_word = (char *) malloc(BUFF_SIZE * sizeof(char)); if (current_word == NULL) { + free(longest_word); return NULL; } - size_t i = 0, j = 0; + size_t i = 0; while (true) { - while (string_of_words[i] != ' ' && string_of_words[i] !='\0') { - current_word[j] = string_of_words[i]; - i++; - j++; + if (word_cpy(current_word, string_of_words, i) != 0){ + free(longest_word); + free(current_word); + return NULL; } - current_word[j] = '\0'; - j = 0; - i++; + i = i + strlen(current_word) + 1; if (strlen(current_word) > strlen(longest_word)) { strcpy(longest_word, current_word); } - if(string_of_words[i-1] == '\0') { + if (string_of_words[i - 1] == '\0') { break; } }