-
Notifications
You must be signed in to change notification settings - Fork 3
/
freq_mt.c
177 lines (143 loc) · 3.43 KB
/
freq_mt.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
/*
* freq_mt.c -- multi-threaded word frequency counter
*/
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define NBUCKETS 10007
/* entries in a bucket are a linked list of struct entry */
struct entry {
struct entry *next;
const char *word;
pthread_mutex_t mtx; /* protects count field */
int count;
};
/* each bucket contains a pointer to the linked list of entries */
struct bucket {
pthread_mutex_t mtx; /* protects entries field */
struct entry *entries;
} H[NBUCKETS];
/* hash a string into an index into H[] */
unsigned hash(const char *s)
{
unsigned h = NBUCKETS ^ ((unsigned)*s++ << 2);
unsigned len = 0;
while (*s) {
len++;
h ^= (((unsigned)*s) << (len % 3)) +
((unsigned)*(s - 1) << ((len % 3 + 7)));
s++;
}
h ^= len;
return h % NBUCKETS;
}
/* bump the count for a word */
void count(const char *word)
{
unsigned h = hash(word);
/* grab bucket lock for bucket search */
pthread_mutex_lock(&H[h].mtx);
struct entry *ep = H[h].entries;
for (; ep != NULL; ep = ep->next)
if (strcmp(word, ep->word) == 0) {
/* already in table, just bump the count */
/* drop bucket lock */
pthread_mutex_unlock(&H[h].mtx);
/* lock the entry and update it */
pthread_mutex_lock(&ep->mtx);
ep->count++;
pthread_mutex_unlock(&ep->mtx);
return;
}
/* allocate new entry in table */
if ((ep = calloc(1, sizeof(*ep))) == NULL)
err(1, "calloc");
if ((ep->word = strdup(word)) == NULL)
err(1, "strdup");
ep->count = 1;
/* add it to the front of the linked list */
ep->next = H[h].entries;
H[h].entries = ep;
pthread_mutex_init(&ep->mtx, NULL);
pthread_mutex_unlock(&H[h].mtx);
}
#define MAXWORD 8192
/* break a test file into words and call count() on each one */
void *count_all_words(void *arg)
{
const char *fname = (const char *)arg;
FILE *fp;
int c;
char word[MAXWORD];
char *ptr;
if ((fp = fopen(fname, "r")) == NULL)
err(1, "fopen: %s", fname);
ptr = NULL;
while ((c = getc(fp)) != EOF)
if (isalpha(c)) {
if (ptr == NULL) {
/* starting a new word */
ptr = word;
*ptr++ = c;
} else if (ptr < &word[MAXWORD - 1])
/* add character to current word */
*ptr++ = c;
else {
/* word too long, truncate it */
*ptr++ = '\0';
count(word);
ptr = NULL;
}
} else if (ptr != NULL) {
/* word ended, store it */
*ptr++ = '\0';
count(word);
ptr = NULL;
}
/* handle the last word */
if (ptr != NULL) {
/* word ended, store it */
*ptr++ = '\0';
count(word);
}
fclose(fp);
return NULL;
}
/* print all entries in the hash table */
void print_counts()
{
struct entry *ep;
for (int i = 0; i < NBUCKETS; i++)
for (ep = H[i].entries; ep != NULL; ep = ep->next)
printf("%d %s\n", ep->count, ep->word);
}
int main(int argc, char *argv[])
{
int pflag = 0;
int arg = 1; /* index into argv[] for first file name */
if (argc > 1 && strcmp(argv[1], "-p") == 0) {
pflag++;
arg++;
}
if (argv[arg] == NULL) {
fprintf(stderr, "usage: %s [-p] wordfiles...\n", argv[0]);
exit(1);
}
int nfiles = argc - arg;
pthread_t tids[nfiles];
for (int i = 0; i < nfiles; i++)
if ((errno = pthread_create(&tids[i], NULL,
count_all_words, (void *)argv[arg++])) != 0)
err(1, "pthread_create %d of %d", i, nfiles);
for (int i = 0; i < nfiles; i++)
pthread_join(tids[i], NULL);
if (pflag)
print_counts();
exit(0);
}