-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtws.c
225 lines (187 loc) · 6.49 KB
/
mtws.c
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// 2024-1 Operating Systems (ITP30002) - HW #3
// File : mtws.c
// Author: Hyunseo Lee (22100600) <hslee@handong.ac.kr>
/* MACROS */
#define _GNU_SOURCE
#define MAX_BUFFER_SIZE 100
/* HEADERS */
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/* TYPE DEFINITIONS */
typedef struct {
char *buffer[MAX_BUFFER_SIZE];
int size;
int front;
int rear;
int count;
pthread_mutex_t mutex;
pthread_cond_t not_empty;
pthread_cond_t not_full;
} bounded_buffer_t;
/* FUNCTION PROTOTYPES */
void *producer(void *param);
void *consumer(void *param);
void search_directory(const char *directory, bounded_buffer_t *buffer);
int search_word_in_file(const char *file_path, const char *word);
/* GLOBAL VARIABLES */
bounded_buffer_t bounded_buffer;
int num_threads;
char *search_word;
volatile int done = 0;
int total_found = 0;
int total_files = 0;
pthread_mutex_t total_mutex;
/* MAIN FUNCTION */
int main(int argc, char *argv[]) {
int buffer_size = 0;
char *directory = NULL;
// Parsing command line arguments
int opt;
while ((opt = getopt(argc, argv, "b:t:d:w:")) != -1) {
switch (opt) {
case 'b':
buffer_size = atoi(optarg);
break;
case 't':
num_threads = atoi(optarg);
break;
case 'd':
directory = optarg;
break;
case 'w':
search_word = optarg;
break;
default:
fprintf(stderr, "Usage: %s\n -b : bounded bufer size\n -t : number of threads searching word\n -d : search directory\n -w : search word\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (buffer_size <= 0 || num_threads <= 0 || !directory || !search_word) {
fprintf(stderr, "Usage: %s\n -b : bounded bufer size\n -t : number of threads searching word\n -d : search directory\n -w : search word\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("Buffer Size = %d, Num Threads = %d, Directory = %s, Search Word = %s\n", buffer_size, num_threads, directory, search_word);
// Initialize bounded buffer
bounded_buffer.size = buffer_size;
bounded_buffer.front = 0;
bounded_buffer.rear = 0;
bounded_buffer.count = 0;
pthread_mutex_init(&bounded_buffer.mutex, NULL);
pthread_cond_init(&bounded_buffer.not_empty, NULL);
pthread_cond_init(&bounded_buffer.not_full, NULL);
pthread_mutex_init(&total_mutex, NULL);
// Create producer thread
pthread_t producer_thread;
pthread_create(&producer_thread, NULL, producer, directory);
// Create consumer threads
pthread_t *consumer_threads = malloc(sizeof(pthread_t) * num_threads);
for (int i = 0; i < num_threads; i++) {
pthread_create(&consumer_threads[i], NULL, consumer, NULL);
printf("[Thread %ld] started searching '%s'...\n", (long)consumer_threads[i], search_word);
}
// Wait for producer thread to finish
pthread_join(producer_thread, NULL);
done = 1;
// Signal all consumer threads to exit
pthread_cond_broadcast(&bounded_buffer.not_empty);
// Wait for all consumer threads to finish
for (int i = 0; i < num_threads; i++) {
pthread_join(consumer_threads[i], NULL);
}
// Print total results
printf("Total found = %d (Num files = %d)\n", total_found, total_files);
// Cleanup
free(consumer_threads);
pthread_mutex_destroy(&bounded_buffer.mutex);
pthread_cond_destroy(&bounded_buffer.not_empty);
pthread_cond_destroy(&bounded_buffer.not_full);
pthread_mutex_destroy(&total_mutex);
return 0;
}
void *producer(void *param) {
char *directory = (char *)param;
search_directory(directory, &bounded_buffer);
return NULL;
}
void search_directory(const char *directory, bounded_buffer_t *buffer) {
struct dirent *entry;
DIR *dp = opendir(directory);
if (dp == NULL) {
perror("opendir");
return;
}
while ((entry = readdir(dp))) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", directory, entry->d_name);
search_directory(path, buffer);
} else {
char *file_path = malloc(1024);
snprintf(file_path, 1024, "%s/%s", directory, entry->d_name);
pthread_mutex_lock(&buffer->mutex);
while (buffer->count == buffer->size) {
pthread_cond_wait(&buffer->not_full, &buffer->mutex);
}
buffer->buffer[buffer->rear] = file_path;
buffer->rear = (buffer->rear + 1) % buffer->size;
buffer->count++;
pthread_cond_signal(&buffer->not_empty);
pthread_mutex_unlock(&buffer->mutex);
}
}
closedir(dp);
}
void *consumer(void *param) {
while (1) {
pthread_mutex_lock(&bounded_buffer.mutex);
while (bounded_buffer.count == 0 && !done) {
pthread_cond_wait(&bounded_buffer.not_empty, &bounded_buffer.mutex);
}
if (bounded_buffer.count == 0 && done) {
pthread_mutex_unlock(&bounded_buffer.mutex);
break;
}
char *file_path = bounded_buffer.buffer[bounded_buffer.front];
bounded_buffer.front = (bounded_buffer.front + 1) % bounded_buffer.size;
bounded_buffer.count--;
pthread_cond_signal(&bounded_buffer.not_full);
pthread_mutex_unlock(&bounded_buffer.mutex);
int found_count = search_word_in_file(file_path, search_word);
pthread_mutex_lock(&total_mutex);
total_found += found_count;
total_files++;
pthread_mutex_unlock(&total_mutex);
printf("[Thread %ld-%d] %s : %d found\n", (long)pthread_self(), total_files, file_path, found_count);
free(file_path);
}
return NULL;
}
int search_word_in_file(const char *file_path, const char *word) {
FILE *file = fopen(file_path, "r");
if (!file) {
perror("fopen");
return 0;
}
int count = 0;
size_t len = strlen(word);
char *line = NULL;
size_t linecap = 0;
while (getline(&line, &linecap, file) != -1) {
for (char *p = line; (p = strcasestr(p, word)) != NULL; p++) {
count++;
}
}
free(line);
fclose(file);
return count;
}