-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask1.c
143 lines (97 loc) · 2.93 KB
/
task1.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
// This code belongs to IT18350906
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; // Initializing the mutex
struct inputGlobal{ // The global structure that holds all the stuff
int arr[128]; //This value 128 can be increased if needed
int m;
float answer; // Not used in the final build!
};
void* calculateAvg(void* args); //This function is to calculate the average
void writeToFile(float r); // This function prints it
int main(void)
{
/***********Counting the number of line to create an equal amount of threads************/
FILE *fileptr;
int count_lines = 0;
char filechar[40], chr;
// printf("Enter file name: ");
// scanf("%s", filechar);
fileptr = fopen("dataset.txt", "r");
//extract character from file and store in chr
chr = getc(fileptr);
while (chr != EOF)
{
//Count whenever new line is encountered
if (chr == '\n')
{
count_lines = count_lines + 1;
}
//take next character from file.
chr = getc(fileptr);
}
fclose(fileptr); //close file.
// printf("There are %d lines in %s in a file\n", count_lines, filechar);
/**********End of that code*************************************************************/
FILE *in = fopen("dataset.txt", "r"); //reading the file
if (in == NULL){
printf("Unable to open dataset.txt\n"); //Error handling
exit(0);
}
if (in != NULL) {
char line[BUFSIZ]; // declaring variables
int k,i;
int j=0; // to track the thread id
pthread_t threadID[count_lines+1];
struct inputGlobal buffer[count_lines+1];
while (fgets(line, sizeof line, in) != NULL) {
int field1;
int tmp = 0;
char *start1 = line;
int k=0;
j++;
while (sscanf(start1, "%d%n", &field1, &tmp) == 1) {
buffer[j].arr[k] = field1;
start1 += tmp; //The FLAG
k++;
}
buffer[j].m = k;
// Creating the threads
pthread_create(&threadID[j], NULL, calculateAvg, &buffer[j]);
}
// joining the threads
for(i=0; i<j; i++){
pthread_join(threadID[i], NULL);
}
fclose(in);
pthread_mutex_destroy(&lock);
return 0;
}
}
// Function Declaration
void writeToFile(float r){
FILE *out = fopen("average.txt","a");
fprintf(out, "%.3f\n", r);
fclose(out);
}
void* calculateAvg(void* args)
{
pthread_mutex_lock(&lock);
struct inputGlobal *buffer2_ptr = (struct inputGlobal*) args;
int sum = 0; // declaring variables
float avg = 0;
int x;
int g = 0;
g = (buffer2_ptr->m);
for(x=0; x < g; x++){
sum = sum + (buffer2_ptr->arr[x]);
}
printf("Sum is %d\n", sum);
avg = sum / g;
printf("Average is %f\n", avg);
writeToFile(avg); //Passing the calculated value to print function
pthread_mutex_unlock(&lock);
pthread_exit(0);
}