Skip to content
Open
Changes from all commits
Commits
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
158 changes: 158 additions & 0 deletions fakultativ_bdz3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

#define MIN 1000
#define MAX 100000

pthread_mutex_t mutex;

typedef struct NodeList {
int val;
struct NodeList* next;
struct NodeList* prev;
} NodeList;

typedef struct List {
NodeList* begin_list;
NodeList* end_list;
int size;
} List;

typedef struct ArgProcFunc {
int option; // 1 - единицы с конца, 0 - нули с головы
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вместо комментария добавь enum

int count_bits;//кол-во битов
int pass_elements;//кол-во обработанных эл-в
List* list;
} ArgProcFunc;

int sumBits(const int* num, const int* option) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет проверки на null

int count_bit = 0;
for (int i = 0; i < sizeof(int) * 8; i++) {
if (((*num >> i) & 1) == *option) {
++count_bit;
}

}
return count_bit;
}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут тоже лишняя

void* threadFunc(void* arg_func) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет проверки на null

ArgProcFunc* arg_procfunc = (ArgProcFunc*)arg_func;
List* list = arg_procfunc->list;
int list_empty = 0;
while (list->size) {
pthread_mutex_lock(&mutex);
NodeList* current_node = NULL;

if (list->size > 1) {//выбор эл-та
if ((arg_procfunc->option) == 1) { // с конца

current_node = list->end_list;
list->end_list = list->end_list->prev;
list->end_list->next = NULL;
}
else { // с головы
current_node = arg_procfunc->list->begin_list;
list->begin_list = list->begin_list->next;
list->begin_list->prev = NULL;
}
--(arg_procfunc->list->size);
}

else {
current_node = arg_procfunc->list->begin_list;
list->begin_list = NULL;
list->end_list = NULL;
list->size = 0;
}

pthread_mutex_unlock(&mutex);

if (current_node) {//подсчет битов
arg_procfunc->count_bits += sumBits(&(current_node->val), &(arg_procfunc->option));
arg_procfunc->pass_elements++;
free(current_node);
}
}
return NULL;
}

void printList(const List* list) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет проверки на null

NodeList* current_node = list->begin_list;
while (current_node) {

printf("%d\t", current_node->val);
current_node = current_node->next;
}

printf("\n");
}

List* createList(int n) {
List* list = (List*)malloc(sizeof(List));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет проверки на null

list->begin_list = NULL;
list->end_list = NULL;
list->size = 0;

for (int i = 0; i < n; ++i) {
NodeList* current_node = (NodeList*)malloc(sizeof(NodeList));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет проверки на null

current_node->val = rand() % (MAX - MIN + 1) + MIN;
current_node->next = NULL;
current_node->prev = list->end_list;
if (list->end_list) {
list->end_list->next = current_node;
}
else {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

зачем лишняя ?

list->begin_list = current_node;
}
list->end_list = current_node;
++(list->size);
}
return list;
}

void intitalArgProcFunc(ArgProcFunc* arg_proc_func, List* list, int option, int count_bits, int pass_elements) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет проверки на null

arg_proc_func->list = list;
arg_proc_func->option = option;
arg_proc_func->count_bits = count_bits;
arg_proc_func->pass_elements = pass_elements;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нет строки

int main() {

pthread_mutex_init(&mutex, NULL);


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

зачем лишние строки ?

int n;
printf("Vvod kolichestva elementov: ");
scanf_s("%d", &n);
srand(time(NULL));
List* list = createList(n);
//printList(list);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это тут зачем ?


ArgProcFunc* arg_procfunc_first = (ArgProcFunc*)malloc(sizeof(ArgProcFunc));
intitalArgProcFunc(arg_procfunc_first, list, 0, 0, 0);// option = 0;с головы, считаем нули

ArgProcFunc* arg_procfunc_second = (ArgProcFunc*)malloc(sizeof(ArgProcFunc));
intitalArgProcFunc(arg_procfunc_second, list, 1, 0, 0);// option = 1; с конца, считаем единицы

pthread_t first_zero, second_one;
pthread_create(&first_zero, NULL, threadFunc, arg_procfunc_first);
pthread_create(&second_one, NULL, threadFunc, arg_procfunc_second);

pthread_join(first_zero, NULL);
pthread_join(second_one, NULL);
printf("Potok 1 (c golovi):\n elementov: %d\n 0 bit: %d\n", arg_procfunc_first->pass_elements, arg_procfunc_first->count_bits);
printf("Potok 2 (c hvosta):\n eleventov: %d\n 1 bit: %d\n", arg_procfunc_second->pass_elements, arg_procfunc_second->count_bits);
free(arg_procfunc_first);
free(arg_procfunc_second);
free(list);
pthread_mutex_destroy(&mutex);
return 0;
}



Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лишние строки