/
pwords.c
167 lines (130 loc) · 3.1 KB
/
pwords.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include <time.h>
#define NUM_THREADS 4
pthread_mutex_t mutexdict;
typedef struct dict {
char *word;
int count;
struct dict *next;
} dict_t;
typedef struct thread_data{
FILE *infile;
dict_t *dict;
char *wordbuf;
}thread_data;
char *
make_word( char *word ) {
return strcpy( malloc( strlen( word )+1 ), word );
}
dict_t *
make_dict(char *word) {
dict_t *nd = (dict_t *) malloc( sizeof(dict_t) );
nd->word = make_word( word );
nd->count = 1;
nd->next = NULL;
return nd;
}
dict_t *
insert_word( dict_t *d, char *word ) {
// Insert word into dict or increment count if already there
// return pointer to the updated dict
dict_t *nd;
dict_t *pd = NULL; // prior to insertion point
dict_t *di = d; // following insertion point
// Search down list to find if present or point of insertion
while(di && ( strcmp(word, di->word ) >= 0) ) {
if( strcmp( word, di->word ) == 0 ) {
di->count++; // increment count
return d; // return head
}
pd = di; // advance ptr pair
di = di->next;
}
nd = make_dict(word); // not found, make entry
nd->next = di; // entry bigger than word or tail
if (pd) {
pd->next = nd;
return d; // insert beyond head
}
return nd;
}
void print_dict(dict_t *d) {
while (d) {
printf("[%d] %s\n", d->count, d->word);
d = d->next;
}
}
int
get_word( char *buf, int n, FILE *infile) {
//pthread_mutex_lock(&mutexword);
int inword = 0;
int c;
while( (c = fgetc(infile)) != EOF ) {
if (inword && !isalpha(c)) {
buf[inword] = '\0'; // terminate the word string
pthread_mutex_unlock(&mutexword);
return 1;
}
if (isalpha(c)) {
buf[inword++] = c;
}
}
// pthread_mutex_unlock(&mutexword);
return 0; // no more words
}
#define MAXWORD 1024
dict_t *
words( thread_data *TData ) {
int c=1;
do {
pthread_mutex_lock(&mutexdict);
c=get_word( TData->wordbuf, MAXWORD, TData->infile );
TData->dict = insert_word(TData->dict, TData->wordbuf); // add to dict
pthread_mutex_unlock(&mutexdict);
}while(c);
return TData->dict;
}
void* worker_thread(void* arg){
thread_data *TData;
TData=(thread_data *) arg;
TData->dict=words(TData);
pthread_exit(NULL);
}
int
main( int argc, char *argv[] ) {
pthread_t thread_ID[NUM_THREADS];
void *status;
pthread_mutex_init(&mutexdict, NULL);
FILE *infile = stdin;
if (argc >= 2) {
infile = fopen (argv[1],"r");
}
if( !infile ) {
printf("Unable to open %s\n",argv[1]);
exit( EXIT_FAILURE );
}
clock_t begin, end;
double time_spent;
dict_t *wd = NULL;
char wordbuf[MAXWORD];
thread_data *TD;
TD->infile=infile;
TD->dict=wd;
TD->wordbuf=wordbuf;
int i;
for(i=0;i<NUM_THREADS;i++){
pthread_create(&thread_ID[i],NULL,worker_thread,(void*) TD);
}
for(i=0;i<NUM_THREADS;i++){
pthread_join(thread_ID[i],&status);
}
print_dict( TD->dict );
int sta=pthread_mutex_destroy(&mutexdict);
printf("done %d\n",sta);
pthread_mutex_destroy(&mutexword);
fclose( infile );
}