Skip to content

hw2 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: CI

on:
push:
branches: [ dev ]

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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/cmake-build-debug
/build
/linters
32 changes: 32 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 --coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -pthread --coverage")

add_compile_options("--coverage")

add_subdirectory(lib/utils)
add_subdirectory(lib/word_search)
add_subdirectory(lib/word_search_procs)

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)

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)
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# HW2
Решение второго ИЗ из курса по углубленному программированию на языке C/C++ в технопарке вк компани! лучшего технического во вселенной

### Вариант #17
Сравните и выведите в консоль время работы последовательного и параллельного с использованием нескольких процессов алгоритмов, каждый из которых выделяет в динамической памяти символьный массив размером 100 Мб и выполняет поиск самого длинного слова в тексте. Словом считается последовательность, состоящая из букв и ограниченная пробелами.

### Coverage
https://app.codecov.io/gh/erik770/HW2/commits?page=1

### Time
```
Run ./build/stress_test && ./build/stress_test_procs
average time: 0.0613759 -w/o forks
average time: 0.0006113 -with forks
```
3 changes: 3 additions & 0 deletions lib/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(utils STATIC src/utils.c include/utils.h)

target_include_directories(utils PUBLIC include)
37 changes: 37 additions & 0 deletions lib/utils/include/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#if defined(__cplusplus)
extern "C" {
#endif

#include "stdio.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>

#define SYMBOLS "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm "
#define NUMB_OF_SPACES 5

#define MIN_SIZE 131072 // 128КБайт
#define MAX_SIZE 10485760 // 100Мбайт(не 100 чтобы тесты очень долго не ждать каждый раз)

#define BUFF_SIZE 1024

char *create_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)
}
#endif
13 changes: 13 additions & 0 deletions lib/utils/include/word_search_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +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
99 changes: 99 additions & 0 deletions lib/utils/src/utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "utils.h"

char *create_words() {
srand(time(NULL));
size_t size = rand() % (MAX_SIZE - MIN_SIZE + 1) + MIN_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 0;
}

size_t word_counter = 0;
size_t i = 0;
while (string_of_words[i] != '\0') {
if (string_of_words[i] == ' ') {
word_counter++;
}
i++;
}
word_counter++;

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;
}

size_t words_count = word_counter(string_of_words);
if (words_count == 0) {
return NULL;
}
size_t j = 0, k = 0, h = 0;
char **words_arr = (char **) malloc(words_count * sizeof(char *));
if (words_arr == NULL) {
return NULL;
}

for (size_t i = 0; i < words_count; ++i) {
words_arr[i] = (char *) malloc(BUFF_SIZE * sizeof(char));
if (words_arr[i] == NULL) {
for (size_t l = i; l > 0; l--) {
free(words_arr[l - 1]);
}
free(words_arr);
return NULL;
}
}

while (string_of_words[j] != '\0') {
if (string_of_words[j] == ' ') {
words_arr[k][h] = '\0';
++j;
++k;
h = 0;
} else {
words_arr[k][h++] = string_of_words[j++];
}
}
words_arr[k++][h++] = '\0';
return words_arr;
}
4 changes: 4 additions & 0 deletions lib/word_search/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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)
39 changes: 39 additions & 0 deletions lib/word_search/src/word_search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "word_search_interface.h"

char *find_longest_word(char *string_of_words) {
if (string_of_words == NULL) {
return NULL;
}

char *longest_word = (char *) calloc(BUFF_SIZE, sizeof(char));
if (longest_word == NULL) {
return NULL;
}

char *current_word = (char *) malloc(BUFF_SIZE * sizeof(char));
if (current_word == NULL) {
free(longest_word);
return NULL;
}

size_t i = 0;

while (true) {
if (word_cpy(current_word, string_of_words, i) != 0){
free(longest_word);
free(current_word);
return NULL;
}
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') {
break;
}
}

free(current_word);
return longest_word;
}
4 changes: 4 additions & 0 deletions lib/word_search_procs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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)
Loading