-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthreadprg.c
More file actions
249 lines (208 loc) · 7.01 KB
/
Copy pathpthreadprg.c
File metadata and controls
249 lines (208 loc) · 7.01 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Author: Himanshu Shah
Program to calculate word count for a single file using Pthreads.
-Counts characters including spaces and period.
-Number of Lines equals the number of period.
-Words are counted using spaces and period.
Date:Jan-16-2013
*/
#include <pthread.h>
#include <stdio.h>
typedef unsigned long count_t;
typedef enum {false=0, true=1} bool;
void *thread_function(void *);
void *excess_thread_function(void *);
// TOTAL COUNTERS
count_t total_ccount;
count_t total_wcount;
count_t total_lcount;
count_t total_error;
//Total counters for all files
count_t sum_total_ccount;
count_t sum_total_wcount;
count_t sum_total_lcount;
count_t sum_total_error;
//Number of threads
int NUM_THREADS=3;
// Counters for threads
struct thread_data_structure{
int thread_id;
count_t ccount;
count_t wcount;
count_t lcount;
count_t error;
char buffer[30];
};
//Main Function
int main(int argc, char **argv)
{
struct thread_data_structure thread_data[NUM_THREADS];
FILE *f;
int m=1;
pthread_t thread[NUM_THREADS],excess_thread;
if (argc < 2)
printf ("usage: wc FILE [FILE...]\n");
for (; m < argc; m++)
{
if (f = fopen(argv[m], "rt"))
{
count_t total_ccount=0;
count_t total_wcount=0;
count_t total_lcount=0;
count_t total_error=0;
int i,excess=0;
while (!feof(f))
{
bool flag=false;
i=0;
int round=0;
for(flag=false; i<NUM_THREADS;i++,round++)
{
excess=(fread(thread_data[i].buffer, 1, 30, f));
thread_data[i].thread_id=i;
if(excess==30){
pthread_create(&thread[i], NULL, thread_function, (void *) &thread_data[i]);
} //Create Threads
else{
//Invoke function that serially calculates the remaining arguments: buffer[i] and excess
thread_data[i].error=excess;
pthread_create(&excess_thread,NULL, excess_thread_function, (void*) &thread_data[i]);
flag=true;
break;
}
}
// Join Threads to the main thread and perform the addition operation
for(i=0; i<round;i++)
{
if(pthread_join(thread[i], NULL)){
fprintf(stderr, "Error joining thread[%d] \n",i);}
else{
total_ccount+=thread_data[i].ccount;
total_wcount+=thread_data[i].wcount;
total_lcount+=thread_data[i].lcount;
total_error+=thread_data[i].error;
// total_wcount+=(total_lcount/2)-1;
// printf("\n\n\t\t=====TOTAL AFTER JOINING THREAD[%d]=============\nTotal Character Counter: %6lu Total Word Count: %6lu Total Lines:%6lu",i,total_ccount,total_wcount,total_lcount);
}
}
if(flag){ /*Joining the Last thread- The Excess Thread*/
if(pthread_join(excess_thread, NULL)){
fprintf(stderr, "Error joining Excess thread \n");
}
else{
total_ccount+=thread_data[i].ccount;
total_wcount+=thread_data[i].wcount;
total_lcount+=thread_data[i].lcount;
total_error+=thread_data[i].error;
// printf("\n\n\t\t=====TOTAL AFTER JOINING EXCESS THREAD[%d]=============\nTotal Character Counter: %6lu Total Word Count: %6lu Total Lines:%6lu",i,total_ccount,total_wcount,total_lcount);
}
}//if flag
}//while
//total_wcount+=total_lcount;
printf("\n\n\t\n==============:%s:===========\n\tTotal Character Counter: %6lu \n\t Total Word Count: %6lu \n\t Total Lines:%6lu \n\t Total Special Characters: %6lu\n",argv[m],total_ccount,total_wcount,total_lcount,total_error);
fclose(f);
//Total counters for all files
sum_total_ccount+=total_ccount;
sum_total_wcount+=total_wcount;
sum_total_lcount+=total_lcount;
sum_total_error+=total_error;
}//if open
else//else open
{
printf("Error Opening File %s", argv[m]);
}
}//for loop
if(m>2)
{
printf("\n\n\t\n==============:TOTAL WORD COUNT FOR ALL FILES:===========\n\tTotal Character Counter: %6lu \n\t Total Word Count: %6lu \n\t Total Lines:%6lu \n\t Total Special Characters: %6lu\n",sum_total_ccount,sum_total_wcount,sum_total_lcount,sum_total_error);
}
}//main
//Function for Threads- USE: Process the 30 character large chunk of data and fill the Ccount,Wcount and Lcount.
void *thread_function(void *threadarg)
{
struct thread_data_structure *my_data;
my_data = (struct thread_data_structure *) threadarg;
my_data->ccount=0;
my_data->wcount=0;
my_data->lcount=0;
my_data->error=0;
// printf("\n=========================================================================================\nThread String Array[%d]:%s",my_data->thread_id,my_data->buffer);
int k=0;
while(k<30)
{
if(isalpha(my_data->buffer[k])|| my_data->buffer[k]== '.'){
// printf("\nTHREAD [%d] :IS ALPHA[%d] %c ||Char counter %6lu",my_data->thread_id,k,my_data->buffer[k],(my_data->ccount+1));
my_data->ccount++;
}
else if(my_data->buffer[k]=='\n')
{
my_data->wcount++;
// printf("\nTHREAD [%d] :IS NEWLINE[%d] %c||Lines counter %6lu",my_data->thread_id,k,my_data->buffer[k],(my_data->lcount+1));
my_data->lcount++;
my_data->ccount++;
}
else if(my_data->buffer[k]== ' ')
{
// printf("\nTHREAD [%d] :IS SPACE[%d] %c||Word counter %6lu",my_data->thread_id,k,my_data->buffer[k],(my_data->wcount+1));
my_data->ccount++;
if(isalpha(my_data->buffer[k+1]))
my_data->wcount++;
}
else
{
my_data->error++;
my_data->ccount++;
if(isalpha(my_data->buffer[k+1]))
my_data->wcount++;
// printf("\nTHREAD [%d] :ERROR Character [%d]: %c||Error counter %6lu",my_data->thread_id,k,my_data->buffer[k],my_data->error);
}
fflush(stdout);
// usleep(100*1000);
k++;
}
}
//Function for Excess characters- USE: Process the excess characters and fill the Ccount,Wcount and Lcount.
void *excess_thread_function(void *threadarg)
{
struct thread_data_structure *my_data;
my_data = (struct thread_data_structure *) threadarg;
int excess= my_data->error;
my_data->ccount=0;
my_data->wcount=0;
my_data->lcount=0;
my_data->error=0;
int k=0;
// printf("\n=========================================================================================\nThread String Array[%d]:%s",my_data->thread_id,my_data->buffer);
while(k<excess)
{
if(isalpha(my_data->buffer[k] || my_data->buffer[k]== '.')){
// printf("\nTHREAD [%d] :IS ALPHA[%d] %c||Char counter %6lu",my_data->thread_id,k,my_data->buffer[k],(my_data->ccount+1));
my_data->ccount++;
}
else if(my_data->buffer[k]=='\n')
{
my_data->wcount++;
// printf("\nTHREAD [%d] :IS NEWLINE[%d] %c||Lines counter %6lu",my_data->thread_id,k,my_data->buffer[k],(my_data->lcount+1));
my_data->lcount++;
my_data->ccount++;
}
else if(my_data->buffer[k]== ' ')
{
// printf("\nTHREAD [%d] :IS SPACE[%d] %c||Word counter %6lu",my_data->thread_id,k,my_data->buffer[k],(my_data->wcount+1));
my_data->ccount++;
// if(isalpha(my_data->buffer[k+1]))
my_data->wcount++;
}
else
{
my_data->error++;
// printf("\nTHREAD [%d] :ERROR Character [%d]: %c||Error counter %6lu",my_data->thread_id,k,my_data->buffer[k],my_data->error);
my_data->ccount++;
if(isalpha(my_data->buffer[k+1]))
my_data->wcount++;
}
fflush(stdout);
// usleep(100*1000);
k++;
}
}