Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:36:39+00:00 2026-06-14T20:36:39+00:00

I’m new at multithreading and I’m trying to simulate banking transactions on the same

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:36:40+00:00Added an answer on June 14, 2026 at 8:36 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.