/
0005-lab06-final.patch
106 lines (97 loc) · 3.26 KB
/
0005-lab06-final.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
From ca861290fdbf37b369f66c42a308dc8864cc4051 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Herman?= <loic@loicherman.ch>
Date: Mon, 30 Oct 2023 20:19:07 +0100
Subject: [PATCH] feat: implement lab06
---
usr/host/threads.c | 71 +++++++++++++++++++++++++++++++++++++++-------
1 file changed, 60 insertions(+), 11 deletions(-)
diff --git a/usr/host/threads.c b/usr/host/threads.c
index 07cdd3c..f520aac 100644
--- a/usr/host/threads.c
+++ b/usr/host/threads.c
@@ -23,10 +23,22 @@ typedef struct {
/**
* @brief Threads function used to count the letters occurence in a text
*/
-void *count_letters(void * args) {
- count_param_t *params = (count_param_t *) args;
-
- //TO COMPLETE: Count each letter's number of occurences in the part of the text assigned to this thread. Processing must be case-insensitive ('A' and 'a' both count as an occurence of A). Store the results in the given argument structure
+void *count_letters(void *args)
+{
+ count_param_t *params = (count_param_t *)args;
+
+ for (int i = 0; i < LETTERS_NB; i++)
+ params->counters[i] = 0;
+
+ // Count each letter's number of occurences in the part of the text assigned to this thread.
+ // Processing must be case-insensitive ('A' and 'a' both count as an occurence of A).
+ // Store the results in the given argument structure.
+ for (size_t i = 0; i < params->size; ++i)
+ {
+ char c = params->text_pointer[i];
+ if (isalpha(c))
+ ++params->counters[c >= A_LOWER_OFFSET ? c - A_LOWER_OFFSET : c - A_UPPER_OFFSET];
+ }
return NULL;
}
@@ -125,18 +137,55 @@ int main(int argc, char **argv)
printf("Error: The number of threads must be between 0 and 15");
return EXIT_FAILURE;
}
- // TO COMPLETE: Load the text and its size
- // TO COMPLETE: Configure and launch threads
+ // Load the text and its size
+ char *text = buffer_from_file(filename);
+ size_t text_size = strlen(text);
+
+ // Configure and launch threads
+ pthread_t *threads = malloc(thread_num * sizeof(pthread_t));
+ count_param_t *params = malloc(thread_num * sizeof(count_param_t));
- // TO COMPLETE: Join the threads and compile the results
+ size_t chunk_size = text_size / thread_num;
+ size_t remainder = text_size % thread_num;
- /* Display results */
- for (int i = 0; i < LETTERS_NB; ++i) {
- printf("Number of %c: %d\n", (char)(i+A_LOWER_OFFSET), result_counters[i]);
+ for (int i = 0; i < thread_num; ++i)
+ {
+ params[i].text_pointer = text + i * chunk_size;
+ params[i].size = chunk_size + (i == thread_num - 1 ? remainder : 0);
+
+ if (pthread_create(&threads[i], NULL, count_letters, ¶ms[i]) != 0)
+ {
+ fprintf(stderr, "Error: pthread_create() failed\n");
+ return EXIT_FAILURE;
+ }
+ }
+
+ // Join the threads and compile the results
+ for (int i = 0; i < thread_num; i++)
+ {
+ if (pthread_join(threads[i], NULL) != 0)
+ {
+ fprintf(stderr, "Error: pthread_join() failed\n");
+ return EXIT_FAILURE;
+ }
+
+ for (int j = 0; j < LETTERS_NB; j++)
+ {
+ result_counters[j] += params[i].counters[j];
+ }
+ }
+
+ // Display results
+ for (int i = 0; i < LETTERS_NB; ++i)
+ {
+ printf("Number of %c: %d\n", (char)(i + A_LOWER_OFFSET), result_counters[i]);
}
- // TO COMPLETE: Free allocated memory
+ // Free allocated memory
+ free(text);
+ free(threads);
+ free(params);
return EXIT_SUCCESS;
}
--
2.42.0