-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtword.c
110 lines (78 loc) · 2.02 KB
/
tword.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
#include <stdio.h>
#include <ctype.h>
#include <pthread.h>
#include <unistd.h>
#include "tree.h"
#define MIN_ARGS 3
#define MAX_FILES 8
#define MAX_SIZE 64
char files[MAX_FILES][255];
pthread_mutex_t lock;
struct Node *root = NULL;
char *toUpperCase(char *str);
void *file_processing(void *param);
int main(int argc, char const *argv[])
{
int file_num;
struct timeval time1, time2;
if (argc < MIN_ARGS || argc > MIN_ARGS + MAX_FILES)
{
fprintf(stderr, "Argument number is inappropriate!\n");
exit(-1);
}
else
{
file_num = atoi(argv[2]);
if (argc != MIN_ARGS + file_num)
{
fprintf(stderr, "File number and existing files are incompatible!\n");
exit(-1);
}
pthread_mutex_init(&lock, NULL);
pthread_t threads[file_num];
for (int i = 0; i < file_num; i++)
{
strcpy(files[i], argv[MIN_ARGS + i]);
}
gettimeofday(&time1, NULL);
for (int i = 0; i < file_num; i++)
{
threads[i] = i;
pthread_create(&threads[i], NULL, file_processing, (void *) files[i]);
}
for (int i = 0; i < file_num; i++)
{
pthread_join(threads[i], NULL);
}
FILE *fp = fopen(argv[1], "w");
writeInorder(root, fp);
gettimeofday(&time2, NULL);
printf("Total time = %ld miliseconds\n", (time2.tv_usec - time1.tv_usec) );
fclose(fp);
free_node(root);
}
return 0;
}
void *file_processing(void *param)
{
char *file_name = (char *) param;
FILE *fp = fopen(file_name, "r");
char buffer[MAX_SIZE];
pthread_mutex_lock(&lock);
while ( fscanf(fp, "%s", buffer) != EOF )
{
insert(&root, toUpperCase(buffer));
}
pthread_mutex_unlock(&lock);
fclose(fp);
pthread_exit(0);
}
char *toUpperCase(char *str)
{
char *result = str;
for (int i = 0; i < strlen(str); i++)
{
result[i] = toupper(result[i]);
}
return result;
}