I’m new at multithreading and I’m trying to simulate banking transactions on the same current account using multithreading.
Each thread reads the actions to perform from a file. The file will contain an operation for each line consisting of an integer. The main program have to create as many threads as files in the path.
int main(int argc,char*argv[]){
DIR *buff;
struct dirent *dptr = NULL;
pthread_t hiloaux[MAX_THREADS];
int i=0,j=0, nthreads=0;
char *pathaux;
memset(hiloaux,0,sizeof(pthread_t)*MAX_THREADS);
diraux=malloc((267+strlen(argv[1]))*sizeof(char));
buff=opendir(argv[1]);
while((dptr = readdir(buff)) != NULL && nthreads<MAX_THREADS)//read files in the path
{
if (dptr->d_name[0]!='.'){
pthread_mutex_lock(&mutex_path);//mutual exclusion
strcpy(pathaux,argv[1]);
strcat (pathaux,"/");
strcat (pathaux,dptr->d_name);//makes the route (ex:path/a.txt)
pthread_create(&hiloaux[nthreads],NULL,readfile,(void *)pathaux);
//creates a thread for each file in the path
nthreads++;
}
}
for (j=0;j<nthreads;j++){
pthread_join(hiloaux[j],NULL);
}
closedir(buff);
return 0;
}
My problem is that the threads don’t seem to receive the correct path argument. Even though I have placed a mutex, (mutex_path) they all read the same file. I unlock this mutex inside the function readfile(), .
void *readfile(void *arg){
FILE *fichero;
int x=0,n=0;
int suma=0;
int cuenta2=0;
char * file_path = (char*)arg;
n=rand() % 5+1; //random number to sleep the program each time I read a file line
pthread_mutex_unlock(&mutex_path);//unlock the mutex
fichero = fopen(file_path, "r");
while (fscanf (fichero, "%d", &x)!=EOF){
pthread_mutex_lock(&mutex2);//mutual exclusion to protect variables(x,cuenta,cuenta2)
cuenta2+=x;
if (cuenta2<0){
printf("count discarded\n");
}
else cuenta=cuenta2;
pthread_mutex_unlock(&mutex2);
printf("sum= %d\n",cuenta);
sleep(n); //Each time i read a line,i sleep the thread and let other thread read other fileline
}
pthread_exit(NULL);
fclose(fichero);
}
When I run the program i get this output
alberto@ubuntu:~/Escritorio/practica3$ ./ejercicio3 path
read file-> path/fb
read -> 2
sum= 2
read file-> path/fb
read -> 2
sum= 2
read file-> path/fb
read -> 4
sum= 6
read file-> path/fb
read -> 4
sum= 6
read file-> path/fb
read -> 6
sum= 12
read file-> path/fb
read -> 6
sum= 12
It seems to work well, it reads a line and sleeps for a time, during this time another thread do its work, but the problem is that both threads open the same file (path/fb).
As i said before i think the problem is in path argument, is like the mutex_path did not make his work.
I would really appreciate a little help with this, as I don’t really know what’s wrong.
Thank you very much.
In your “readfile” function
the line
char * file_path = (char*)arg;
Just copies a pointer the string memory but not the memory itself.
So it can (and will) still be altered by the man thread while worker thread continues.
Make a memory copy there.
Or even better store all arguments to your threads in distinct memory in main thread already, so you wont need the first mutex at all.