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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:52:58+00:00 2026-06-01T08:52:58+00:00

I am trying to write a program which gets input files and prints included

  • 0

I am trying to write a program which gets input files and prints included items into the screen by threads. However, last thread doesn’t give any output unexpectedly. I couldn’t figure out what the problem is. I am waiting for your help.

Each thread gets 3 parameters :inputFile1, inputFile2 and targetBuf(lets say it is sequence number.) Files are sorted, I just simply try to print their union in order. Only positive numbers are printed.

Command line : merge N outfile file1 file2 file3 …. fileN
N is the number of input files.

If I give 2 input files (means 1 thread), it works. If I give 4 files, then 1st thread give output, 2nd one does not. If I give 6 input files, then 1st and 2nd threads give output but 3rd doesn’t.

There are two structs in header file. I pasted them below.

//thread information
struct threadInfo{
    pthread_t tid;
    pthread_attr_t attr;
};
//Beginning thread input
struct beginThreadInput{
    FILE **inputFile1, **inputFile2;
    int targetBuf;
};
typedef struct threadInfo THREADINFO;
typedef struct beginThreadInput BEGINT_INPUT;

Main File Code:

      #include <stdio.h>
      #include <unistd.h>
      #include <stdlib.h>
      #include <pthread.h>
      #include <semaphore.h>
      #include <sys/types.h>
      #include <errno.h>
      #include <sys/mman.h>
      #include <sys/stat.h>
      #include <unistd.h>
      #include <fcntl.h>    //for O_ constants
      #include <sys/stat.h>
      #include <pthread.h> /* thread library */

      #include "merge.h"



      int main(int argc, char** argv){
        int i, k, N;

        /***FILE INITIALIZATION***/
        FILE *output;
        N = atoi(argv[1]);
        output = fopen(argv[2], "w");
        FILE **inputFileList = ((FILE**)malloc (N * sizeof(FILE*)));
        printf("int N is %d\n", N);
        for(i = 0; i<N; i++){
            inputFileList[i] = fopen(argv[i + 3], "r");
        }
        //START THREADS
        BEGINT_INPUT **threadInputList = ((BEGINT_INPUT**) malloc ( (N/2)*        sizeof(BEGINT_INPUT*)));
        THREADINFO **threadInfoList = ((THREADINFO**) malloc ( (N/2) *          sizeof(THREADINFO*)));
        for(i = 0; i< N/2 ; i++){
            threadInputList[i] = (BEGINT_INPUT *) malloc (sizeof(BEGINT_INPUT));
            threadInfoList[i] = (THREADINFO *) malloc (sizeof(THREADINFO));
        }
        pthread_t tid;
        pthread_attr_t attr;
        for(i = 0, k = 0; i < (N/2); i++){


            threadInputList[i]->inputFile1 = &(inputFileList[k]);
            threadInputList[i]->inputFile2 = &(inputFileList[++k]);
            threadInputList[i]->targetBuf = i;
            pthread_attr_init(&(threadInfoList[i]->attr));
            pthread_create(&(threadInfoList[i]->tid), &(threadInfoList[i]->attr), runBeginningThreads, (void*)threadInputList[i]);

        }
        pthread_join((threadInfoList[[(N/2)-1]]->tid), NULL);
        for(i = 0; i<N; i++){
            fclose(inputFileList[i]);
        }

      }

      void *runBeginningThreads(void *input){

        BEGINT_INPUT *myInput = (BEGINT_INPUT *)input;
        int first = -1, second = -1, iseof;
        printf("Thread number %d\n", myInput->targetBuf);
        while((second > -2) && (first > -2)){
            //read integer values from files
            if(first == -1){
                iseof = fscanf(*(myInput->inputFile1), "%d", &first);
                if(iseof == EOF){
                    first = -2;     //means end of file
                }
                else if(first < 0)
                    first = -1;     //means waiting for an integer
            }
            if(second == -1){
                iseof = fscanf(*(myInput->inputFile2), "%d", &second);
                if(iseof == EOF){
                    second = -2;
                }
                else if(second < 0)
                    second = -1;
            }
            //print smaller one
            if((first != -1) && (second != -1)){
                if(((first < second) || (second == -2)) && (first != -2)){
                    printf("%d\n", first);
                    first = -1;
                }
                else if(second != -2){
                    printf("%d\n", second);
                    second = -1;
                }
            }
        }
      }
  • 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-01T08:52:59+00:00Added an answer on June 1, 2026 at 8:52 am

    I solve my question in the following way. As it is seen above, I open the files in main function, then pass them into corresponding threads. I have changed this way and passed the names of the files into threads and open related files in them.

    Additionally, while passing files, I was making mistakes, its code is below

     threadInputList[i]->inputFile1 = &(inputFileList[k]);
     threadInputList[i]->inputFile2 = &(inputFileList[++k]);
    

    This lines are run in a loop. This causes an input sequence like the following ([0,1], [1,2], [2,3], [3,4] …) becuase I increment k only one time.However, each thread takes two of inputFiles. So the correct code is this:

     threadInputList[i]->inputFile1 = &(inputFileList[k++]);
     threadInputList[i]->inputFile2 = &(inputFileList[k++]);
    

    I want to point out an issue. I fixed this code for file names, not FILE pointer variables as it is posted there. But to make it clear, I fixed the bug on a code posted there. Thanks for your help, everybody

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

Sidebar

Related Questions

I'm trying to write a program in Prolog, which will insert an element into
I am trying to write a program which will listen to the serial input
I am trying to write a program in PHP which I had already written
I'm trying to write a program in C++ which runs Conway's Game of Life.
I am trying to write a Java program or Hadoop Pig script which will
I'm trying to write a program which, at some point, needs to invoke an
Greetings, I'm trying to write a program in Python which would print a string
I'm trying to write a program which would write on the console at a
I am new to Ruby and I am trying to write a program which
I'm trying to write a program in which a program continuously takes in a

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.