I'm trying to use C multithreading to find out the frequency of each alphabet letter in a text file. Assignment is to: 1) write a function that read every single sentence in a text, ended by '.' 2) write a function that load a sentence in a bidimensional array 3) write a function that generates a pthread for every letter for every sentence (pthread function add 1 to a counter for that letter).
EDIT: I figured out with Valgrind that the problem is in sentence
function, by I dont understand why.
Here's the code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
int count[26];
char* sentence(char * s){
char* p;
char* q;
char* arr;
int i;
p = s;
q = malloc(100);
arr = q;
for (i=0; *p != '.'; i++){
*q = *p;
q++;
p++;
}
*q = '\0';
return arr;
}
char** load_sentence(char* p, char** q, int i){
q[i] = malloc(strlen(p)+1);
strcpy(q[i], p);
return q;
}
void* count_letter(void * s){
char* p = (char*) s;
int i;
for (i=0; i<26; i++){
if (*p == alphabet[i]){
count[i]++;
}
}
}
void frequency(char* str){
char* s = str;
int i, j, l;
l = strlen(str);
pthread_t tid[l];
for (i=0; i<l; i++){
pthread_create(&tid[i], NULL, count_letter, (void*) s);
s++;
}
for (j=0; j<l; j++){
pthread_join(tid[j], NULL);
}
}
int main(int argc, char* argv[]){
int fd;
char buff[100];
fd = open(argv[1], O_RDONLY);
char ** text = malloc(10*sizeof(char*));
read(fd, buff, sizeof(buff));
char* start = buff;
int i = 0; //number of phrases!
char* p = NULL;
while (*(p = sentence(start)) != '\0'){
text = load_sentence(p, text, i);
start += strlen(p)+1;
i++;
}
int j, k;
for (k=0; k<i; k++){
frequency(text[k]);
}
for (j=0; j<26; j++){
printf("%c : %d times\n", alphabet[j], count[j]);
}
}
It looks like that with cases like this:
hope it's a good reading. bye.
The output is correct:
a : 2 times
b : 1 times
c : 0 times
d : 2 times
e : 3 times
f : 0 times
g : 3 times
h : 1 times
i : 2 times
j : 0 times
k : 0 times
l : 0 times
m : 0 times
n : 1 times
o : 3 times
p : 1 times
q : 0 times
r : 1 times
s : 1 times
t : 1 times
u : 0 times
v : 0 times
w : 0 times
x : 0 times
y : 1 times
z : 0 times
With others, a "memory error", that begins with free() : invalid next size (normal)
. The error has many lines of memory map and ends with abortion.
I'm quite new to C, sorry for my inexperience.
Is it necessary to introduce a mutex
in this case?