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 7995559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:27:05+00:00 2026-06-04T14:27:05+00:00

My intention is to transpose two files using multithreading. But the program below is

  • 0

My intention is to transpose two files using multithreading. But the program below is giving me segmentation fault.

#include   <stdio.h>
#include  <stdlib.h>
#include <pthread.h>
#include  <string.h>


void *a_to_temp( void *filea);
void copyFile( FILE *in, FILE *out );
void *temp_to_b( void *fileb);
void *b_to_a(void *ab);

struct files{
    char a[80];
    char b[80];
} ab;

pthread_mutex_t temptob     = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t btoa        = PTHREAD_MUTEX_INITIALIZER;

main(int argc, char **argv)
{
    fprintf(stderr, "in main");

    pthread_t thread1, thread2, thread3;
    strcpy( ab.a, argv[1]);
    strcpy(ab.b, argv[2]);

    int  iret1, iret2, iret3;

    pthread_mutex_lock( &temptob );
    pthread_mutex_lock( &btoa );

    iret1 = pthread_create( &thread1, NULL, a_to_temp, (void*) &argv[1]);

    iret2 = pthread_create( &thread2, NULL, b_to_a, (void*) &ab);

    iret3 = pthread_create( &thread3, NULL, temp_to_b, (void*) &argv[2]);


    pthread_join( thread1, NULL);
    pthread_join( thread2, NULL);
    pthread_join( thread3, NULL);

    exit(0);
}


void *a_to_temp( void *filea) {
    FILE *a = fopen((char *)filea, "r");    
    FILE *f = fopen( "temp", "w");
    copyFile( a , f);
    fclose(f);
    fclose( a); 
    pthread_mutex_unlock( &temptob );
}

void *temp_to_b( void *fileb) {
    pthread_mutex_lock( &temptob );
    FILE *b = fopen((char *)fileb, "r");
    FILE *f = fopen( "temp", "r");
    copyFile( f, b);
    fclose(f);  
    pthread_mutex_unlock( &btoa );
}

void *b_to_a(void *ab) {
    pthread_mutex_lock( &btoa );
    FILE *a = fopen(((struct files *) ab)->a, "w"); //
    FILE *b = fopen(((struct files *) ab)->b, "r");//
    fprintf(stderr, "c files opened");
    copyFile( b, a);
    fclose(a);
    fclose(b);
}

void copyFile( FILE *in, FILE *out) {
    char ch;
    while(!feof(in)) {
        ch = getc(in);  
        if(!feof(in)) putc(ch, out);            
    }
}

I’ve tested the code until the end of the main function by printing values. I’m guessing that the error should be inside one of the functions.

  • 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-04T14:27:07+00:00Added an answer on June 4, 2026 at 2:27 pm

    You pass &argv[1] to the a_to_temp() function, which is a char **, and then try to use it as if it was a char *. Ditto for &argv[2] and the temp_to_b() function. This is not a recipe for happiness; a core dump is quite a plausible response.

    The simplest fix is to drop the & in the pthread_create() calls. The alternative is to handle the char ** in the called functions.

    Note: this simply fixes the core dumps caused by accessing the wrong data. There may be algorithmic problems with the code too, ensuring that the correct synchronization occurs. And it is debatable whether there is any performance gain to using threads here. Indeed, there probably isn’t. But that is presumably tangential to the point of the exercise, which is to get threaded code working at all.


    Can you enlighten me how to serialize my threads. I was trying to use mutexes which don’t seem to help.

    One item would be to ensure you error check every system call; at the moment, you are assuming everything will work. However, I think Jay is on the right track with ‘the thread that locks a mutex must unlock it too’. You probably want one or two conditions (pthread_cond_init() et al) controlling access to the files instead of the mutexes.

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

Sidebar

Related Questions

I know the whole intention of using CRC is to do error detection, but
This is the code section from inno setup.My intention is to make two Checkbox
I have the line of code below in my contact form with the intention
This question may sound silly.. but my intention is to understand advantages of Java
My intention is to investigate the Methods of a Type using reflection in order
My intention in this question is not to be pedantic, but rather to explore
I have no intention of just altering the link (I hear that's impossible, but
My intention is to create a dynamic 3D array in C++ using pointers. MyType***
I'm writing a CouchDB sample. My intention is to write a web app using
I have no intention of using this in real code. I promise. Does the

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.